栈的C++实现(指针)——创建-push-pop-top-清空栈-处理栈
今天学习了栈的C++实现,跟单链表很像:
push相当于单链表在第一个位置插入元素;
pop相当于单链表在第一个位置删除元素;
1、空栈检查
int stack::isEmpty(Stack *S)
{
return S->next == NULL;
}
2、创建一个空栈
stack::Stack *stack::createStack()
{
Stack *S;
S = (Stack*)new(Stack);
//栈空间满后,溢出
if (S == NULL)
cout << "Out of space! " << '\n';
S->next = NULL;
makeEmpty(S);
return S;
}
空栈只有头结点,第9行表示若不为空栈则删除除头结点以外的所有结点。
3、清空栈(保留头结点)
void stack::makeEmpty(Stack *S)
{
if (isEmpty(S))
cout << "Donnot need to makeEmpty!" << '\n';
else
while (!isEmpty(S))
pop(S);
}
4、push操作
stack::Stack *stack::push(int x, Stack *S)
{
Stack *tem;
tem = (Stack*)new(Stack);
if (tem == NULL)
{
cout << "Out of space! " << '\n';
}
else
{
cout << "please input the data to push: " << '\n'; scanf_s("%d",&x); tem->Data = x;
tem->next = S->next;
S->next = tem;
return S;
}
}
5、top操作
int stack::top(Stack *S)
{
if (isEmpty(S))
{
cout << "Empty stack! " << '\n';
return -;
}
else
return S->next->Data;
}
6、pop操作(释放第一个结点后,显示该结点的数据元素)
stack::Stack *stack::pop(Stack *S)
{
Stack *p;
p = NULL;
if (isEmpty(S))
cout << "Empty stack! " << '\n';
else
{
p = S->next;
cout << "the Data be poped is : " << p->Data << endl;
S->next = p->next;
free(p);
return S;
}
}
7、处理栈(删除包括头结点)
void stack::disposeStack(Stack *S)
{
if (S == NULL)
cout << "Donnot need to disposeStack! " << '\n';
while (!isEmpty(S))
pop(S);
free(S);
}
8、主函数
int main()
{
cout << '\n' << "***************************************" << '\n' << '\n';
cout << "Welcome to the stack world! " << '\n';
cout << '\n' << "***************************************" << '\n' << '\n'; int i = ;
int j = ;
int topElement = ;
stack *a = new stack;
stack::Stack *S = NULL;
int x = ;
while (i)
{
cout << '\n' << "***************************************" << '\n';
cout << " 0 : end the stack " << '\n';
cout << " 1 : creat a stack " << '\n';
cout << " 2 : display the top element of stack " << '\n';
cout << " 3 : push a node in the stack " << '\n';
cout << " 4 : pop a node from the stack " << '\n';
cout << "***************************************" << '\n';
cout << "Please input the function your want with the number above : " << '\n';
scanf_s("%d", &j); switch (j)
{
case :
cout << "CreatStack now begin : ";
S = a->createStack();
break;
case :
topElement = a->top(S);
cout << "The top element of stack is : " << topElement;
break;
case :
cout << "push now begin : ";
S = a->push(x, S);
break;
case :
cout << "pop now begin : ";
S = a->pop(S);
break;
default:
cout << "End the stack. ";
a->disposeStack(S);
i = ;
break;
} }
}
运行结果:






