C++實現(xiàn)循環(huán)隊列的算法+步驟(附全代碼):
使用c++完成數(shù)據(jù)結(jié)構(gòu)循環(huán)隊列的基本操作,包括(初始化、入隊、出隊、取隊頭元素、遍歷輸出隊列、求隊列長度等),可直接編譯運行。
隊列
又稱為 “先進先出” (FIFO)線性表。限定插入操作只能在隊尾進行,而刪除操作只能在隊首進行。循環(huán)隊列
——采用順序存儲結(jié)構(gòu)來實現(xiàn),用一組連續(xù)的存儲單元依次存放從隊首到隊尾的元素,附設(shè)兩個整型變量front和rear分別指向隊首元素和隊尾元素的位置。
循環(huán)隊列的定義:
#define MAXQSIZE 100
typedef int QElemType;
typedef int Status;
//隊列的順序存儲結(jié)構(gòu)
typedef struct
{
QElemType* base; //存儲空間的基地址
int front; //頭指針
int rear; //尾指針
}SqQueue;
循環(huán)隊列的初始化:
【算法步驟】
① 為隊列分配一個最大容量為MAXQSIZE的數(shù)組空間,base指向數(shù)組空間首地址。
② 頭指針和尾指針置為零,表示隊列為空。
【算法描述】
//循環(huán)隊列的初始化
Status InitQueue(SqQueue& Q)
{//構(gòu)造一個空隊列Q
Q.base = new QElemType[MAXQSIZE];
if(!Q.base)
exit(OVERFLOW); //存儲分配失敗
Q.front = Q.rear = 0;
return OK;
}
循環(huán)隊列的入隊:
【算法步驟】
① 判斷隊列是否滿,滿則返回ERROR。
② 將新元素插入隊尾。
③ 隊尾指針加1。
【算法描述】
//循環(huán)隊列的入隊
Status EnQueue(SqQueue& Q, QElemType e)
{//插入元素e為Q的新的隊尾元素
if ((Q.rear + 1) % MAXQSIZE == Q.front)
return ERROR; //尾指針在循環(huán)意義上加1后等于頭指針,表明隊滿
Q.base[Q.rear] = e;
Q.rear = (Q.rear + 1) % MAXQSIZE;
return OK;
}
循環(huán)隊列的出隊:
【算法步驟】
① 判斷隊列是否為空,若空則返回ERROR。
② 保存隊頭元素。
③ 隊頭指針加1。
【算法描述】
//循環(huán)隊列的出隊
Status DeQueue(SqQueue& Q, QElemType &e)
{//刪除Q的隊頭元素,用e返回其值
if (Q.front == Q.rear)
return ERROR;
e = Q.base[Q.front];
Q.front = (Q.front + 1) % MAXQSIZE;
return OK;
}
取循環(huán)隊列的隊頭元素:
【算法步驟】
① 判斷隊列是否空。
② 返回隊頭元素的值,隊頭指針不變。
【算法描述】
//取循環(huán)隊列的隊頭元素
Status GetHead(SqQueue Q)
{//返回Q的隊頭元素,不修改隊頭指針
if (Q.front != Q.rear) //隊列非空
return Q.base[Q.front]; //返回隊頭元素的值,隊頭指針不變
}
取循環(huán)隊列的隊頭元素:
【算法步驟】
① 判斷隊列是否空,若空則返回ERROR。
② 循環(huán)輸出隊頭元素。
【算法描述】
//遍歷輸出循環(huán)隊列
Status QueueTraverse(SqQueue Q)
{
cout << "當前隊列為:";
if (Q.front == Q.rear)
return ERROR;
while (Q.front != Q.rear) //隊列非空
{
cout << Q.base[Q.front] << " ";
Q.front = (Q.front + 1) % MAXQSIZE;
}
cout << endl;
}
求循環(huán)隊列長度:
【算法描述】
//求循環(huán)隊列長度
Status QueueLength(SqQueue Q)
{//返回Q的元素個數(shù)
int len = (Q.rear - Q.front + MAXQSIZE) % MAXQSIZE;
cout << "循環(huán)隊列的長度為:" << len << endl;
return OK;
}
全代碼如下:
//循環(huán)隊列的基本操作.cpp
#include<iostream>
using namespace std;
#define ERROR 0
#define OK 1
#define MAXQSIZE 100
typedef int QElemType;
typedef int Status;
//隊列的順序存儲結(jié)構(gòu)
typedef struct
{
QElemType* base; //存儲空間的基地址
int front; //頭指針
int rear; //尾指針
}SqQueue;
Status InitQueue(SqQueue&); //初始化隊列
Status EnQueue(SqQueue&, QElemType); //入隊
Status DeQueue(SqQueue&, QElemType &); //出隊
Status GetHead(SqQueue); //取隊頭元素
Status QueueTraverse(SqQueue); //遍歷隊列
Status QueueLength(SqQueue); //隊列長度
int main()
{
SqQueue S;
int e, a;
if (InitQueue(S))
cout << "循環(huán)隊列初始化成功!" << endl;
else
cout << "循環(huán)隊列初始化失??!" << endl;
while (1)
{
cout << "\n【1】入隊 【2】出隊 【3】取隊頭元素 【4】輸出隊列 【5】求隊列長度 【0】退出" << endl;
cout << "請選擇要進行的操作:";
cin >> a;
switch (a)
{
case 1:
int x, n;
cout << "請輸入要插入的元素個數(shù):";
cin >> n;
for (int i = 0; i < n; i++)
{
cout << "請輸入第" << i + 1 << "元素值:";
cin >> x;
EnQueue(S, x);
}
cout << "入隊完成!" << endl;
break;
case 2:
cout << "請輸入要刪除的元素個數(shù):";
cin >> n;
for (int j = 0; j < n; j++) {
if (!DeQueue(S, e))
cout << "出隊失?。? << endl;
else
cout << "第【" << j+1 << "】個元素:" << e << " 出隊成功!" << endl;
}
break;
case 3:
cout << "隊頭元素為:" << GetHead(S) << endl;
break;
case 4:
if(!QueueTraverse(S))
cout << "隊列為空!" << endl;
break;
case 5:
QueueLength(S);
break;
case 0: return OK;
default:
return OK;
}
}
return 0;
}
//循環(huán)隊列的初始化
Status InitQueue(SqQueue& Q)
{//構(gòu)造一個空隊列Q
Q.base = new QElemType[MAXQSIZE];
if(!Q.base)
exit(OVERFLOW); //存儲分配失敗
Q.front = Q.rear = 0;
return OK;
}
//循環(huán)隊列的入隊
Status EnQueue(SqQueue& Q, QElemType e)
{//插入元素e為Q的新的隊尾元素
if ((Q.rear + 1) % MAXQSIZE == Q.front)
return ERROR; //尾指針在循環(huán)意義上加1后等于頭指針,表明隊滿
Q.base[Q.rear] = e;
Q.rear = (Q.rear + 1) % MAXQSIZE;
return OK;
}
//循環(huán)隊列的出隊
Status DeQueue(SqQueue& Q, QElemType &e)
{//刪除Q的隊頭元素,用e返回其值
if (Q.front == Q.rear)
return ERROR;
e = Q.base[Q.front];
Q.front = (Q.front + 1) % MAXQSIZE;
return OK;
}
//取循環(huán)隊列的隊頭元素
Status GetHead(SqQueue Q)
{//返回Q的隊頭元素,不修改隊頭指針
if (Q.front != Q.rear) //隊列非空
return Q.base[Q.front]; //返回隊頭元素的值,隊頭指針不變
}
//遍歷輸出循環(huán)隊列
Status QueueTraverse(SqQueue Q)
{
cout << "當前隊列為:";
if (Q.front == Q.rear)
return ERROR;
while (Q.front != Q.rear) //隊列非空
{
cout << Q.base[Q.front] << " ";
Q.front = (Q.front + 1) % MAXQSIZE;
}
cout << endl;
}
//求循環(huán)隊列長度
Status QueueLength(SqQueue Q)
{//返回Q的元素個數(shù)
int len = (Q.rear - Q.front + MAXQSIZE) % MAXQSIZE;
cout << "循環(huán)隊列的長度為:" << len << endl;
return OK;
}
運行結(jié)果:
文章來源:http://www.zghlxwxcb.cn/news/detail-415015.html
美美的解決!文章來源地址http://www.zghlxwxcb.cn/news/detail-415015.html
到了這里,關(guān)于【C++】【數(shù)據(jù)結(jié)構(gòu)】循環(huán)隊列的基本操作(初始化、入隊、出隊、取隊頭元素、遍歷輸出隊列、求隊列長度)順序隊列的算法實現(xiàn)【附全代碼】的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!