用链表实现栈
1 /****************************** 2 * 3 * 用链表表示栈 4 * 5 *******************************/ 6 7 #include <stdio.h> 8 #include <stdlib.h> 9 10 typedef struct linkStack{ 11 char data; 12 struct linkStack* next; 13 }linkStack; 14 15 linkStack* push(linkStack* stack, char a) 16 { 17 linkStack* link = (linkStack*)malloc(sizeof(linkStack)); 18 link->data = a; 19 link->next = stack; 20 stack = link; 21 22 return link; 23 } 24 25 linkStack* pop(linkStack* stack) 26 { 27 if(stack) 28 { 29 linkStack* p = stack; 30 stack = stack->next; 31 printf("出栈元素:%c ",p->data); 32 33 if(stack) 34 { 35 printf("栈顶元素:%c ",stack->data); 36 } 37 else 38 { 39 printf("栈已空 "); 40 } 41 free(p); 42 } 43 else 44 { 45 printf("栈内没有元素 "); 46 return stack; 47 } 48 49 return stack; 50 } 51 52 int main() 53 { 54 linkStack* stack = NULL; 55 56 stack = push(stack, 'a'); 57 stack = push(stack, 'b'); 58 stack = push(stack, 'c'); 59 stack = push(stack, 'd'); 60 stack = pop(stack); 61 stack = pop(stack); 62 stack = pop(stack); 63 stack = pop(stack); 64 stack = pop(stack); 65 66 return 0; 67 }