#include#include #define Status int //数据类型 typedef struct { int number; }ElemType; //结点类型 typedef struct LNode { LNode *next; ElemType data; }LNode, *linkList; //初始化链表 Status InitStack(linkList &S) { if(!S) { S = (LNode *)malloc(sizeof(LNode)); S->next = NULL; } return 1; } //插入结点 Status Push(linkList &S, ElemType e) { if(!S) return 0; LNode *p = S; //注意链表的栈顶元素是S->next LNode *q = (LNode *)malloc(sizeof(LNode)); q->data = e; q->next = NULL; q->next = p->next; p->next = q; return 1; } //删除栈顶结点 Status Pop(linkList &S,ElemType &e) { if(!S && !S->next) return 0; LNode *p = S; LNode *q; q = p->next; //注意链表的栈顶元素是S->next e = q->data; p->next = q->next; free(q); return 1; } //取栈顶元素 Status GetTop(linkList &S,ElemType &e) { if(!S && !S->next) return 0; e = S->next->data; //注意链表的栈顶元素是S->next return 1; } //置空栈 Status ClearStack(linkList &S) { if(!S || !S->next) return 0; S->next = NULL; return 1; } //判断是否为空栈 Status StackEmpty(linkList &S) { if(!S) return 0; if(!S->next) return 1; return 0; } //摧毁栈 Status DestroyStack(linkList &S) { if(!S) return 0; LNode *p,*q; p = S; while(p) { q = p->next; free(p); p = q; } return 1; } void main() { linkList S; S = NULL; InitStack(S); ElemType x,y; int num; printf("输入入栈元素n"); for(int i=0;i<=3;i++) { scanf("%d",&num); x.number = num; Push(S,x); } printf("删除栈顶元素n"); Pop(S,y); printf("%dn",y.number); printf("返回栈顶元素n"); GetTop(S,y); printf("%dn",y.number); ClearStack(S); if(StackEmpty(S)) printf("现在是空栈!n"); if(DestroyStack(S)) printf("销毁栈成功!n"); }



