1、棧
頭文件
#ifndef ZHAN_H
#define ZHAN_H
#define MAX 8
#include <iostream>
using namespace std;
class Shu
{
int datatype; //入棧的數(shù)據(jù)
int *arr; //棧的數(shù)組
int top; //記錄棧頂元素的下標(biāo)
public:
//構(gòu)造函數(shù)
Shu();
//析構(gòu)函數(shù)
~Shu();
//判斷空
int stack_empty();
//判斷滿
int stack_full();
//入棧
int stack_push(int data);
//遍歷棧
void stack_show();
//出棧
int stack_pop();
//獲取棧頂元素
int stack_top();
//求棧的大小
int stack_size();
//清空棧
void stack_free();
};
void xitong();
#endif // ZHAN_H
源文件
#include "zhan.h"
Shu::Shu()
{
datatype=0;
arr = new int[MAX];
top=-1;
}
Shu::~Shu()
{
delete arr;
arr=nullptr;
}
//判斷空
int Shu::stack_empty()
{
if(NULL==arr)
{
cout<<"判空失敗"<<endl;
return -1;
}
return this->top==-1;
}
//判斷滿
int Shu::stack_full()
{
if(NULL==arr)
{
cout<<"判滿失敗"<<endl;
return -1;
}
return this->top==MAX-1;
}
//入棧
int Shu::stack_push(int data)
{
datatype=data;
if(stack_full())
{
cout<<"入棧失敗"<<endl;
return -1;
}
//top下標(biāo)自增
this->top++;
//將輸入的函數(shù)放入數(shù)組中
arr[this->top]=datatype;
cout<<"入棧成功"<<endl;
return 0;
}
//遍歷棧
void Shu::stack_show()
{
if(stack_empty())
{
cout<<"遍歷失敗"<<endl;
return ;
}
//遍歷
for(int i=0;i<this->top+1;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
//出棧
int Shu::stack_pop()
{
if(stack_empty())
{
cout<<"出棧失敗"<<endl;
return -1;
}
//出棧
cout<<arr[this->top]<<"出棧成功"<<endl;
//自減
this->top--;
return 0;
}
//獲取棧頂元素
int Shu::stack_top()
{
if(stack_empty())
{
cout<<"獲取棧頂元素失敗"<<endl;
return -1;
}
//獲取棧頂元素
cout<<"棧頂元素為:"<<arr[top]<<endl;
return 0;
}
//求棧的大小
int Shu::stack_size()
{
if(stack_empty())
{
cout<<"求棧大小失敗"<<endl;
return -1;
}
//棧的大小
int size=this->top+1;
cout<<"棧的大小為:"<<size<<endl;
return 0;
}
//清空棧
void Shu::stack_free()
{
this->top=-1;
cout<<"清空棧成功"<<endl;
}
//系統(tǒng)函數(shù)
void xitong()
{
//定義一個Shu類型的變量名
Shu zhan;
while(1)
{
END:
system("CLS");
cout<<"**********************"<<endl;
cout<<"********1、入棧********"<<endl;
cout<<"********2、遍歷********"<<endl;
cout<<"********3、出棧********"<<endl;
cout<<"********4、棧頂元素*****"<<endl;
cout<<"********5、棧的大小*****"<<endl;
cout<<"********0、退出********"<<endl;
cout<<"**********************"<<endl;
int num;
int data;
cout<<"請輸入對應(yīng)的功能:";
cin>>num;
switch(num)
{
case 1:
cout<<"請輸入要加入的數(shù):";
cin>>data;
zhan.stack_push(data);
break;
case 2:
zhan.stack_show();
break;
case 3:
zhan.stack_pop();
break;
case 4:
zhan.stack_top();
break;
case 5:
zhan.stack_size();
break;
case 0:
goto ENDL;
}
int qp;
cout<<"輸入清屏(7):";
cin>>qp;
if(qp==7)
{
goto END;
}
}
ENDL:
cout<<"系統(tǒng)退出成功"<<endl;
zhan.stack_free();
}
主函數(shù)
#include <iostream>
#include "zhan.h"
using namespace std;
int main()
{
xitong();
return 0;
}
2、循環(huán)隊列
頭文件
#ifndef DUILIE_H
#define DUILIE_H
#define MAX 8
#include <iostream>
using namespace std;
class Shu
{
int datatype; //入隊的數(shù)據(jù)
int *arr; //循環(huán)隊列的數(shù)組
int head; //記錄隊列頭元素的下標(biāo)
int tail; //記錄隊列尾元素下標(biāo)
public:
//構(gòu)造函數(shù)
Shu();
//析構(gòu)函數(shù)
~Shu();
//判斷空
int queue_empty();
//判斷滿
int queue_full();
//入隊
int queue_push(int data);
//遍歷隊
void queue_show();
//出隊
int queue_pop();
//求隊列的大小
int queue_size();
//清空隊
void queue_free();
};
//系統(tǒng)函數(shù)
void xitong();
#endif // DUILIE_H
源文件
#include "duilie.h"
Shu::Shu()
{
datatype=0;
arr = new int[MAX];
head=0;
tail=0;
}
Shu::~Shu()
{
delete arr;
arr=nullptr;
}
//判斷空
int Shu::queue_empty()
{
if(NULL==arr)
{
cout<<"判空失敗"<<endl;
return -1;
}
return this->head==this->tail;
}
//判斷滿
int Shu::queue_full()
{
if(NULL==arr)
{
cout<<"判滿失敗"<<endl;
return -1;
}
return (this->tail+1)%MAX==this->head;
}
//入隊
int Shu::queue_push(int data)
{
datatype=data;
if(queue_full())
{
cout<<"入隊失敗"<<endl;
return -1;
}
//將輸入的函數(shù)放入數(shù)組中
arr[this->tail]=datatype;
//尾下標(biāo)自增
this->tail=(this->tail+1)%MAX;
cout<<"入隊成功"<<endl;
return 0;
}
//遍歷棧
void Shu::queue_show()
{
if(queue_empty())
{
cout<<"遍歷失敗"<<endl;
return ;
}
//遍歷
for(int i=this->head;i!=this->tail;i=(i+1)%MAX)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
//出隊
int Shu::queue_pop()
{
if(queue_empty())
{
cout<<"出隊失敗"<<endl;
return -1;
}
//出隊
cout<<arr[this->head]<<"出隊成功"<<endl;
//自增
this->head=(this->tail+1)%MAX;
return 0;
}
//求隊列的大小
int Shu::queue_size()
{
if(queue_empty())
{
cout<<"求隊列大小失敗"<<endl;
return -1;
}
//棧的大小
int size=(this->tail+MAX-this->head)%MAX;
cout<<"隊列的大小為:"<<size<<endl;
return 0;
}
//清空棧
void Shu::queue_free()
{
this->head=0;
this->tail=0;
cout<<"清空隊列成功"<<endl;
}
//系統(tǒng)函數(shù)
void xitong()
{
//定義一個Shu類型的變量名
Shu zhan;
while(1)
{
END:
system("CLS");
cout<<"**********************"<<endl;
cout<<"********1、入隊********"<<endl;
cout<<"********2、遍歷********"<<endl;
cout<<"********3、出隊********"<<endl;
cout<<"********4、隊列的大小***"<<endl;
cout<<"********0、退出********"<<endl;
cout<<"**********************"<<endl;
int num;
int data;
cout<<"請輸入對應(yīng)的功能:";
cin>>num;
switch(num)
{
case 1:
cout<<"請輸入要加入的數(shù):";
cin>>data;
zhan.queue_push(data);
break;
case 2:
zhan.queue_show();
break;
case 3:
zhan.queue_pop();
break;
case 4:
zhan.queue_size();
break;
case 0:
goto ENDL;
}
int qp;
cout<<"輸入清屏(7):";
cin>>qp;
if(qp==7)
{
goto END;
}
}
ENDL:
cout<<"系統(tǒng)退出成功"<<endl;
zhan.queue_free();
}
主函數(shù)
#include <iostream>
#include "duilie.h"
using namespace std;
int main()
{
xitong();
return 0;
}
3、思維導(dǎo)圖文章來源:http://www.zghlxwxcb.cn/news/detail-712692.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-712692.html
到了這里,關(guān)于c++實現(xiàn)數(shù)據(jù)結(jié)構(gòu)棧和隊列的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!