/* 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语言)的更多相关文章

  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. Mac OSX系统下通过ProxyChains-NG实现终端下的代理

    项目主页:https://github.com/rofl0r/proxychains-ng 官方说明: proxychains ng (new generation) - a preloader wh ...

  2. linux 文件处理大杂烩

    1.对文件某行进行统计排序 awk  '{ printf "%-40s \n",$4}' /var/log/yum.log | sort | uniq -c | sort -nk ...

  3. 【scrapy】使用方法概要(一)(转)

    [请初学者作为参考,不建议高手看这个浪费时间] 工作中经常会有这种需求,需要抓取互联网上的数据.笔者就经常遇到这种需求,一般情况下会临时写个抓取程序,但是每次遇到这种需求的时候,都几乎要重头写,特别是 ...

  4. HDU 2089 不要62(数位DP&#183;记忆化搜索)

    题意  中文 最基础的数位DP  这题好像也能够直接暴力来做   令dp[i][j]表示以 j 开头的 i 位数有多少个满足条件 那么非常easy有状态转移方程 dp[i][j] = sum{ dp[ ...

  5. 用Qemu模拟vexpress-a9 (一) --- 搭建Linux kernel调试环境

    参考: http://blog.csdn.net/linyt/article/details/42504975 环境介绍: Win7 64 + Vmware 11 + ubuntu14.04 32 u ...

  6. MVC动态添加文本框,后台使用FormCollection接收

    在"MVC批量添加,增加一条记录的同时添加N条集合属性所对应的个体"中,对于前台传来的多个TextBox值,在控制器方法中通过强类型来接收.使用FormCollection也可以接 ...

  7. 将数据处理逻辑集中到一处进行管理,逐步实现真正有效的 MVC 分层结构

    将数据处理逻辑集中到一处进行管理,逐步实现真正有效的 MVC 分层结构.

  8. python文本 maketrans和translate

    python文本 maketrans和translate 场景: 过滤字符串的某些字符,我们从例子出发 >>> tb=str.maketrans ('abc','123')    & ...

  9. 新浪行情 vb代码

    Sub 新浪行情() Cells.Clear Dim n As Integer, Js As Object Dim i As Integer, j As Integer, m As Integer, ...

  10. 分析oracle索引空间使用情况,以及索引是否须要重建

    分析索引空间使用情况.以及索引是否须要重建 分析其它用户下的索引须要 analyze any的权限 分析索引前先查看表的大小和索引的大小,假设索引大小和表大小一样大或者大于表的大小,那么能够推断索引可 ...