会员
众包
新闻
博问
闪存
赞助商
HarmonyOS
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
会员中心
简洁模式
...
退出登录
注册
登录
1989huangkq
huangkq1989
博客园
|
首页
|
新随笔
|
新文章
|
联系
|
订阅
|
管理
栈
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define STACK_SIZE 10 typedef unsigned int DATA; typedef struct STACK { DATA data[STACK_SIZE]; int top; }Stack; Stack stack; void initial() { stack.top = 0; for(int i=0; i<STACK_SIZE; i++) { stack.data[i] = 0; } } void push(DATA data) { if(stack.top == STACK_SIZE) { printf("%s\a\n","stack is full!"); }else{ stack.data[stack.top++] = data; } } bool is_empty() { return stack.top == 0; } DATA pop() { if(is_empty()) { printf("%s\a\n","stack is empty now"); return -1; }else{ return stack.data[--stack.top]; } } void display() { printf("Size is %d :",stack.top); for(int i=0; i<stack.top; i++) printf("%d ",stack.data[i]); printf("\n"); } int main() { initial(); while(1) { int choice_input; int data_input; printf("==================================\n"); printf("===0: initial 1:push 2:pop ====\n"); printf("===3: see it's empty or not ====\n"); printf("==================================\n"); scanf("%d",&choice_input); switch(choice_input) { case 0: initial(); break; case 1: scanf("%d",&data_input); push(data_input); display(); break; case 2: printf("%d\n",pop()); display(); break; case 3: printf("%s\n",is_empty()?"Empty":"Not empty!"); break; default: printf("sorry,unaccepted input!\n"); } } return 0; }
发表于
2011-08-27 23:25
huangkq1989
阅读(
100
) 评论(
0
)
收藏
举报
刷新页面
返回顶部
公告