当前位置:网站首页>第三章 栈和队列
第三章 栈和队列
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;
}
链队列的存储实现
边栏推荐
- Virtual machine 14 installing win7 (Figure tutorial)
- What is online account opening? Is it safe to open an account online now?
- Proxy mode (proxy)
- 满电出发加速品牌焕新,长安电动电气化产品吹响“集结号”
- 理想中的接口自动化项目
- 爬虫小操作
- Methods for creating multithreads ---1 creating subclasses of thread class and multithreading principle
- PMP考试重点总结六——图表整理
- 栈的弹出压入序列<难度系数>
- Stutter participle_ Principle of word breaker
猜你喜欢

Installing redis under Linux and windows (ultra detailed graphic tutorial)

桥接模式(Bridge)

bye! IE browser, this route edge continues to go on for IE

JDBC connection database (MySQL) steps

组合模式(Composite Pattern)

Write a simple timeline

Dbeaver installation and use tutorial (super detailed installation and use tutorial)

PMP考试重点总结五——执行过程组

DBeaver安装与使用教程(超详细安装与使用教程)
![QT signal and slot communication mechanism (when multiple windows communicate back and forth [parent and child windows])](/img/17/57ffb7393b71eddc5ac92ae3944338.jpg)
QT signal and slot communication mechanism (when multiple windows communicate back and forth [parent and child windows])
随机推荐
Custom exception classes and exercises
Interpretation of new products: realm launched GT neo2 Dragon Ball customized version
理想中的接口自动化项目
Matplotlib属性及注解
flink cep 跳过策略 AfterMatchSkipStrategy.skipPastLastEvent() 匹配过的不再匹配 碧坑指南
Ingersoll Rand panel maintenance IR Ingersoll Rand microcomputer controller maintenance xe-145m
Sword finger offer | Fibonacci sequence
Application of X6 in data stack index management
标识符的命名规则和规范
Ingersoll Rand面板维修IR英格索兰微电脑控制器维修XE-145M
==And eqauls()
结巴分词器_分词器原理
For the development of short video app, the elder warned me to choose the open source code
Check whether the table contains rows SQL Server 2005 - check whether a table contains rows or not SQL Server 2005
On the influence of small program on the digitalization of media industry
Methods for creating multithreads ---1 creating subclasses of thread class and multithreading principle
Static page of pinyougou mall
用 Compose 实现个空调,为你的夏日带去清凉
布隆过滤器 课程研究报告
异常