#include <stdio.h>#include <stdlib.h> #define fail 0 #define success 1 #define status int typedef struct node{ int data; struct node *next; }*Snode,*pStack,*LinkedStack; LinkedStack myStack=(LinkedStack)malloc(sizeof(struct node));pStack top; //初始化链栈 LinkedStack makeStack(LinkedStack myStack,pStack top) { myStack->data=-1; myStack->next=NULL; top=myStack; return myStack; } //判空 bool Stack_Empty(void) { return (top==myStack); } //入栈 status Push(LinkedStack myStack,int data) { LinkedStack cursor=myStack; Snode newNode=(Snode)malloc(sizeof(struct node)); newNode->data=data; newNode->next=NULL; top=newNode; while(cursor->next) { cursor=cursor->next; } cursor->next=newNode; //此处还要补上将该新结点插入链栈myStack的末尾 return success; } //出栈 int* Pop(LinkedStack myStack,int *data) { pStack cursor=myStack; if(Stack_Empty()) { } else { while(cursor->next!=top) { cursor=cursor->next; } data=&(top->data); top=cursor; return data; } } //程序测试 int main(void) { int *data=NULL; myStack=makeStack(myStack,top); Push(myStack,3); Push(myStack,8); Push(myStack,19); Push(myStack,31); Push(myStack,43); Push(myStack,36); Pop(myStack,data); printf("%d ",data); }
胡子哥哥
相关分类