栈的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 ...
随机推荐
- Linux常用工具之XFTP、Xshell配置
Xftp是一个基于 MS windows 平台的功能强大的SFTP.FTP文件传输软件.使用了 Xftp 以后,MS windows 用户能安全地在UNIX/Linux 和 Windows PC 之间 ...
- hdu5119 dp
题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5119 题意: 输入T组数据,每组数据包括两个数n和m,接下来n个数,这n个数可以随意取( ...
- 配置.net连接数据库的配置文件
今天调试一个学生信息管理系统时,启动系统之后,登录时老是报错说实例有问题,拿着报错信息去找方法也没遇到能解决问题的,最后怀疑是数据库配置和配置文件不匹配, 发现自己的数据库里并没有SQLEXPRESS ...
- 设置某个ip对mysql服务器有权限,以及mysql定时备份
CREATE USER 'new_username'@'211.68.%.%' IDENTIFIED BY 'password_for_new_username'; GRANT ALL ON * TO ...
- SQL SERVER获取数据库中所有表名 XTYPE类型
SELECT (case when a.colorder=1 then d.name else null end) 表名, a.colorder 字段序号,a.name 字段名, (case whe ...
- ural 2071. Juice Cocktails
2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...
- ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 A. Anagrams
A. Anagrams time limit per test 1 second memory limit per test 512 megabytes input standard input ou ...
- Moscow Pre-Finals Workshop 2016. National Taiwan U Selection
A. As Easy As Possible 每个点往右贪心找最近的点,可以得到一棵树,然后倍增查询即可. 时间复杂度$O((n+m)\log n)$. #include <bits/stdc+ ...
- 【BZOJ3450】Tyvj1952 Easy 期望DP
[BZOJ3450]Tyvj1952 Easy Description 某一天WJMZBMR在打osu~~~但是他太弱逼了,有些地方完全靠运气:(我们来简化一下这个游戏的规则有n次点击要做,成功了就是 ...
- 使用AFNetworking 2.0 请求数据时出现错误 Request failed: unacceptable content-type: text/html 解决方法
使用AFNetworking 2.0 请求数据时出现错误 Request failed: unacceptable content-type: text/html 解决方法 添加一行 manager. ...