栈的实现实例(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 顺序栈的定义 既然栈是线性表的特例,那么栈的顺序存储其实也是线性表顺序存储的简化,我们简称为顺序栈.线性表是用数组来实现的,对于栈这种只能一头插入删除的线性表来说,用数组哪 ...
随机推荐
- 与Win8之磁盘活动时间100%斗争心得
Windows8因人而异地会在使用过程中磁盘活动时间无缘无故提升到100%并且可能出现持续性抽风现象,具体表现为0%瞬间飙升至100%后又回落,或者一直保持在100%导致使用过程卡顿,认真阅读本文有助 ...
- TSC条码打印机C#例程(tsclib.dll调用)
TSC条码打印机C#例程(tsclib.dll调用) //---- program.cs using System;using System.Collections.Generic;using Sy ...
- MVC中CheckBoxList的3种实现方式
比如,当为一个用户设置角色的时候,角色通常以CheckBoxList的形式呈现.用户和角色是多对多关系: using System.Collections.Generic; using System. ...
- python测试开发django-5.模板templates
前言 html是一个静态的语言,里面没法传一些动态参数,也就是一个写死的html页面.如果想实现在一个固定的html样式,传入不同的参数,这就可以用django的模板传参来解决. 模板参数 先在hel ...
- error: conflicting type qualifiers for 'xxxxx'
网上的非常多解释--非常难理解-- 情景描写叙述: 在代码中,写了A\B两个文件,A:是.c文件,B是.h文件和.c文件. 在A中包括了B.h文件. 在B.h文件里声明了A中定义的变量. 这样编译的情 ...
- JobDataMap 不能被序列化如何解决研究中
JobDataMap被用来保存一系列的(序列化的)对象,这些对象在Job执行时可以得到.JobDataMap是Java Map接口的一个实现,而且还增加了一些存储和读取主类型数据的便捷方法. 如果使用 ...
- struts2 select 默认选中
jsp: <s:select list="#{'1':'男','2':'女'}" name="sex"/> action: private Stri ...
- 关于Themleaf学习总结
此篇记录学习Themleaf测试的相关用例: study01 Thymeleaf 的HelloWorld级别的例子 简单介绍Thymeleaf的工作流程 study02 使用spring.thymel ...
- Java操作Mongodb 保存/读取java对象到/从mongodb
从http://central.maven.org/maven2/org/mongodb/mongo-java-driver/选择一个版本进行下载,这里选择的是3.0.0版本,具体下载以下jar包: ...
- OpenCV学习(24) 直方图(1)
直方图是对数据的统计,并将统计结果分布于一系列预定义的槽中.这里的数据不仅仅指的是灰度值,它可以是任何能有效描述图像特征的数据,比如图像梯度等等. 假设有一个矩阵包含一张图像的信息 (灰度值 0-25 ...