堆栈

来源:互联网 发布:化妆品成分查询软件 编辑:程序博客网 时间:2024/05/01 05:44
#include<stdio.h>#include<stdlib.h>#include<stdbool.h>#define size 10typedef int datatype;typedef struct  {datatype data[size];int top;}stack_node, * stack_quenue;bool is_empty(stack_quenue s){return s->top == -1;}bool is_full(stack_quenue s){return s->top >= size-1;}void init(stack_quenue *s){(*s)->top = -1;}bool push(stack_quenue s,int tmp){if(is_full(s)){printf("the stack is full\n");return false;}else {(s->top)++;s->data[s->top]=tmp;return true;}}bool pop(stack_quenue s,int *x){if(is_empty(s)){return false;}else{*x=s->data[s->top];s->top--;return true;}}int main(void){stack_node s;stack_quenue q;q=&s;init(&q);int tmp;puts("please input the data to enter stack");while(1){int ret;ret = scanf("%d",&tmp);if(ret == 1){if(push(q,tmp))continue;elsebreak;}else{break;}}int x;printf("the order if data's exit is :\n");while(q->top <= size-1){if(pop(q,&x)){printf("%d ",x);}else{break;}}printf("\n");}


原创粉丝点击