堆栈

来源:互联网 发布:linux下oracle自启动 编辑:程序博客网 时间:2024/05/16 06:43

栈:只允许一端插入和删除的线性表。(后进先出)

#include<stdio.h>#include<stdlib.h>#define MAX 10struct stack{int top;                  //栈顶指针int stack[8];             //栈大小,可理解为几层};typedef struct stack Stack1;typedef struct stack * Stack2;enum is_stack{FULL_OK = 100,FULL_NO,EMPTY_OK,EMPTY_NO};void create_stack(Stack2 * stack){*stack = (Stack2)malloc(sizeof(Stack1));if(*stack == NULL){printf("malloc error!\n");exit(-1);}}void init_stack(Stack2 stack){stack -> top = -1;}int push_stack(Stack2 stack,int num){if(stack -> top == 7){printf("push error!\n");return FULL_OK;}stack ->top++;stack->stack[stack -> top] = num;printf("push ok!\n");}int pop_stack(Stack2 stack){int n;if(stack -> top == -1){printf("pop error!\n");return EMPTY_OK;}return  stack -> stack[stack -> top--];}int main(){int i;Stack2 stack;create_stack(&stack);init_stack(stack);for(i = 0; i < MAX; i++){push_stack(stack,i+1);}for(i = 0;i < MAX;i++){int num;num = pop_stack(stack);if(num == EMPTY_OK);elseprintf("%d\n",num);}}



0 1