1 #include<stdio.h> 2 #include<stdlib.h> 3 4 typedef int DataType; 5 struct Node { 6 DataType data; 7 struct Node* next; 8 }; 9 typedef struct Node *PNode; 10 typedef struct Node *LinkStack; 11 12 // 定义一个栈 13 LinkStack SetNullStack_Link() 14 { 15 LinkStack top = (LinkStack)malloc(sizeof(struct Node)); 16 if (top != NULL) top->next = NULL; 17 else printf("Alloc failure"); 18 return top; 19 } 20 21 //判断是否为空栈 22 int IsNullStack_link(LinkStack top) 23 { 24 if (top->next == NULL) 25 return 1; 26 else 27 return 0; 28 } 29 30 //入栈 31 void Push_link(LinkStack top, DataType x) 32 { 33 PNode p; 34 p = (PNode)malloc(sizeof(struct Node)); 35 if (p == NULL) 36 printf("Alloc failure"); 37 else 38 { 39 p->data = x; 40 p->next = top->next; 41 top->next = p; 42 } 43 } 44 45 //出栈 46 void Pop_link(LinkStack top) 47 { 48 PNode p; 49 if (top->next == NULL) 50 printf("it is empty stack!"); 51 else 52 { 53 p = top->next; 54 top->next = p->next; 55 free(p); 56 } 57 } 58 59 //输出栈里面对应的数据 60 DataType Top_link(LinkStack top) 61 { 62 if (top->next == NULL) 63 { 64 printf("It is empty stack!"); 65 return 0; 66 } 67 else 68 return top->next->data; 69 } 70 int main() 71 { 72 LinkStack stackA = SetNullStack_Link(); 73 DataType data; 74 scanf("%d,", &data); 75 while (data != -1) 76 { 77 78 Push_link(stackA,data); 79 scanf("%d,", &data); 80 } 81 while (!IsNullStack_link(stackA)) 82 { 83 84 printf("%d",Top_link(stackA)); 85 Pop_link(stackA); 86 } 87 return 0; 88 }