数据结构——栈——C++实现栈及其操作
来源:互联网 发布:淘宝怎样延迟久点收货 编辑:程序博客网 时间:2024/05/22 11:54
C++实现栈的构建和操作:
1:栈的结构体定义
2:置空栈
3:判断是否为空栈
4:进栈
5:出栈
6:显示整个栈元素
切记亲力亲为,动手实践写代码
Stack.h
#define MAXSIZE 100typedef int datatype;typedef struct {datatype data[MAXSIZE];int top; //记录当前栈顶位置 } Stack;//置空栈bool Empty_Stack(Stack &S);//判断栈是否为空bool IsNull(Stack S);//进栈bool PushStack(Stack &S,datatype a);//出栈bool PopStack(Stack &S);//取栈顶元素,不改变栈本身datatype Top_element(Stack S);//显示整个栈bool show(Stack S);
Stack.cpp
#include "Stack.h"#include <iostream>using std::cin;using std::cout;using std::endl;bool Empty_Stack(Stack &S){S.top = -1;return true;}bool IsNull(Stack S){if (S.top >= 0){return true;}return false;}//进栈bool PushStack(Stack &S,datatype a){if (S.top >= MAXSIZE-1) //溢出检查必不可少{cout<<"Stack Overflow~"<<endl;return false;}S.data[S.top+1] = a;S.top += 1;return true;}//出栈bool PopStack(Stack &S){if (S.top <= 0){cout<<"Stack Underflow~"<<endl;return false;}S.top -= 1;return true;}//取栈顶元素,不改变栈本身datatype Top_element(Stack S){if (IsNull(S)){return S.data[S.top];}else{cout<<"Empty Stack~";return NULL;}}//显示整个栈bool show(Stack S){if (IsNull(S)){for (int i = 0;i <= S.top;++i){cout<<S.data[i]<<" ";}return true;}else{cout<<"Empty Stack~";return false;}}
main.cpp
/*************************************************************************** * @file main.cpp * @author MISAYAONE * @date 17 may 2017 * @remark 17 may 2017 * @theme Stack C++ ***************************************************************************/ #include "Stack.h"#include <iostream>using namespace std;int main(int argc,char** argv){Stack S;Empty_Stack(S); //置空栈cout<<"This Stack is Null ?"<<bool(IsNull(S))<<endl; //判断是否为空栈PushStack(S,2); //进栈PushStack(S,3);PushStack(S,4);PushStack(S,2);PushStack(S,8);cout<<"The elements are : ";show(S); //显示整个栈的内容PopStack(S); //出栈PopStack(S);cout<<"After the Pop, the top element is: ";cout<<Top_element(S);system("pause");return 0;}
阅读全文
1 0
- 数据结构——栈——C++实现栈及其操作
- 数据结构算法——单链表及其操作
- 数据结构——矩阵及其常用操作
- 常用数据结构——栈及其应用
- 数据结构与算法—常用数据结构及其Java实现
- 数据结构——树——C++实现树及其操作
- 数据结构——队列——C++实现队列及其操作
- 数据结构——图——C++实现图及其遍历操作
- 数据结构——二叉树及其基本操作
- 数据结构专题——栈与队列之顺序栈及其Java实现
- 数据结构专题——栈与队列之链栈及其Java实现
- C语言实现栈数据结构操作
- C语言------数据结构(栈操作,数组实现)
- 《C算法》——基本数据结构及其算法
- 数据结构栈及其用法(C语言实现)
- c语言实现单链表数据结构及其基本操作
- 数据结构—线性结构—线性及其实现
- 数据结构例程——哈希表及其运算的实现
- c# if elseif else的判断
- 系统启动(grub--centos5)
- Ubuntu下添加开机启动项的
- 百度地图js 自定义背景图片
- SQLAlchemy 插入 性能 速度 比较
- 数据结构——栈——C++实现栈及其操作
- dojo 的DOM操作 dojo/dom
- 数据库范式
- Microsoft IIS FTP Service CVE-2012-2532 Remote Command Injection Vulnerability
- linux下socket通信,server和client简单例子
- L-LSP和E-LSP介绍
- js有哪些内置对象????
- 盒图介绍
- javascript表单(正则表达式)验证大全