堆栈的实现(LIFO、链表)
来源:互联网 发布:苹果手机数据恢复软件 编辑:程序博客网 时间:2024/06/08 07:46
#include<stdio.h>#include<stdlib.h>#include<assert.h>#define stack_size100typedefstruct stack_node{ struct stack_node *next; int value;}stack_node;static stack_node *stack;void destroy_stack();void push(int value);void pop();int top();void create_stack(int size){}void destroy_stack(){ while(stack) pop();}void push(int value){ stack_node *new_node; new_node=(stack_node *)malloc(sizeof(stack_node)); assert(new_node!=NULL); new_node->value=value; new_node->next=stack; stack=new_node;}void pop(){ stack_node *node; assert(stack!=NULL); node=stack; stack=node->next; free(node);}int top(){ assert(stack!=NULL); return stack->value;}int main(){ push(5); push(4); push(3); while(stack!=NULL) { printf("%d",top()); pop(); } return 0;}