一、头文件:

  1. #ifndef _STACK_LINK_H_
  2. #define _STACK_LINK_H_
  3.  
  4. struct stack_record;
  5. typedef struct stack_record* stack;
  6. typedef int elementType;
  7. struct link_node;
  8. typedef struct link_node node;
  9.  
  10. int IsEmpty(stack s);
  11. int IsFull(stack s);
  12. stack creatStack(int maxElement);
  13. void disposeStack(stack s);
  14. void makeEmpty(stack s);
  15. void pushElement(elementType x,stack s);
  16. elementType Top(stack s);
  17. void popElement(stack s);
  18. elementType topAndpop(stack s);
  19.  
  20. #define EMPTY 0
  21. #define MINISIZE (5)
  22.  
  23. struct stack_record
  24. {
  25. int capacity;
  26. int size;
  27. node *item;
  28. };
  29.  
  30. struct link_node
  31. {
  32. elementType data;
  33. node *next;
  34. };
  35.  
  36. #endif

二、c文件:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "stack_link.h"
  4.  
  5. #define MAXSIZE 100
  6.  
  7. int IsEmpty(stack s)
  8. {
  9. return s->size == 0;
  10. }
  11.  
  12. int IsFull(stack s)
  13. {
  14. return s->size == s->capacity;
  15. }
  16.  
  17. stack creatStack(int maxElement)
  18. {
  19. stack s;
  20. s = (stack)malloc(sizeof(struct stack_record));
  21. if(s == NULL)
  22. {
  23. printf("the stack alloca error\n");
  24. exit(-1);
  25. }
  26.  
  27. s->capacity = MAXSIZE;
  28. s->item = NULL;
  29. s->size = 0;
  30.  
  31. return s;
  32. }
  33.  
  34. void disposeStack(stack s)
  35. {
  36. if(s)
  37. {
  38. if(s->size)
  39. makeEmpty(s);
  40.  
  41. free(s);
  42. }
  43. }
  44.  
  45. void makeEmpty(stack s)
  46. {
  47. if(s)
  48. {
  49. while(s->size)
  50. {
  51. popElement(s);
  52. }
  53. }
  54. }
  55.  
  56. void pushElement(elementType x,stack s)
  57. {
  58. if(s)
  59. {
  60. if(IsFull(s))
  61. return;
  62.  
  63. node *ptr = (node *)malloc(sizeof(node));
  64. if(NULL == ptr)
  65. {
  66. printf("the node alloc is error\n");
  67. exit(-2);
  68. }
  69.  
  70. ptr->data = x;
  71. ptr->next = s->item;
  72. s->item = ptr;
  73. s->size++;
  74. }
  75. }
  76.  
  77. elementType Top(stack s)
  78. {
  79. if(s)
  80. {
  81. if(IsEmpty(s))
  82. {
  83. printf("the stack is empty\n");
  84. exit(-3);
  85. }
  86. return s->item->data;
  87. }
  88. }
  89.  
  90. void popElement(stack s)
  91. {
  92. if(IsEmpty(s) )
  93. {
  94. printf("the stack is empty\n");
  95. exit(-4);
  96. }
  97.  
  98. node *ptr = NULL;
  99. ptr = s->item->next;
  100. free(s->item);
  101. s->item = ptr;
  102. s->size--;
  103. }
  104.  
  105. elementType topAndpop(stack s)
  106. {
  107. if(s)
  108. {
  109. if(IsEmpty(s))
  110. {
  111. printf("the stack is empty\n");
  112. exit(-5);
  113. }
  114.  
  115. elementType x;
  116. x = Top(s);
  117. popElement(s);
  118. return x;
  119. }
  120. }
  121.  
  122. int main(int argc,char *argv[])
  123. {
  124. stack s;
  125.  
  126. s = creatStack(10);
  127. int i = 0;
  128. elementType x;
  129. while(++i <= 10)
  130. {
  131. pushElement(i,s);
  132. printf("the stack size is %d\n",s->size);
  133. x = Top(s);
  134. printf("the stack top is %d\n",x);
  135. }
  136.  
  137. while(s->size)
  138. {
  139. x = topAndpop(s);
  140. printf("the top stack is %d\n",x);
  141. sleep(1);
  142. }
  143.  
  144. disposeStack(s);
  145.  
  146. return 0;
  147. }

三、打印输出:

  1. the stack size is 1
  2. the stack top is 1
  3. the stack size is 2
  4. the stack top is 2
  5. the stack size is 3
  6. the stack top is 3
  7. the stack size is 4
  8. the stack top is 4
  9. the stack size is 5
  10. the stack top is 5
  11. the stack size is 6
  12. the stack top is 6
  13. the stack size is 7
  14. the stack top is 7
  15. the stack size is 8
  16. the stack top is 8
  17. the stack size is 9
  18. the stack top is 9
  19. the stack size is 10
  20. the stack top is 10
  21. the top stack is 10
  22. the top stack is 9
  23. the top stack is 8
  24. the top stack is 7
  25. the top stack is 6
  26. the top stack is 5
  27. the top stack is 4
  28. the top stack is 3
  29. the top stack is 2
  30. the top stack is 1

