当前位置:网站首页>Chapter 3 stack and queue
Chapter 3 stack and queue
2022-06-28 09:57:00 【Zhong Zhongzhong】
Implementation of sequential stack
#include <bits/stdc++.h>
using namespace std;
const int N=1e4+5;
class Seqstack
{
public:
Seqstack();
~Seqstack(){
}
void Push(int k);
int Pop();
bool Empty();
int Gettop();
private:
int data[N];
int top;
};
Seqstack::Seqstack()
{
top=-1;
}
void Seqstack::Push(int k)
{
if(top==N)
{
cout<<" Overflow "<<endl;return;
}
data[++top]=k;
}
int Seqstack::Pop()
{
if(top==-1)
return -1;
return data[top--];
}
int Seqstack::Gettop()
{
if(top==-1)
return -1;
return data[top];
}
bool Seqstack::Empty()
{
if(top==-1)
return 1;
return 0;
}
int main()
{
Seqstack s=Seqstack();
s.Push(1);
s.Push(2);
cout<<s.Pop()<<endl;
cout<<s.Gettop()<<endl;
if(s.Empty()!=1)
cout<<" Non empty "<<endl;
else
cout<<" empty "<<endl;
return 0;
}
The realization of chain stack
#include <bits/stdc++.h>
using namespace std;
const int N=1e4+5;
struct Node
{
int x;
Node *nxt;
};
class Linkstack
{
public:
Linkstack();
~Linkstack();
void Push(int k);
int Pop();
bool Empty();
int Gettop();
private:
Node *top;
};
Linkstack::Linkstack()
{
top=new Node;
top->nxt=NULL;
}
Linkstack::~Linkstack()
{
Node *p=top;
while(top!=NULL)
{
top=top->nxt;
delete p;
p=top;
}
}
void Linkstack::Push(int k)
{
Node* s=new Node;
s->x=k;
s->nxt=top;
top=s;
}
int Linkstack::Pop()
{
if(top->nxt==NULL)
return -1;
int x=top->x;
Node *p=top;
top=top->nxt;
delete p;
return x;
}
int Linkstack::Gettop()
{
return top->x;
}
bool Linkstack::Empty()
{
if(top->nxt==NULL)
return 1;
return 0;
}
int main()
{
Linkstack ls=Linkstack();
if(ls.Empty()!=1)
cout<<" Non empty "<<endl;
else
cout<<" empty "<<endl;
ls.Push(1);
ls.Push(22);
cout<<ls.Pop()<<endl;
cout<<ls.Gettop()<<endl;
ls.Pop();
if(ls.Empty()!=1)
cout<<" Non empty "<<endl;
else
cout<<" empty "<<endl;
return 0;
}
Implementation of circular queue
#include <bits/stdc++.h>
using namespace std;
const int N=1e4+5;
class Cirqueue
{
public:
Cirqueue();
~Cirqueue(){
};
void en_que(int k);
int de_que();
int Gethead();
bool Empty();
public:
int data[N];
int fr,re;
};
Cirqueue::Cirqueue()
{
fr=re=N-1;
}
void Cirqueue::en_que(int k)
{
if((re+1)%N==fr)
{
cout<<" Overflow "<<endl;return ;
}
re=(re+1)%N;
data[re]=k;
}
int Cirqueue::de_que()
{
if(re==fr)
{
cout<<" underflow "<<endl;return 0;
}
fr=(fr+1)%N;
return data[fr];
}
int Cirqueue::Gethead()
{
return data[(fr+1)%N];
}
bool Cirqueue::Empty()
{
if(fr==re)
return 1;
else
return 0;
}
int main()
{
Cirqueue cq=Cirqueue();
cq.en_que(1);
cq.en_que(22);
cq.en_que(33);
cout<<cq.de_que()<<endl;
cout<<cq.de_que()<<endl;
cout<<cq.Gethead()<<endl;
cq.de_que();
cout<<cq.Empty()<<endl;
return 0;
}
Storage implementation of chain queue
边栏推荐
- Huawei OSPF single region
- PMP考试重点总结八——监控过程组(2)
- Flip CEP skip policy aftermatchskipstrategy Skippastlastevent() matched no longer matches the Bikeng Guide
- Naming rules and specifications for identifiers
- ECS MySQL query is slow
- MySQL的开发环境和测试环境有什么区别??
- Key summary IV of PMP examination - planning process group (2)
- Methods for creating multithreads ---1 creating subclasses of thread class and multithreading principle
- Global exception handlers and unified return results
- Dbeaver installation and use tutorial (super detailed installation and use tutorial)
猜你喜欢

小米旗下支付公司被罚 12 万,涉违规开立支付账户等:雷军为法定代表人,产品包括 MIUI 钱包 App

我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!

满电出发加速品牌焕新,长安电动电气化产品吹响“集结号”

详解final、finally和finalize

虚拟机14安装win7(图教程)

如何查看谷歌浏览器保存的网页密码

SQL中的DQL、DML、DDL和DCL是怎么区分和定义的

PMP考试重点总结六——图表整理

Full link service tracking implementation scheme

Starting from full power to accelerate brand renewal, Chang'an electric and electrification products sound the "assembly number"
随机推荐
手机号、邮箱正则验证[通俗易懂]
优秀笔记软件盘点:好看且强大的可视化笔记软件、知识图谱工具Heptabase、氢图、Walling、Reflect、InfraNodus、TiddlyWiki
满电出发加速品牌焕新,长安电动电气化产品吹响“集结号”
Stutter participle_ Principle of word breaker
PMP考试重点总结八——监控过程组(2)
Proxy mode (proxy)
函数的分文件编写
Dbeaver连接人大金仓KingbaseES V8(超详细图文教程)
谁知道在中信建投证券开户是不是安全的
JDBC connection database (MySQL) steps
组合模式(Composite Pattern)
Installing redis under Linux and windows (ultra detailed graphic tutorial)
An error is reported when uninstalling Oracle
大纲笔记软件 Workflowy 综合评测:优点、缺点和评价
Interpretation of new products: realm launched GT neo2 Dragon Ball customized version
Au revoir! Navigateur ie, cette route Edge continue pour IE
Unity loads AssetBundle resources from the server and writes them to local memory, and loads the downloaded and saved AB resources from local memory to the scene
==和eqauls()的区别
JVM family (2) - garbage collection
Key summary V of PMP examination - execution process group