《数据结构与算法分析——C语言描述》ADT实现(NO.01) : 栈(Stack)
这次的数据结构是一种特殊的线性表:栈(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)的更多相关文章
- 《数据结构与算法分析——C语言描述》ADT实现(NO.00) : 链表(Linked-List)
开始学习数据结构,使用的教材是机械工业出版社的<数据结构与算法分析——C语言描述>,计划将书中的ADT用C语言实现一遍,记录于此.下面是第一个最简单的结构——链表. 链表(Linked-L ...
- 数据结构与算法分析——C语言描述 第三章的单链表
数据结构与算法分析--C语言描述 第三章的单链表 很基础的东西.走一遍流程.有人说学编程最简单最笨的方法就是把书上的代码敲一遍.这个我是头文件是照抄的..c源文件自己实现. list.h typede ...
- 最小正子序列(序列之和最小,同时满足和值要最小)(数据结构与算法分析——C语言描述第二章习题2.12第二问)
#include "stdio.h" #include "stdlib.h" #define random(x) (rand()%x) void creat_a ...
- C语言学习书籍推荐《数据结构与算法分析:C语言描述(原书第2版)》下载
维斯 (作者), 冯舜玺 (译者) <数据结构与算法分析:C语言描述(原书第2版)>内容简介:书中详细介绍了当前流行的论题和新的变化,讨论了算法设计技巧,并在研究算法的性能.效率以及对运行 ...
- 《数据结构与算法分析-Java语言描述》 分享下载
书籍信息 书名:<数据结构与算法分析-Java语言描述> 原作名:Data Structures and Algorithm Analysis in Java 作者: 韦斯 (Mark A ...
- 读书笔记:《数据结构与算法分析Java语言描述》
目录 第 3 章 表.栈和队列 3.2 表 ADT 3.2.1 表的简单数组实现 3.2.2 简单链表 3.3 Java Collections API 中的表 3.3.1 Collection 接口 ...
- 《数据结构与算法分析——C语言描述》ADT实现(NO.03) : 二叉搜索树/二叉查找树(Binary Search Tree)
二叉搜索树(Binary Search Tree),又名二叉查找树.二叉排序树,是一种简单的二叉树.它的特点是每一个结点的左(右)子树各结点的元素一定小于(大于)该结点的元素.将该树用于查找时,由于二 ...
- 《数据结构与算法分析——C语言描述》ADT实现(NO.05) : 散列(Hash)
散列(Hash)是一种以常数复杂度实现查找功能的数据结构.它将一个关键词Key,通过某种映射(哈希函数)转化成索引值直接定位到相应位置. 实现散列有两个关键,一是哈希函数的选择,二是冲突的处理. 对于 ...
- 《数据结构与算法分析——C语言描述》ADT实现(NO.04) : AVL树(AVL-Tree)
上次我们已经实现了普通的二叉查找树.利用二叉查找树,可以用O(logN)高度的树状结构存储和查找数据,提高了存储和查找的效率. 然而,考虑一种极端情形:依次插入1,2,3,4,5,6,7,8,9九个元 ...
随机推荐
- PAT甲级——A1109 Group Photo【25】
Formation is very important when taking a group photo. Given the rules of forming K rows with Npeopl ...
- 通过JBOSS服务器来实现JMS消息传送
首先必须启动JBOSS服务器,以便于充当JMS传递消息的中间键: JBOSS消息发送端: package test; import java.util.concurrent.CountDownLatc ...
- 使用virtualenv发布Python程序
客户环境不能上网,开始想把所有依赖包下载下来,进入客户环境进行安装.但为了避免出差,部署工作交给其他同事了,我想还是需要更简单的方式. 实验了一下virtualenv是可以的 1. 创建一个新的环境( ...
- CentOS7-Minimal安装MySQL服务
CentOS7默认安装的是Mariadb而不是mysql,而Mariadb是mysql的一个分支, 安装mysql会覆盖Mariadb 一.下载MySQL官方的 Yum Repository [roo ...
- js 禁止/允许页面滚动
参考:https://blog.csdn.net/huangfu_chunfeng/article/details/46429997 https://www.cnblogs.com/w ...
- spark 应用场景1-求年龄平均值
原文引自:http://blog.csdn.net/fengzhimohan/article/details/78535143 该案例中,我们将假设我们需要统计一个 10 万人口的所有人的平均年龄,当 ...
- Ubuntu usb设备端口号绑定
1.将串口设备插入USB口,通过lsusb查看端口信息.例如: ID 1a86:7523 表示usb设备的ID(这个ID由芯片制造商设置,可以唯一表示该设备) 1a86 usb_device_desc ...
- vuex结合vue-cookies的使用
一.创建vuex import Vue from 'vue' import Vuex from 'vuex' import cookie from "vue-cookies" Vu ...
- JS Math.sin() 与 Math.cos() 用法 (含圆上每个点的坐标)
Math.sin(x) x 的正玄值.返回值在 -1.0 到 1.0 之间: Math.cos(x) x 的余弦值.返回的是 -1.0 到 1.0 之间的数: 这两个函数中的X 都是指 ...
- System.Web.Mvc.FileContentResult.cs
ylbtech-System.Web.Mvc.FileContentResult.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, ...