/* linkStack.c */
/* 链栈 */ #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h> /* 链栈数据结构 */
/*
————————————————
| value | next | <--- top
————————————————

————————————————
| value | next |
————————————————

————————————————
| value | next |
————————————————
*/
typedef struct node {
int data;
struct node *next;
} StackNode; void interface(void);
/* 链栈函数声明 */
StackNode *initializeLinkStack();
bool isEmptyLinkStack(StackNode*);
StackNode *pushLinkStack(StackNode*, int);
StackNode *popLinkStack(StackNode*); /* 程序主函数入口 */
int main(){
StackNode *top = initializeLinkStack();
int flag, num; interface();
for(;;){
printf("Command: ");
scanf("%d", &num);
switch(num){
case : puts("Bye!"); return ; break;
case :
printf("Enter number: ");
scanf("%d", &num);
top = pushLinkStack(top, num);
break;
case :
if(isEmptyLinkStack(top))
printf("Link stack is empty!\n");
else
top = popLinkStack(top);
break;
}
} return ;
} /* 用户界面 */
void interface(void){
puts("+******************************+");
puts("+ 0, quit 退出 +");
puts("+ 1, push 压入 +");
puts("+ 2, pop 弹出 +");
puts("+******************************+");
}
/* 链栈函数实现 */
/* 初始化链栈 */
StackNode *initializeLinkStack(){
StackNode *top = (StackNode*)malloc(sizeof(StackNode));
top = NULL;
return top;
}
/* 链栈是否为空 */
bool isEmptyLinkStack(StackNode* top){
if(top==NULL)
return true;
else
return false;
}
/* 入栈 */
StackNode *pushLinkStack(StackNode *top, int number){
StackNode *s = (StackNode*)malloc(sizeof(StackNode));
s->data = number;
s->next = top;
top = s;
return top;
}
/* 出栈 */
StackNode *popLinkStack(StackNode *top){
int i = top->data;
StackNode *p = top;
top = top->next;
free(p);
printf("Pop: %d\n", i);
return top;
}

最新文章

  1. 将Excel文件转换为Html
  2. java基础 集合类
  3. 使用exp进行SQL报错注入
  4. InstallShield 2012 Spring优惠升级到最新版本(2015.4.30之前)
  5. 使用git推送代码到开源中国以及IDEA环境下使用git
  6. 小程序---根据数据库反向生成java文件
  7. python3爬虫初探(三)之正则表达式
  8. sql2008_x64 读取excel
  9. bash 脚本
  10. PHP关于foreach使用引用变量的坑
  11. C# 6 与 .NET Core 1.0 高级编程 - 41 ASP.NET MVC(上)
  12. 向ajaxform和ajaxgrid中添加数据
  13. 《Jave并发编程的艺术》学习笔记(1-2章)
  14. 【Keras篇】---利用keras改写VGG16经典模型在手写数字识别体中的应用
  15. day 15 - 1 内置函数
  16. Sublime 个人配置
  17. web网站在不同设备下进行缩放
  18. docker镜像打包save,载入load,启动run
  19. ABP捕捉异常错误代码
  20. L1-014 简单题

热门文章

  1. [算法模版]Tarjan爷爷的几种图论算法
  2. ItelliJ Idea 2019提交TFVC变更,系统提示Validation must be performed before checking in
  3. 软件----- idea 配置创建一个简单javase项目
  4. SpringBoot整合kafka(实现producer和consumer)
  5. dedecms5.7的文章详情页页面标题加载指定txt文本的随机关键字
  6. C# 方法默认访问级别 : private C# 类默认访问级别 : internal
  7. SATA、PCIe、AHCI、NVMe
  8. go 1.11 模块和版本管理
  9. Python - 正则表达式 - 第二十二天
  10. SpringBoot+Swagger整合