栈的实现实例(C语言)
- /* stack.h */
- #ifndef _stack_h
- #define _stack_h
- struct stack_record;
- typedef struct stack_record *stack;
- typedef int element_type;
- int is_empty( stack s );
- int is_full( stack s );
- stack create_stack( int max_elements );
- void dispose_stack( stack s );
- void make_empty( stack s );
- void push( element_type x, stack s );
- element_type top( stack s );
- void pop( stack s );
- element_type top_and_pop( stack s );
- #endif
- /* stack.c */
- #include "stack.h"
- #include <stdio.h>
- #include <stdlib.h>
- #define MIN_STACK_SIZE 5
- struct stack_record
- {
- int capacity;
- int top_of_stack;
- element_type *array;
- };
- stack
- create_stack( int max_elements )
- {
- stack s;
- if( max_elements < MIN_STACK_SIZE )
- {
- printf( "Stack size is too small\n" );
- exit(0);
- }
- s = malloc( sizeof( struct stack_record ));
- if( s == NULL )
- {
- printf( "Out of sapce\n" );
- exit(1);
- }
- s->array = malloc( sizeof( element_type ) * max_elements );
- if( s->array == NULL )
- {
- printf( "Out of space\n" );
- exit(1);
- }
- s->capacity = max_elements;
- make_empty( s );
- return s;
- }
- void
- dispose_stack( stack s )
- {
- if( s != NULL )
- {
- free( s->array );
- free( s );
- }
- }
- int
- is_empty( stack s )
- {
- return s->top_of_stack == -1;
- }
- int
- is_full( stack s )
- {
- return s->capacity == s->top_of_stack + 1;
- }
- void
- make_empty( stack s )
- {
- s->top_of_stack = -1;
- }
- void
- push( element_type x, stack s )
- {
- if( is_full( s ) )
- {
- printf( "Full stack!\n" );
- exit(0);
- }
- else
- s->array[++s->top_of_stack] = x;
- }
- element_type
- top( stack s )
- {
- if( !is_empty( s ) )
- return s->array[s->top_of_stack];
- else
- {
- printf( "Empty stack\n" );
- exit(0);
- }
- return 0;
- }
- void
- pop( stack s )
- {
- if( is_empty( s ) )
- {
- printf ("Empty stack\n");
- exit(0);
- }
- else
- s->top_of_stack--;
- }
- element_type
- top_and_pop( stack s )
- {
- if( !is_empty( s ) )
- return s->array[s->top_of_stack--];
- else
- {
- printf( "Empty stack\n" );
- exit(0);
- }
- return 0;
- }
- /* stack_test.c */
- #include "stack.h"
- #include <stdio.h>
- int
- main(void)
- {
- stack s;
- int x;
- int y;
- int z;
- s = create_stack(10);
- push( 1, s );
- push( 2, s );
- push( 3, s );
- x = top_and_pop( s );
- y = top_and_pop( s );
- z = top_and_pop( s );
- printf("%d\n%d\n%d\n", x, y, z);
- dispose_stack( s );
- return 0;
- }
测试结果:
栈的实现实例(C语言)的更多相关文章
- Redis:安装、配置、操作和简单代码实例(C语言Client端)
Redis:安装.配置.操作和简单代码实例(C语言Client端) - hj19870806的专栏 - 博客频道 - CSDN.NET Redis:安装.配置.操作和简单代码实例(C语言Client端 ...
- 实例15_C语言绘制万年历
实例说明:
- 数栈运维实例:Oracle数据库运维场景下,智能运维如何落地生根?
从马车到汽车是为了提升运输效率,而随着时代的发展,如今我们又希望用自动驾驶把驾驶员从开车这项体力劳动中解放出来,增加运行效率,同时也可减少交通事故发生率,这也是企业对于智能运维的诉求. 从人工运维到自 ...
- JavaScript中的栈及通过栈操作的实例
<script> /*栈操作*/ function Stack() { this.dataStore = []; this.top = 0; this.push = push; this. ...
- 栈(链式栈)----C语言
链式栈:就是一种操作受限的单向链表,对单向链表还不了解的可先看一下之前的一篇关于单向链表的随笔,链表(单向链表的建立.删除.插入.打印),理解了单向链表后再来看链式栈就比较轻松了 链式栈的操作一般含有 ...
- 栈的一个实例——Dijkstra的双栈算术表达式求值法
Dijkstra的双栈算术表达式求值法,即是计算算术表达式的值,如表达式(1 + ( (2+3) * (4*5) ) ). 该方法是 使用两个栈分别存储算术表达式的运算符与操作数 忽略左括号 遇到右括 ...
- 栈的应用实例——计算后缀表达式
用户输入一个后缀表达式,程序计算该后缀表达式的值并输出结果: /* postfix_expression.c */ #include "stack.h" #include < ...
- 数据结构 - 链栈的实行(C语言)
数据结构-链栈的实现 1 链栈的定义 现在来看看栈的链式存储结构,简称为链栈. 想想看栈只是栈顶来做插入和删除操作,栈顶放在链表的头部还是尾部呢?由于单链表有头指针,而栈顶指针也是必须的,那干吗不让它 ...
- 数据结构 - 顺序栈的实行(C语言)
数据结构-顺序栈的实现 1 顺序栈的定义 既然栈是线性表的特例,那么栈的顺序存储其实也是线性表顺序存储的简化,我们简称为顺序栈.线性表是用数组来实现的,对于栈这种只能一头插入删除的线性表来说,用数组哪 ...
随机推荐
- sass compass问题小结
1.中文注释编译报错Invalid GBK character的问题,找到ruby sass安装目录,如D:\Ruby22-x64\lib\ruby\gems\2.2.0\gems\sass-3.4. ...
- 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 ...
- 【微信小程序】微信小程序 文本过长,自动换行的问题
小程序开发过程出现的问题: 文本过长,以致于在view中显示不全,没有自动换行的问题 解决方法: 在wxss样式文件中添加样式 .font-break { word-break:break-all; ...
- DocumentManager在标签位置显示气泡框 z
关于DevExpress DockManager下的DocumentManager头部标签如何显示气泡框,类似Visual studio那样显示文件的路径,如下图所示,------- 方式很简单,从工 ...
- pytest文档22-fixture详细介绍-作为参数传入,error和failed区别
前言 fixture是pytest的核心功能,也是亮点功能,熟练掌握fixture的使用方法,pytest用起来才会得心应手! fixture简介 fixture的目的是提供一个固定基线,在该基线上测 ...
- java自动创建多级目录
// 创建文件上传路径 public static void mkdir(String path) { File fd = null; try { fd = new File(path); if (! ...
- 深度学习材料:从感知机到深度网络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 ...
- 【POJ】【2068】Art Gallery
计算几何/半平面交 裸的半平面交,关于半平面交的入门请看神犇博客:http://blog.csdn.net/accry/article/details/6070621 然而代码我是抄的proverbs ...
- windows media server 组件安装后流媒体服务器启动失败
做好的web应用,去客户现场部署的时候发现流媒体服务器不能启动.(现场服务器系统为windows server2008 R2) 自己测试的时候搭建环境没什么问题.从来没有遇到安装windows med ...
- C#读写txt文件的两种方法介绍[转]
C#读写txt文件的两种方法介绍 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出 ...