c++栈
来源:互联网 发布:网络五大鬼畜歌曲视频 编辑:程序博客网 时间:2024/06/01 17:01
#include<iostream>
using namespace std;
struct Node
{
char data;
Node *next;
};
class Seqstack
{
public:
Seqstack(){top=NULL;}
~Seqstack(){}
void Push(char x);
char Pop();
char GetTop();
int Empty();
void Emptystack();
private:
Node *top;
};
int Seqstack::Empty()//栈是否为空
{
if(top==NULL)
return 1;
else
return 0;
}
char Seqstack::GetTop()//获取栈顶元素
{
if(top!=NULL)
return top->data;
return 0;
}
void Seqstack::Push(char x)//添加节点
{
Node *s;
s=new Node;s->data=x;
s->next=top;top=s;
}
char Seqstack::Pop()//弹出栈顶
{
Node *p;
char x;
x=top->data;p=top;
top=top->next;
delete p;
return x;
}
void Seqstack::Emptystack()//清空栈
{
Node *p;
while(top!=NULL)
{
p=top;
top=top->next;
delete p;
}
}
int main()//主函数
{
Seqstack s;
char m;
char c;
while(cin>>c&&c!='E')
{
if(c=='P')
{
cin>>m;
s.Push(m);
}
if(c=='D')
{
if(!s.Empty())
{
cout<<s.Pop()<<endl;
}
else
{
cout<<"None"<<endl;
}
}
if(c=='Y')
{
if(s.Empty()==1)
{
cout<<"Yes"<<endl;
}
else
cout<<"No"<<endl;
}
if(c=='G')
{
if(!s.Empty())
{
cout<<s.GetTop()<<endl;
}
}
if(c=='T')
{
if(!s.Empty())
{
s.Emptystack();
}
}
}
return 0;
}
using namespace std;
struct Node
{
char data;
Node *next;
};
class Seqstack
{
public:
Seqstack(){top=NULL;}
~Seqstack(){}
void Push(char x);
char Pop();
char GetTop();
int Empty();
void Emptystack();
private:
Node *top;
};
int Seqstack::Empty()//栈是否为空
{
if(top==NULL)
return 1;
else
return 0;
}
char Seqstack::GetTop()//获取栈顶元素
{
if(top!=NULL)
return top->data;
return 0;
}
void Seqstack::Push(char x)//添加节点
{
Node *s;
s=new Node;s->data=x;
s->next=top;top=s;
}
char Seqstack::Pop()//弹出栈顶
{
Node *p;
char x;
x=top->data;p=top;
top=top->next;
delete p;
return x;
}
void Seqstack::Emptystack()//清空栈
{
Node *p;
while(top!=NULL)
{
p=top;
top=top->next;
delete p;
}
}
int main()//主函数
{
Seqstack s;
char m;
char c;
while(cin>>c&&c!='E')
{
if(c=='P')
{
cin>>m;
s.Push(m);
}
if(c=='D')
{
if(!s.Empty())
{
cout<<s.Pop()<<endl;
}
else
{
cout<<"None"<<endl;
}
}
if(c=='Y')
{
if(s.Empty()==1)
{
cout<<"Yes"<<endl;
}
else
cout<<"No"<<endl;
}
if(c=='G')
{
if(!s.Empty())
{
cout<<s.GetTop()<<endl;
}
}
if(c=='T')
{
if(!s.Empty())
{
s.Emptystack();
}
}
}
return 0;
}
阅读全文
0 0
- 栈(C#)
- C/C++:堆与栈
- [C/C++] 函数调用的栈分配
- 堆和栈的区别(C/C++)
- C++/C语言中链表队列与栈
- 【C/C++】堆和栈的区别
- 【C语言/C++】 栈和队列
- 【C/C++】堆和栈的区别
- 两个栈实现一个队列(C/C++)
- 数据结构(栈)c/c++/java实现
- 顺序栈(C#)
- NUnit--C#---栈---单元测试
- 栈(C语言)
- C数据结构 栈
- c实现顺序栈
- 四则运算 c/c++ 栈
- C语言算法:栈
- C语言--堆、栈
- 理解面向对象(二)
- (win32)python3.5 安装scrapy
- 实验二 创建学生成绩-顺序表
- 最近 5 年 133 个 Java 面试问题列表(上)
- JAVA基础-01
- c++栈
- 二维树状数组
- poj1509 后缀自动机模板题
- 【LeetCode】C# 67、Add Binary
- Lonely Planet Thailand 16ed孤独星球泰国2016
- c3p0,dbcp与druid 三大连接池的区别
- Java练习题12 继承
- 第四章 函数
- 时间序列数据存储