1. /* stack.h */
  2.  
  3. #ifndef _stack_h
  4. #define _stack_h
  5.  
  6. struct stack_record;
  7. typedef struct stack_record *stack;
  8. typedef int element_type;
  9.  
  10. int is_empty( stack s );
  11. int is_full( stack s );
  12. stack create_stack( int max_elements );
  13. void dispose_stack( stack s );
  14. void make_empty( stack s );
  15. void push( element_type x, stack s );
  16. element_type top( stack s );
  17. void pop( stack s );
  18. element_type top_and_pop( stack s );
  19.  
  20. #endif
  1. /* stack.c */
  2.  
  3. #include "stack.h"
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. #define MIN_STACK_SIZE 5
  8.  
  9. struct stack_record
  10. {
  11. int capacity;
  12. int top_of_stack;
  13. element_type *array;
  14. };
  15.  
  16. stack
  17. create_stack( int max_elements )
  18. {
  19. stack s;
  20.  
  21. if( max_elements < MIN_STACK_SIZE )
  22. {
  23. printf( "Stack size is too small\n" );
  24. exit(0);
  25. }
  26.  
  27. s = malloc( sizeof( struct stack_record ));
  28. if( s == NULL )
  29. {
  30. printf( "Out of sapce\n" );
  31. exit(1);
  32. }
  33.  
  34. s->array = malloc( sizeof( element_type ) * max_elements );
  35. if( s->array == NULL )
  36. {
  37. printf( "Out of space\n" );
  38. exit(1);
  39. }
  40.  
  41. s->capacity = max_elements;
  42. make_empty( s );
  43.  
  44. return s;
  45. }
  46.  
  47. void
  48. dispose_stack( stack s )
  49. {
  50. if( s != NULL )
  51. {
  52. free( s->array );
  53. free( s );
  54. }
  55. }
  56.  
  57. int
  58. is_empty( stack s )
  59. {
  60. return s->top_of_stack == -1;
  61. }
  62.  
  63. int
  64. is_full( stack s )
  65. {
  66. return s->capacity == s->top_of_stack + 1;
  67. }
  68.  
  69. void
  70. make_empty( stack s )
  71. {
  72. s->top_of_stack = -1;
  73. }
  74.  
  75. void
  76. push( element_type x, stack s )
  77. {
  78. if( is_full( s ) )
  79. {
  80. printf( "Full stack!\n" );
  81. exit(0);
  82. }
  83. else
  84. s->array[++s->top_of_stack] = x;
  85. }
  86.  
  87. element_type
  88. top( stack s )
  89. {
  90. if( !is_empty( s ) )
  91. return s->array[s->top_of_stack];
  92. else
  93. {
  94. printf( "Empty stack\n" );
  95. exit(0);
  96. }
  97.  
  98. return 0;
  99. }
  100.  
  101. void
  102. pop( stack s )
  103. {
  104. if( is_empty( s ) )
  105. {
  106. printf ("Empty stack\n");
  107. exit(0);
  108. }
  109. else
  110. s->top_of_stack--;
  111. }
  112.  
  113. element_type
  114. top_and_pop( stack s )
  115. {
  116. if( !is_empty( s ) )
  117. return s->array[s->top_of_stack--];
  118. else
  119. {
  120. printf( "Empty stack\n" );
  121. exit(0);
  122. }
  123.  
  124. return 0;
  125. }
  1. /* stack_test.c */
  2.  
  3. #include "stack.h"
  4. #include <stdio.h>
  5.  
  6. int
  7. main(void)
  8. {
  9. stack s;
  10. int x;
  11. int y;
  12. int z;
  13.  
  14. s = create_stack(10);
  15.  
  16. push( 1, s );
  17. push( 2, s );
  18. push( 3, s );
  19.  
  20. x = top_and_pop( s );
  21. y = top_and_pop( s );
  22. z = top_and_pop( s );
  23. printf("%d\n%d\n%d\n", x, y, z);
  24.  
  25. dispose_stack( s );
  26. return 0;
  27. }

 

测试结果:

