数据结构-堆栈-链表实现

来源:互联网 发布:软件开发运维驻场合同 编辑:程序博客网 时间:2024/05/20 06:24
/* * 堆栈 链表实现 * * */#include <stdio.h>#include <stdlib.h>#include <stdbool.h>//定义结构typedef struct _Stack * Stack;struct _Stack{    int data;    Stack next;};//创建一个堆栈Stack CreakStack(){    Stack stack=(Stack)malloc(sizeof(struct _Stack));    stack->next=NULL;    return stack;}//判断是否为空bool isEmpty(Stack stack){    return (stack->next==NULL);}//压栈bool Push(Stack stack,int num){    Stack stack1=(Stack)malloc(sizeof(struct _Stack));    stack1->data=num;    stack1->next=stack->next;    stack->next=stack1;    return true;}//出栈int Pop(Stack stack){    if(isEmpty(stack)){        printf("堆栈空");        return NULL;    }    else{        Stack stack1;        int numS;        stack1=stack->next;        numS=stack1->data;        stack->next=stack1->next;        free(stack1);        return numS;    }}//测试int main(){    Stack stack=CreakStack();    Pop(stack);    for (int i = 0; i < 10; i++) {        Push(stack,i);    }    Push(stack,10);    for(int i=0;i<10;i++){        printf("%d  ",Pop(stack));    }    return 0;}