用链表实现栈----《数据结构与算法分析----C语言描述》的更多相关文章

  1. 数据结构与算法分析——C语言描述 第三章的单链表

    数据结构与算法分析--C语言描述 第三章的单链表 很基础的东西.走一遍流程.有人说学编程最简单最笨的方法就是把书上的代码敲一遍.这个我是头文件是照抄的..c源文件自己实现. list.h typede ...

  2. 《数据结构与算法分析——C语言描述》ADT实现(NO.00) : 链表(Linked-List)

    开始学习数据结构,使用的教材是机械工业出版社的<数据结构与算法分析——C语言描述>,计划将书中的ADT用C语言实现一遍,记录于此.下面是第一个最简单的结构——链表. 链表(Linked-L ...

  3. C语言学习书籍推荐《数据结构与算法分析:C语言描述(原书第2版)》下载

    维斯 (作者), 冯舜玺 (译者) <数据结构与算法分析:C语言描述(原书第2版)>内容简介:书中详细介绍了当前流行的论题和新的变化,讨论了算法设计技巧,并在研究算法的性能.效率以及对运行 ...

  4. 最小正子序列(序列之和最小,同时满足和值要最小)(数据结构与算法分析——C语言描述第二章习题2.12第二问)

    #include "stdio.h" #include "stdlib.h" #define random(x) (rand()%x) void creat_a ...

  5. 《数据结构与算法分析-Java语言描述》 分享下载

    书籍信息 书名:<数据结构与算法分析-Java语言描述> 原作名:Data Structures and Algorithm Analysis in Java 作者: 韦斯 (Mark A ...

  6. 《数据结构与算法分析:C语言描述_原书第二版》CH3表、栈和队列_reading notes

    表.栈和队列是最简单和最基本的三种数据结构.基本上,每一个有意义的程序都将明晰地至少使用一种这样的数据结构,比如栈在程序中总是要间接地用到,不管你在程序中是否做了声明. 本章学习重点: 理解抽象数据类 ...

  7. 《数据结构与算法分析——C语言描述》ADT实现(NO.01) : 栈(Stack)

    这次的数据结构是一种特殊的线性表:栈(Stack) 栈的特点是后入先出(LIFO),可见的只有栈顶的一个元素. 栈在程序中的地位非常重要,其中最重要的应用就是函数的调用.每次函数调用时都会创建该函数的 ...

  8. 使用链表实现队列------《数据结构与算法分析-C语言描述》

    经过ubuntu的gcc验证 一.头文件 que_link.h #ifndef _QUE_LINK_H_ #define _QUE_LINK_H_ struct que_record; typedef ...

  9. 读书笔记:《数据结构与算法分析Java语言描述》

    目录 第 3 章 表.栈和队列 3.2 表 ADT 3.2.1 表的简单数组实现 3.2.2 简单链表 3.3 Java Collections API 中的表 3.3.1 Collection 接口 ...

随机推荐

  1. MySQL数据库触发器(trigger)

    MySQL触发器(trigger):监视某种情况并触发某种操作 一:四要素 触发时间:before/after 地点:table 监视操作:insert/update/delete 触发操作:inse ...

  2. (1)前言——(10)jquery项目的历史(History of the jQuery project)

    This book covers the functionality and syntax of jQuery 1.6.x, the latest version at the time of wri ...

  3. 52. 模版和设计元素——Lotus Notes的代码重用

    不论是理论上还是实用上,代码重用都是编程的一个重要议题.可以从两个角度来讨论代码重用. 一是逻辑上代码以怎样的方式被重用.既可以通过面向对象的思想普及以来耳熟能详的继承的方式.比如先建了一个车的基类, ...

  4. .NET常见面试题

    面试题 1  什么是 CTS.CLS 和CLR 公共语言运行库(CLR)是一个CLI 的一个实现,包含了.NET 运行引擎和符合 CLI 的类库. 通用类型系统(CTS)包含在微软公司提交的 CLI ...

  5. 【linux】常用网站

    Kernel: http://www.kernel.org/ LSB (Linux Standard Base): http://www.linuxbase.org/ ELC(Embedded Lin ...

  6. BSGS_Baby steps giant steps算法

    BSGS这个主要是用来解决这个题: A^x=B(mod C)(C是质数),都是整数,已知A.B.C求x. 在具体的题目中,C一般是所有可能事件的总数. 解: 设m = ceil(sqrt(C))(ce ...

  7. Android Sip学习(三)Android Voip实现

    Android Sip学习(三)Android Voip实现   Android Sip学习(准备知识)SIP 协议完整的呼叫流程 Android Sip学习(一)Android 2.3 APIs S ...

  8. MySQL内存表(MEMORY)说明 | 一个PHP程序员的备忘录

    MySQL内存表(MEMORY)说明 | 一个PHP程序员的备忘录 MySQL内存表(MEMORY)说明

  9. LA - 4043 - Ants

    题意:n只蚂蚁,n棵树,每只蚂蚁要连一棵树,连线(直线)不能相交,给出n只蚂蚁和n棵树的坐标,输出n只蚂蚁所配对的树的编号(1 <= n <= 100, -10000 <= 坐标x, ...

  10. NEU 1173: 这是物理学的奇迹!! 分解质数

    1173: 这是物理学的奇迹!! 题目描述 goagain在做物理电学实验时需要一个2Ω的电阻,但是他发现他的实验台上只剩下了3Ω,4Ω,5Ω,6Ω的电阻若干,于是goagain把两个4Ω的电阻并联起 ...