顺序栈
头文件
#ifndef HEADER_H
#define HEADER_H
class Stack
{
private:
int *seqStack;
int top; //栈顶元素下标
public:
//初始化函数
void init(int size);
//判空函数(空返回1,非空返回0)
int empty();
//入栈函数
void stack_push(int num);
//出栈函数
int stack_pop();
//遍历函数
int stack_show();
//销毁函数
int stack_destroy();
};
#endif // HEADER_H
源文件
#include <iostream>
#include <header.h>
using namespace std;
//初始化函数
void Stack::init(int size)
{
seqStack = new int[size];
top = -1;
cout<<"初始化成功"<<endl;
}
//判空函数(空返回1,非空返回0)
int Stack::empty()
{
return top == -1?1:0;
}
//入栈函数
void Stack::stack_push(int num)
{
top++;
seqStack[top] = num;
cout<<"入栈成功"<<endl;
}
//出栈函数
int Stack::stack_pop()
{
if(empty())
{
cout<<"出栈失败"<<endl;
return 0;
}
top--;
cout<<"出栈成功"<<endl;
return 1;
}
//遍历函数
int Stack::stack_show()
{
if(empty())
{
cout<<"遍历失败"<<endl;
return 0;
}
for(int i=0;i<=top;i++)
{
cout<<seqStack[i]<<" ";
}
cout<<endl;
cout<<"遍历成功"<<endl;
return 1;
}
//销毁函数
int Stack::stack_destroy()
{
delete []seqStack;
seqStack = nullptr;
top = -1;
cout<<"销毁成功"<<endl;
return 1;
}
主调函数
#include <iostream>
#include <header.h>
using namespace std;
int main()
{
//定义类
Stack s1;
//初始化
s1.init(5);
//入栈
s1.stack_push(5);
s1.stack_push(6);
s1.stack_push(7);
s1.stack_push(8);
s1.stack_push(9);
//遍历
s1.stack_show();
//出栈
s1.stack_pop();
s1.stack_pop();
//遍历
s1.stack_show();
//销毁
s1.stack_destroy();
//遍历
s1.stack_show();
return 0;
}
结果
顺序队列
头文件
#ifndef HEAD_H
#define HEAD_H
#define Max 20
class Loopseque{
private:
int *data;
int head;
int tail;
public:
//创建
void seque_create();
//判空
bool seque_empty();
//判满
bool seque_full();
//入队
void seque_insert();
//出队
void seque_out();
//遍历
void seque_show();
//求队列长度
void seque_length();
//销毁
void seque_delete();
};
#endif // HEAD_H
源文件
#include <iostream>
#include <header.h>
using namespace std;
//创建
void Loopseque::seque_create(){
data = new int[Max];
head = 0;
tail = 0;
}
//判空
bool Loopseque::seque_empty(){
return head==tail;
}
//判满
bool Loopseque::seque_full(){
return (tail+1)%Max == head;
}
//入队
void Loopseque::seque_insert(){
if(seque_full()){
cout<<"队列已满,入队失败"<<endl;
return;
}
int num;
cout<<"请输入要入队的数:";
cin>>num;
data[tail] = num;
tail = (tail+1)%Max;
}
//出队
void Loopseque::seque_out(){
if(seque_empty()){
cout<<"队列已空,出队失败"<<endl;
return;
}
cout << data[head] <<"出队成功" <<endl;
head = (head+1)%Max;
}
//遍历
void Loopseque::seque_show(){
if(seque_empty()){
cout<<"队列已空,遍历失败"<<endl;
return;
}
for(int i = head ; i!=tail;i=(i+1)%Max){
cout<<data[i]<<' ';
}
cout<<endl;
}
//求队列长度
void Loopseque::seque_length(){
if(seque_empty()){
cout<<"队列已空,求队列长度失败"<<endl;
return;
}
cout << "队列长度 = " << (head+tail)%Max << endl;
}
//销毁
void Loopseque::seque_delete(){
delete []data;
data = nullptr;
cout<<"销毁成功"<<endl;
}
主调函数
#include <iostream>
#include <header.h>
using namespace std;
int main()
{
Loopseque ls;
//创建
ls.seque_create();
//创建后遍历
ls.seque_show();
//入队
ls.seque_insert();
ls.seque_insert();
ls.seque_insert();
//入队后遍历
ls.seque_show();
//求队列长度
ls.seque_length();
//出队
ls.seque_out();
ls.seque_out();
//出队后第一次遍历
ls.seque_show();
//出队
ls.seque_out();
//最终遍历
ls.seque_show();
//销毁
ls.seque_delete();
return 0;
}
结果