C实现链式栈
/***************************************** C实现链式栈*****************************************/#include<stdio.h>#include<stdlib.h>#define OK 1#define ERROR 0typedef int StackData;typedef struct node{ StackData data; //结点数据 struct node * next; //结点链指针} StackNode;StackNode * InitStack(void) { StackNode *top; top=NULL;return (top);}int Push ( StackNode **top, StackData x ) { StackNode *p =(StackNode*)malloc(sizeof(StackNode)); if(top==NULL) { *top=p;p->data = x; p->next=NULL; }else{p->data = x; p->next = *top;*top = p; }return 1;}int StackEmpty (StackNode *top){ return (top==NULL);}int Pop(StackNode **top,StackData * x) { if ( StackEmpty (*top) ) return 0; StackNode * p = (*top); *top = p->next; (*x) = p->data; free (p); return 1;} int GetTop (StackNode *top, StackData *x ) { if ( StackEmpty (top) ) return 0; (*x) = top->data; return 1;}int main(){ int i=0;StackData x=0;StackNode *top;top=InitStack();for(i=0;i<10;i++){ Push(&top,i);}GetTop(top,&x);printf("%d\n",x);for(i=0;i<10;i++){Pop(&top,&x);printf("%d\n",x);}return 0;}