当前位置:网站首页>第三章 栈和队列
第三章 栈和队列
2022-06-28 09:40:00 【钟钟终】
顺序栈的实现
#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<<"上溢"<<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<<"非空"<<endl;
else
cout<<"空"<<endl;
return 0;
}
链栈的实现
#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<<"非空"<<endl;
else
cout<<"空"<<endl;
ls.Push(1);
ls.Push(22);
cout<<ls.Pop()<<endl;
cout<<ls.Gettop()<<endl;
ls.Pop();
if(ls.Empty()!=1)
cout<<"非空"<<endl;
else
cout<<"空"<<endl;
return 0;
}
循环队列的实现
#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<<"上溢"<<endl;return ;
}
re=(re+1)%N;
data[re]=k;
}
int Cirqueue::de_que()
{
if(re==fr)
{
cout<<"下溢"<<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;
}
链队列的存储实现
边栏推荐
猜你喜欢

Ingersoll Rand panel maintenance IR Ingersoll Rand microcomputer controller maintenance xe-145m

Full link service tracking implementation scheme

线程的生命周期

Au revoir! Navigateur ie, cette route Edge continue pour IE

dotnet 使用 Crossgen2 对 DLL 进行 ReadyToRun 提升启动性能
![[ybtoj advanced training guide] maximum separation [hash] [Floyd]](/img/86/542ab1728a2ddbc01592b2fa83491a.jpg)
[ybtoj advanced training guide] maximum separation [hash] [Floyd]

How to view the web password saved by Google browser

通过PyTorch构建的LeNet-5网络对手写数字进行训练和识别

Xiaomi's payment company was fined 120000 yuan, involving the illegal opening of payment accounts, etc.: Lei Jun is the legal representative, and the products include MIUI wallet app

Static page of pinyougou mall
随机推荐
剑指Offer | 斐波那契数列
Function sub file writing
On the influence of small program on the digitalization of media industry
JVM family (2) - garbage collection
再见!IE浏览器,这条路由Edge替IE继续走下去
Application of X6 in data stack index management
The digital human industry is about to break out. What is the market pattern?
Multithreading concurrent parallel threaded process
Key summary IV of PMP examination - planning process group (2)
Regular verification of mobile phone number and email [easy to understand]
手机号、邮箱正则验证[通俗易懂]
Dbeaver connects to kingbasees V8 (ultra detailed graphic tutorial)
Key summary VII of PMP examination - monitoring process group (1)
读取pdf文字和excel写入操作
我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!
PHP curl forged IP address and header information code instance - Alibaba cloud
六月集训(第28天) —— 动态规划
纵观jBPM从jBPM3到jBPM5以及Activiti
Dbeaver installation and use tutorial (super detailed installation and use tutorial)
Wechat applet development log