#include<stdio.h>/*带头结点的链栈操作(出栈和入栈)王红刚2009/8/12完成*/ #include<stdlib.h>/*程序只用作学习交流,如要转载请注明出处!!·*/ #include <malloc.h> #define ERROR 0 #define OK 1 typedef int StackElemType; typedef struct Node { StackElemType data; struct Node *next; }LinkStackNode,*LinkStack; void InitListStack(LinkStack *top)//*top为初始化链栈的头指针变量// { *top=(LinkStackNode *)malloc(sizeof(LinkStackNode));//建立头结点 (*top)->next=NULL;//建立空的链栈 } LinkStack Push(LinkStack top,StackElemType x)//入栈 { LinkStackNode *temp; temp=(LinkStackNode *)malloc(sizeof(LinkStackNode)); if(temp==NULL) return ERROR; temp->data=x; temp->next=top->next; top->next=temp; return top->next; //return OK; } int Pop(LinkStack top,StackElemType *x)//出栈 { LinkStackNode *temp; temp=top->next; if(temp==NULL) return ERROR; top->next=temp->next; *x=temp->data; printf("%d ",*x); free(temp); return OK; } void main() { LinkStack TOP; LinkStackNode *p,*q; int m,n,i; InitListStack(&TOP); printf("please input the LinkStack Length:"); scanf("%d",&m); printf("/nplease input the LinkStack value:"); for(i=0;i<m;i++) { scanf("%d",&n); q=Push(&p,n); } printf("Input the LinkStack is ok!/n "); printf("pop the stack is:"); for(i=0;i<m;i++) Pop(q,&q->data);//好像有问题 }