栈的C++实现(指针)——创建-push-pop-top-清空栈-处理栈的更多相关文章
- 29. 栈的push,pop序列
题目:给定2个整数序列,其中1个是栈的push顺序,判断另一个有没有可能是对应的pop顺序 解:其实这题主要是判断进栈次数和出栈次数誓不是相等.我是用栈作的,效率不高,每一个元素最多出栈1次,进栈1此 ...
- 数据结构---设计一个栈,push, pop, min 时间复杂度都是 O(1)
普通的栈,push, pop 操作的复杂度是 O(1), 但是如果要找出其中的最小值,则需要 O(N)的时间. 题目要求 min 复杂度也是 O(1), 做法便是 空间换时间,每一步栈的最小值都用一个 ...
- php实现栈操作(不用push pop 库函数)
直接上代码 <?php /*php不用库函数实现栈操作 * @author Geyaru 2019-04-20 */ class stack{ private $top = -1; //栈指针初 ...
- 汇编 push ,pop指令
知识点: PUSH POP CALL堆栈平衡 RETN指令 一.PUSH入栈指令 (压栈指令): 格式: PUSH 操作数 //sub esp,4 ;mov [esp],EBP 操作数 ...
- 用OC实现一个栈:结合单链表创建动态栈
一.介绍 栈是一种数据存储结构,存储的数据具有先进后出的特点.栈一般分为动态栈和静态栈. 静态栈比较好理解,例如用数组实现的栈.动态栈可以用链表来实现. 方式:固定base指针,每次更改top指向入栈 ...
- js中常用数组方法concat join push pop slice splice shift
javascript给我们很多常用的 数组方法,极大方便了我们做程序.下面我们来介绍下常用的集中数组方法. 比如 concat() join() push() pop() unshift() shif ...
- js 的数组怎么push一个对象. Js数组的操作push,pop,shift,unshift JavaScrip
push()函数用于向当前数组的添加一个或多个元素,并返回新的数组长度.新的元素将会依次添加到数组的末尾. 该函数属于Array对象,所有主流浏览器均支持该函数. 语法 array.push( ite ...
- 【PAT甲级】1051 Pop Sequence (25 分)(栈的模拟)
题意: 输入三个正整数M,N,K(<=1000),分别代表栈的容量,序列长度和输入序列的组数.接着输入K组出栈序列,输出是否可能以该序列的顺序出栈.数字1~N按照顺序随机入栈(入栈时机随机,未知 ...
- 1051 Pop Sequence (25分)栈
刷题 题意:栈的容量是5,从1~7这7个数字,写5个测试数据 做法:模拟栈 #include<bits/stdc++.h> using namespace std; const int m ...
随机推荐
- Liferay 6.2 改造系列之十八:修改登录Portlet配置,去除无用链接
在/portal-master/portal-impl/src/portal.properties文件中,有如下配置: # # Input a list of sections that will b ...
- Linux环境变量设置指南
以配置java环境变量为例 目录 [隐藏] 1 修改/etc/profile文件 2 修改用户目录下的.bash_profile 3 修改.bashrc文件 4 直接在shell下设置 5 查看环境 ...
- 去掉SrollView、GrdiView、ListView、ViewPager等滑动到边缘的光晕效果
当我们使用SrollView.GrdiView.ListView.ViewPager带有滑动功能的组件时,滑动到边缘时总会出现类光晕效果.这是用于提示用户已经滑动到了组件的边缘,不能再滑动了,但有时候 ...
- HTML5 重要标签以及属性学习
1.一个标签可以有多个,class=“A B C ” 效果: 2.padding的扩展:当padding的值是正的时候,元素显示的大小会变大:当padding的值是负的时候,元素显示的大小会变小 pa ...
- Codeforces Round #375 (Div. 2) - C
题目链接:http://codeforces.com/contest/723/problem/C 题意:给定长度为n的一个序列.还有一个m.现在可以改变序列的一些数.使得序列里面数字[1,m]出现次数 ...
- maven junit 单元测试插件配置
单元测试插件配置 pom.xml中增加 <dependency> <groupId>junit</groupId> <artifactId>junit& ...
- 封装原生Ajax
var Chef = { createAjax:function() { var xhr = null; try { //IE系列浏览器 xhr = new ActiveXObject("m ...
- PHP、Java对称加密中的AES加密方法
PHP AES加密 <?php ini_set('default_charset','utf-8'); class AES{ public $iv = null; public $key = n ...
- 如何让Ue4画面产生振动效果
可以使用CameraShake蓝图类,对应的C++类为UCameraShake. 这个类是通过修改PlayerController来达到效果
- XIII Open Cup named after E.V. Pankratiev. GP of America
A. Explosions 注意到将炸弹按坐标排序后,每个炸弹直接引爆和间接引爆的都是连续的一段区间,因此只需要求出每个炸弹能间接炸到的最左和最右的炸弹即可. 建立图论模型,炸弹$i$向炸弹$j$连单 ...