一、头文件:

#ifndef _STACK_LINK_H_
#define _STACK_LINK_H_ struct stack_record;
typedef struct stack_record* stack;
typedef int elementType;
struct link_node;
typedef struct link_node node; int IsEmpty(stack s);
int IsFull(stack s);
stack creatStack(int maxElement);
void disposeStack(stack s);
void makeEmpty(stack s);
void pushElement(elementType x,stack s);
elementType Top(stack s);
void popElement(stack s);
elementType topAndpop(stack s); #define EMPTY 0
#define MINISIZE (5) struct stack_record
{
int capacity;
int size;
node *item;
}; struct link_node
{
elementType data;
node *next;
}; #endif

二、c文件:

#include <stdio.h>
#include <stdlib.h>
#include "stack_link.h" #define MAXSIZE 100 int IsEmpty(stack s)
{
return s->size == 0;
} int IsFull(stack s)
{
return s->size == s->capacity;
} stack creatStack(int maxElement)
{
stack s;
s = (stack)malloc(sizeof(struct stack_record));
if(s == NULL)
{
printf("the stack alloca error\n");
exit(-1);
} s->capacity = MAXSIZE;
s->item = NULL;
s->size = 0; return s;
} void disposeStack(stack s)
{
if(s)
{
if(s->size)
makeEmpty(s); free(s);
}
} void makeEmpty(stack s)
{
if(s)
{
while(s->size)
{
popElement(s);
}
}
} void pushElement(elementType x,stack s)
{
if(s)
{
if(IsFull(s))
return; node *ptr = (node *)malloc(sizeof(node));
if(NULL == ptr)
{
printf("the node alloc is error\n");
exit(-2);
} ptr->data = x;
ptr->next = s->item;
s->item = ptr;
s->size++;
}
} elementType Top(stack s)
{
if(s)
{
if(IsEmpty(s))
{
printf("the stack is empty\n");
exit(-3);
}
return s->item->data;
}
} void popElement(stack s)
{
if(IsEmpty(s) )
{
printf("the stack is empty\n");
exit(-4);
} node *ptr = NULL;
ptr = s->item->next;
free(s->item);
s->item = ptr;
s->size--;
} elementType topAndpop(stack s)
{
if(s)
{
if(IsEmpty(s))
{
printf("the stack is empty\n");
exit(-5);
} elementType x;
x = Top(s);
popElement(s);
return x;
}
} int main(int argc,char *argv[])
{
stack s; s = creatStack(10);
int i = 0;
elementType x;
while(++i <= 10)
{
pushElement(i,s);
printf("the stack size is %d\n",s->size);
x = Top(s);
printf("the stack top is %d\n",x);
} while(s->size)
{
x = topAndpop(s);
printf("the top stack is %d\n",x);
sleep(1);
} disposeStack(s); return 0;
}

三、打印输出:

the stack size is 1
the stack top is 1
the stack size is 2
the stack top is 2
the stack size is 3
the stack top is 3
the stack size is 4
the stack top is 4
the stack size is 5
the stack top is 5
the stack size is 6
the stack top is 6
the stack size is 7
the stack top is 7
the stack size is 8
the stack top is 8
the stack size is 9
the stack top is 9
the stack size is 10
the stack top is 10
the top stack is 10
the top stack is 9
the top stack is 8
the top stack is 7
the top stack is 6
the top stack is 5
the top stack is 4
the top stack is 3
the top stack is 2
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. sed 技巧一例:特定位置插入

    通过一例子熟悉 sed 的运用 下面命令是在修改 ~/fs/install/nzos.conf 文件, 并在 env 第一次出现的地方再添加一行 env LXC_EXTRA_PORT=5556 sed ...

  2. Python 技巧

    1.根据路径导入模块 如果想引用指定路径下的某个模块,则需要使用sys.path.append("module_directory") 来把这个路径添加到sys下,这就涉及到Pyt ...

  3. android 多媒体数据库详解

    主要分为几节: 1. Android的媒体文件内部是如何存储的? 2. Andoid的媒体文件如何获取? 3. 在使用媒体文件的一些小技巧. 1. Android的多媒体如何存储的? Android的 ...

  4. CABasicAnimation学习Demo 包含了一些经常使用的动画效果

    个人写的一些样例: // // ViewController.m // CABasicAnimationDemo // // Created by haotian on 14-6-13. // Cop ...

  5. Oracle实用-01:绑定变量

    数据库虽然在学校系统学习过,但是在工作中真正使用起来收获又是不一样的,今天起打算将项目中使用到的技术再分享出来,不以书本的顺序,只从碰到的问题为顺序. 虽然不是纯粹的数据库工程师,但是每个程序员总免不 ...

  6. Hadoop--序列化

    序列化: 对象的序列化用于将一个对象编码成字节流,以及从字节流中重新构建对象. 将一个对象编码成一个字节流称为序列化该对象. 序列化三种主要的用途: 1.作为一种持久化格式. 2.作为一种通信的数据格 ...

  7. TMsgThread, TCommThread -- 在delphi线程中实现消息循环(105篇博客,好多研究消息的文章)

    在delphi线程中实现消息循环 在delphi线程中实现消息循环 Delphi的TThread类使用很方便,但是有时候我们需要在线程类中使用消息循环,delphi没有提供.   花了两天的事件研究了 ...

  8. 8086 CPU 寻址方式

    8086 CPU 寻址方式灵活.有以下几种 idata 表示常量 1.   [ idata ] 用一个常量来表示地址,可用于直接定位内存单元,但是在 MASM中要显实在的说明 ds 段寄存器, 比如 ...

  9. windows系统port监听

    通常情况下.假设想发现全部已经使用的和正在监听的port,我们能够使用netstat命令. netstat并不是一个port扫描工具.假设你想扫描计算机开放了哪些port的话.建议使用本文介绍的方法. ...

  10. 1038. Recover the Smallest Number (30) - 字符串排序

    题目例如以下: Given a collection of number segments, you are supposed to recover the smallest number from ...