这次的数据结构是一种特殊的线性表:栈(Stack)

栈的特点是后入先出(LIFO),可见的只有栈顶的一个元素。

栈在程序中的地位非常重要,其中最重要的应用就是函数的调用。每次函数调用时都会创建该函数的一个“活动记录”( Activation Record ,或称作“帧”( Frame ))压入运行时堆栈中,用于保存函数的参数,返回值,返回指令地址,返回活动记录地址,局部变量等内容。当然,在高级语言中我们不需要手动完成这一工作,学习汇编语言时会体会到其真正过程。

下面给出笔者对于堆栈的两种实现。首先是顺序(数组)存储实现:

// Stack.h

#include <stdio.h>
#include <stdlib.h> struct StackRecord;
typedef struct StackRecord *Stack; int IsEmpty(Stack S);
int IsFull(Stack S);
Stack CreateStack(int MaxElements);
void DisposeStack(Stack S);
void MakeEmpty(Stack S);
void Push(ElementType X, Stack S);
ElementType Top(Stack S);
void Pop(Stack S);
ElementType TopAndPop(Stack S);

  

// Stack.c

#include "Stack.h"

struct StackRecord{
int Capacity;
int TopOfStack;
ElementType *Array;
}; int IsEmpty(Stack S)
{
return S->TopOfStack == -1;
} int IsFull(Stack S)
{
return S->TopOfStack == S->Capacity - 1;
} Stack CreateStack(int MaxElements)
{
Stack ret;
if((ret = (Stack)malloc(sizeof(struct StackRecord))) != NULL)
{
ret->TopOfStack = -1;
ret->Capacity = MaxElements;
if((ret->Array = (ElementType*)malloc(MaxElements * sizeof(ElementType))) != NULL)
return ret;
free(ret);
}
printf("Error! Out of memory! \n");
return NULL;
} void DisposeStack(Stack S)
{
if(S)
{
free(S->Array);
free(S);
}
} void MakeEmpty(Stack S)
{
S->TopOfStack = -1;
} void Push(ElementType X, Stack S)
{
if(IsFull(S))
{
printf("Error! The stack is full! \n");
return;
}
(S->Array)[++(S->TopOfStack)] = X;
} ElementType Top(Stack S)
{
if(IsEmpty(S))
{
printf("Error! The stack is empty! \n");
return 0;
}
return (S->Array)[(S->TopOfStack)];
} void Pop(Stack S)
{
if(IsEmpty(S))
{
printf("Error! The stack is empty! \n");
return;
}
S->TopOfStack -= 1;
} ElementType TopAndPop(Stack S)
{
if(IsEmpty(S))
{
printf("Error! The stack is empty! \n");
return 0;
}
return (S->Array)[(S->TopOfStack)--];
}

  

接下来是链式存储实现:

// Stack.h

#include <stdio.h>
#include <stdlib.h> struct _Node;
typedef struct _Node Node;
typedef Node *PtrToNode;
typedef PtrToNode Stack; int IsEmpty(Stack S);
Stack CreateStack();
void DisposeStack(Stack S);
void MakeEmpty(Stack S);
void Push(ElementType X, Stack S);
ElementType Top(Stack S);
void Pop(Stack S);

  

// Stack.c

#include "Stack.h"

struct _Node
{
ElementType Element;
struct _Node *Next;
}; void print(Stack S)
{
for (PtrToNode p = S->Next; p; p = p->Next)
{
printf("%d ", p->Element);
}
printf("\n");
} int main()
{
Stack s = CreateStack();
print(s);
Pop(s);
printf("%d", Top(s));
Push(2, s);
Push(3, s);
Push(5, s);
print(s);
Pop(s);
print(s);
MakeEmpty(s);
print(s);
DisposeStack(s);
getchar();
getchar();
} int IsEmpty(Stack S)
{
return S->Next == NULL;
} Stack CreateStack()
{
Stack ret;
if ((ret = (Stack)malloc(sizeof(Node))) != NULL)
{
ret->Next = NULL;
return ret;
}
printf("Error! Out of memory! \n");
return 0;
} void DisposeStack(Stack S)
{
PtrToNode p = S;
while (S != NULL)
{
S = S->Next;
free(p);
p = S;
}
} void MakeEmpty(Stack S)
{
if (S == NULL)
{
printf("Error! It's not a Stack!\n");
return;
}
while (!IsEmpty(S))
Pop(S);
} void Push(ElementType X, Stack S)
{
PtrToNode t;
if ((t = (PtrToNode)malloc(sizeof(Node))) != NULL)
{
t->Element = X;
t->Next = S->Next;
S->Next = t;
return;
}
printf("Error! Out of memory! \n");
} ElementType Top(Stack S)
{
if (S->Next == NULL)
{
printf("Error! The stack is empty! \n");
return 0;
}
return S->Next->Element;
} void Pop(Stack S)
{
PtrToNode p = S->Next;
if (p == NULL)
{
printf("Error! The stack is empty! \n");
return;
}
S->Next = p->Next;
free(p);
}

 以上就是堆栈的两种实现。由于较为简单,不再给出测试样例。