栈的实现实例(C语言)的更多相关文章

  1. Redis:安装、配置、操作和简单代码实例(C语言Client端)

    Redis:安装.配置.操作和简单代码实例(C语言Client端) - hj19870806的专栏 - 博客频道 - CSDN.NET Redis:安装.配置.操作和简单代码实例(C语言Client端 ...

  2. 实例15_C语言绘制万年历

    实例说明:

  3. 数栈运维实例:Oracle数据库运维场景下,智能运维如何落地生根?

    从马车到汽车是为了提升运输效率,而随着时代的发展,如今我们又希望用自动驾驶把驾驶员从开车这项体力劳动中解放出来,增加运行效率,同时也可减少交通事故发生率,这也是企业对于智能运维的诉求. 从人工运维到自 ...

  4. JavaScript中的栈及通过栈操作的实例

    <script> /*栈操作*/ function Stack() { this.dataStore = []; this.top = 0; this.push = push; this. ...

  5. 栈(链式栈)----C语言

    链式栈:就是一种操作受限的单向链表,对单向链表还不了解的可先看一下之前的一篇关于单向链表的随笔,链表(单向链表的建立.删除.插入.打印),理解了单向链表后再来看链式栈就比较轻松了 链式栈的操作一般含有 ...

  6. 栈的一个实例——Dijkstra的双栈算术表达式求值法

    Dijkstra的双栈算术表达式求值法,即是计算算术表达式的值,如表达式(1 + ( (2+3) * (4*5) ) ). 该方法是 使用两个栈分别存储算术表达式的运算符与操作数 忽略左括号 遇到右括 ...

  7. 栈的应用实例——计算后缀表达式

    用户输入一个后缀表达式,程序计算该后缀表达式的值并输出结果: /* postfix_expression.c */ #include "stack.h" #include < ...

  8. 数据结构 - 链栈的实行(C语言)

    数据结构-链栈的实现 1 链栈的定义 现在来看看栈的链式存储结构,简称为链栈. 想想看栈只是栈顶来做插入和删除操作,栈顶放在链表的头部还是尾部呢?由于单链表有头指针,而栈顶指针也是必须的,那干吗不让它 ...

  9. 数据结构 - 顺序栈的实行(C语言)

    数据结构-顺序栈的实现 1 顺序栈的定义 既然栈是线性表的特例,那么栈的顺序存储其实也是线性表顺序存储的简化,我们简称为顺序栈.线性表是用数组来实现的,对于栈这种只能一头插入删除的线性表来说,用数组哪 ...

随机推荐

  1. sass compass问题小结

    1.中文注释编译报错Invalid GBK character的问题,找到ruby sass安装目录,如D:\Ruby22-x64\lib\ruby\gems\2.2.0\gems\sass-3.4. ...

  2. DISQLite3 - A self-contained, embeddable, zero-configuration SQL database engine for Delphi

    DISQLite3 implements a self-contained, embeddable, zero-configuration SQL database engine for Delphi ...

  3. 【微信小程序】微信小程序 文本过长,自动换行的问题

    小程序开发过程出现的问题: 文本过长,以致于在view中显示不全,没有自动换行的问题 解决方法: 在wxss样式文件中添加样式 .font-break { word-break:break-all; ...

  4. DocumentManager在标签位置显示气泡框 z

    关于DevExpress DockManager下的DocumentManager头部标签如何显示气泡框,类似Visual studio那样显示文件的路径,如下图所示,------- 方式很简单,从工 ...

  5. pytest文档22-fixture详细介绍-作为参数传入,error和failed区别

    前言 fixture是pytest的核心功能,也是亮点功能,熟练掌握fixture的使用方法,pytest用起来才会得心应手! fixture简介 fixture的目的是提供一个固定基线,在该基线上测 ...

  6. java自动创建多级目录

    // 创建文件上传路径 public static void mkdir(String path) { File fd = null; try { fd = new File(path); if (! ...

  7. 深度学习材料:从感知机到深度网络A Deep Learning Tutorial: From Perceptrons to Deep Networks

    In recent years, there’s been a resurgence in the field of Artificial Intelligence. It’s spread beyo ...

  8. 【POJ】【2068】Art Gallery

    计算几何/半平面交 裸的半平面交,关于半平面交的入门请看神犇博客:http://blog.csdn.net/accry/article/details/6070621 然而代码我是抄的proverbs ...

  9. windows media server 组件安装后流媒体服务器启动失败

    做好的web应用,去客户现场部署的时候发现流媒体服务器不能启动.(现场服务器系统为windows server2008 R2) 自己测试的时候搭建环境没什么问题.从来没有遇到安装windows med ...

  10. C#读写txt文件的两种方法介绍[转]

    C#读写txt文件的两种方法介绍 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出 ...