栈的顺序存储实现
来源:互联网 发布:js 精确计时 编辑:程序博客网 时间:2024/03/29 17:52
#include<iostream>#include<stdlib.h>using namespace std;#define STACK_SIZE 100struct Stack{ int *top; int *base; int Size;}Stack;/********************** * Function Statements**********************/bool InitStack(struct Stack &S); //构建栈bool IsEmpty(struct Stack &S); //判断栈空bool IsFull(struct Stack &S); //判断栈满void Push(struct Stack &S, int e); //压栈,插入数据void Pop(struct Stack &S, int &e); //删除,即弹出数据bool DestroyStack(struct Stack &S); //销毁栈bool ClearStack(struct Stack &S); //清空栈void PrintStack(struct Stack &S); //打印栈中元素,即输出/********************** * Main ************************/int main(){ struct Stack S; int b, n; if(InitStack(S)==false){ cout << "Init Stack error!" << endl; exit(0); } cout << "push a data into stack:" << endl; cin >> n; //压栈 cout << "压栈操作" << endl; while(n--){ Push(S, n); PrintStack(S); } //弹栈 cout << "弹栈操作" << endl; while(n<4){ Pop(S, b); PrintStack(S); n++; } //清栈 cout << "清栈操作" << endl; if(ClearStack(S)) PrintStack(S); //销毁栈 cout << "销毁栈操作" << endl; DestroyStack(S); cout << "S.base = " << S.base << "; " <<"S.top = " << S.top << "; " << "S.Size = " << S.Size << endl; return 0;}/******************************* * PrintStack * 打印栈中的元素,自顶向下显示 * 即从栈尾输出到栈首*******************************/void PrintStack(struct Stack &S){ int n = S.top - S.base; int index; //若栈空则输出 if(!n){ cout << ".................................." << endl; cout << "Stack is Empty..."<<endl; cout << ".................................." << endl; return; } //非空时 cout << ".................................." << endl; cout << "栈中的数据(从栈顶元素开始向下显示)" << endl; for(index = 1; index<=n; ++index){ cout << *(S.top - index) << " "; } cout << endl;}/******************************* *InitStack *动态分配内存 *并对栈的属性初始化*******************************/bool InitStack(struct Stack &S){ S.base = (int* ) malloc (STACK_SIZE *sizeof(Stack)); if(S.base == NULL){ cout << "malloc stack error" << endl; return false; } S.top = S.base; S.Size = STACK_SIZE; return true;}/******************************* *ClearStack *清空栈内数据*******************************/bool ClearStack(struct Stack &S){ S.top = S.base; return true;}/******************************* *DestroyStack *销毁栈S,释放内存 *并将指针属性置空 *防止悬浮指针产生*******************************/bool DestroyStack(struct Stack &S){ free(S.base); S.base = NULL; //不用的指针一定用指向空 S.top = NULL; //防止悬浮指针的产生 S.Size = 0; return true;}/******************************* *IsEmpty *判断栈是否为空 *即判断S.top == S.base ? *若S.top == S.base 则表示栈空*******************************/bool IsEmpty(struct Stack &S){ if(S.top == S.base) return true; else return false;}/****************************************** *IsFull *判断栈是否存满 *A = S.top - S.base 代表此时栈中所有元素 *B = S.Size 表示栈被分配的空间 *若A >= B, 则表示栈溢出******************************************/bool IsFull(struct Stack &S){ int A = S.top - S.base; int B = S.Size; if(A >= B){ cout << "Stack is Full" << endl; return true; } else return false;}/************************************************************ *Push *压栈操作 *栈是特殊的线性表,其不支持随机访问,因此只能按照顺序存储。 *例如:子弹夹上子弹(该例子与本实现不同的是 *++子弹夹栈顶指针保持不变,栈底指针游动。 *++本程序实现的是栈底指针不变,栈顶指针游动。)*************************************************************/void Push(struct Stack &S, int e){ if(!IsFull(S)){ *(S.top) = e; S.top ++; }}/****************************************************************** * Pop * 弹栈操作 * 栈是特殊的线性表,其不支持随机访问,因此只能按照顺序存储。 * 例如:子弹夹推出子弹(该例子与本实现不同的是 * ++子弹夹栈顶指针保持不变,栈底指针游动。 * ++本程序实现的是栈底指针不变,栈顶指针游动。)*******************************************************************/void Pop(struct Stack &S, int &e){ if(IsFull(S)){ cout << "Pop error" << endl; } else{ e = *(S.top - 1); S.top --; }}
点击打开链接
0 0
- 栈的顺序存储实现
- 栈的顺序存储实现
- 栈的顺序存储实现
- 栈的顺序存储实现
- 栈的顺序存储实现
- 栈的顺序存储实现
- 栈的顺序存储实现
- 栈的顺序存储结构的实现
- 栈的顺序存储的实现代码
- 栈的顺序存储的代码实现
- 顺序栈存储实现
- 顺序存储实现栈
- 栈的顺序存储实现和链式存储实现
- 栈的顺序存储结构---顺序栈的实现
- 栈的顺序存储结构实现
- 数据结构:栈的顺序存储实现
- 栈的顺序存储结构和实现
- 线性结构--->栈的顺序存储实现
- Mongo数据库的基本指令
- Java中的泛型的初遇
- 高性能WEB开发 nginx HTTP服务器篇
- 在HAl层里创建线程
- android 框架包含的view的类图
- 栈的顺序存储实现
- QML元素
- 数据结构与算法(个人总结)
- hdoj 2203 亲和串
- meachine learning
- Sqoop1.99.3安装(Hadoop 2.4.1版本)
- hdu 4897 Little Devil I(树链剖分+线段树)
- linux grep命令
- Android知识笔记