《数据结构与算法分析——C语言描述》ADT实现(NO.01) : 栈(Stack)的更多相关文章

  1. 《数据结构与算法分析——C语言描述》ADT实现(NO.00) : 链表(Linked-List)

    开始学习数据结构,使用的教材是机械工业出版社的<数据结构与算法分析——C语言描述>,计划将书中的ADT用C语言实现一遍,记录于此.下面是第一个最简单的结构——链表. 链表(Linked-L ...

  2. 数据结构与算法分析——C语言描述 第三章的单链表

    数据结构与算法分析--C语言描述 第三章的单链表 很基础的东西.走一遍流程.有人说学编程最简单最笨的方法就是把书上的代码敲一遍.这个我是头文件是照抄的..c源文件自己实现. list.h typede ...

  3. 最小正子序列(序列之和最小,同时满足和值要最小)(数据结构与算法分析——C语言描述第二章习题2.12第二问)

    #include "stdio.h" #include "stdlib.h" #define random(x) (rand()%x) void creat_a ...

  4. C语言学习书籍推荐《数据结构与算法分析:C语言描述(原书第2版)》下载

    维斯 (作者), 冯舜玺 (译者) <数据结构与算法分析:C语言描述(原书第2版)>内容简介:书中详细介绍了当前流行的论题和新的变化,讨论了算法设计技巧,并在研究算法的性能.效率以及对运行 ...

  5. 《数据结构与算法分析-Java语言描述》 分享下载

    书籍信息 书名:<数据结构与算法分析-Java语言描述> 原作名:Data Structures and Algorithm Analysis in Java 作者: 韦斯 (Mark A ...

  6. 读书笔记:《数据结构与算法分析Java语言描述》

    目录 第 3 章 表.栈和队列 3.2 表 ADT 3.2.1 表的简单数组实现 3.2.2 简单链表 3.3 Java Collections API 中的表 3.3.1 Collection 接口 ...

  7. 《数据结构与算法分析——C语言描述》ADT实现(NO.03) : 二叉搜索树/二叉查找树(Binary Search Tree)

    二叉搜索树(Binary Search Tree),又名二叉查找树.二叉排序树,是一种简单的二叉树.它的特点是每一个结点的左(右)子树各结点的元素一定小于(大于)该结点的元素.将该树用于查找时,由于二 ...

  8. 《数据结构与算法分析——C语言描述》ADT实现(NO.05) : 散列(Hash)

    散列(Hash)是一种以常数复杂度实现查找功能的数据结构.它将一个关键词Key,通过某种映射(哈希函数)转化成索引值直接定位到相应位置. 实现散列有两个关键,一是哈希函数的选择,二是冲突的处理. 对于 ...

  9. 《数据结构与算法分析——C语言描述》ADT实现(NO.04) : AVL树(AVL-Tree)

    上次我们已经实现了普通的二叉查找树.利用二叉查找树,可以用O(logN)高度的树状结构存储和查找数据,提高了存储和查找的效率. 然而,考虑一种极端情形:依次插入1,2,3,4,5,6,7,8,9九个元 ...

随机推荐

  1. Python 迭代器与生成器及装饰器

    1.迭代器(Iterator) 迭代器是访问集合元素的一种方式.有下面特点: 1)每次调用__next__()方法只访问一个元素,而且不能后退,便于循环比较大的数据集合,节省内存:(当容器中没有可访问 ...

  2. npm使用入门

    NPM使用入门 npm 就是node package manager node的包管理工具 我们通过npm install 模块 来安装模块,缩写:npm i 模块,注意,低版本的node可能需要np ...

  3. HTTP协议响应篇

    http响应的基本介绍 一个HTTP响应代表服务器向客户端回送的数据, 由三个部分构成 状态行[200 , 302 304, 403, 404, 500] 响应消息头 返回的实体内容 http响应状态 ...

  4. shell脚本练习04

    ######################################################################### # File Name: -.sh # Author ...

  5. Error:Execution failed for task ':app:validateSigningDebug'.

    今天出差回来 第一天   把项目重新移植到新的电脑上 一运行 给我报错了 ! 这个是签名的路径错误  我们需要重新导一下路径就可以了 点击左上角 File ->  project structu ...

  6. 读《深入PHP 面向对象、模式与实践》笔记

    1. include() 和require() 语句的不同在于它们如何处理错误.使用require()调用文件发生错误时,将会停止整个程序;调用include()时遇到相同的错误,则会生成警告并停止执 ...

  7. ie中onclick问题

    代码:<button > <span onclick="xxx();">确定</span></button> 在chrome和fir ...

  8. task code

    using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks ...

  9. MyEclipse如何使用debug模式

    知道如何打断点,如何进入debug与debug模式的视图,还有工具栏的使用和快捷键的使用 https://blog.csdn.net/menglanyingfei/article/details/55 ...

  10. Python-基本运算符与流程控制

    目录 基本运算符 算术运算符 比较运算符 赋值运算符 逻辑运算符 身份运算符 位运算符 成员运算符 运算符优先级 流程控制 if 判断 单分支结构 双分支结构 多分支结构 while 循环 while ...