1.链栈:链表实现栈的基本操作 #include <stdio.h> #include <stdlib.h> struct node { int data; struct node * next; }; //初始化栈 struct node * init_stack(void) { struct node * top = NULL; return top; } //压栈 int push_stack(struct node ** top, int data) { struct node * new_node = NULL; new_node = (struct node*)malloc(sizeof(struct node)); if (new_node == NULL) { printf("malloc error!/n"); return -1; } new_node->data = data; new_node->next = *top; *top = new_node; return 0; } //出栈 int pop_stack(struct node** top) { int value = 0; if (*top == NULL) { printf("no data in stack!/n"); return -1; } value = (*top)->data; *top = (*top)->next; return value; } int main(int argc, char* argv[]) { int pop_value; struct node * top = NULL; top = init_stack(); push_stack(&top,1); push_stack(&top,2); push_stack(&top,3); pop_value = pop_stack(&top); printf("%d/n",pop_value); pop_value = pop_stack(&top); printf("%d/n",pop_value); pop_value = pop_stack(&top); printf("%d/n",pop_value); }