一、創(chuàng)建linkqueue.h頭文件,定義隊(duì)列并聲明函數(shù)

#pragma once
#includeusing namespace std;
//定義
struct QNode
{
int data; //數(shù)據(jù)域
QNode* next; //指針域
};
typedef QNode* QueuePtr;//將QueuePtr定義為QNode*類(lèi)型
struct LinkQueue
{
QueuePtr front; //隊(duì)頭指針
QueuePtr rear; //隊(duì)尾指針
};
//初始化
bool InitQueue(LinkQueue& Q);
//插入
bool InsertQueue(LinkQueue& Q, int e);
//刪除
bool DeQueue(LinkQueue& Q,int &e);
//遍歷
void PrintQueue(LinkQueue Q);
//判斷隊(duì)列是否為空
bool QueueEmpty(LinkQueue Q);
//隊(duì)列長(zhǎng)度
int QueueLength(LinkQueue Q);
//清空
bool ClearQueue(LinkQueue& Q);
//銷(xiāo)毀
bool DestoryQueue(LinkQueue& Q); 二、創(chuàng)建linkqueue.cpp源文件,將聲明的函數(shù)做具體實(shí)現(xiàn)
#include"linkqueue.h"
//初始化
bool InitQueue(LinkQueue& Q)
{
Q.front = Q.rear = new QNode; //頭指針尾指針指向頭結(jié)點(diǎn)
if (!Q.front) //分配失敗退出
{
exit(0);
}
Q.front->next = NULL; //頭結(jié)點(diǎn)指針域置為空
return true;
}
//插入
bool InsertQueue(LinkQueue& Q, int e)
{
QueuePtr p = new QNode; //生產(chǎn)新結(jié)點(diǎn)
if (!p) //分配失敗
{
return false;
}
p->data = e; //將e的值賦給新結(jié)點(diǎn)的指針域
p->next = NULL; //新結(jié)點(diǎn)數(shù)據(jù)域?yàn)榭? Q.rear->next = p; //尾指針的指針域指向新結(jié)點(diǎn)
Q.rear = p; //新結(jié)點(diǎn)賦給尾指針
return true;
}
//刪除
bool DeQueue(LinkQueue& Q, int& e)
{
if (Q.front == Q.rear) //判斷隊(duì)列是否為空
{
return false;
}
QueuePtr p = Q.front->next; //將首元結(jié)點(diǎn)地址賦給p
e = p->data; //首元結(jié)點(diǎn)數(shù)據(jù)域賦值給e
Q.front->next = p->next; //頭結(jié)點(diǎn)的數(shù)據(jù)域指向首元結(jié)點(diǎn)的下一結(jié)點(diǎn)
//判斷尾指針是否指向首元結(jié)點(diǎn),是則將頭指針賦值給尾指針,表為空
if (Q.rear == p)
{
Q.rear = Q.front;
}
delete p; //是否p
return true;
}
//遍歷
void PrintQueue(LinkQueue Q)
{
QueuePtr p = Q.front->next;
while (p)
{
cout<< p->data<< endl;
p = p->next;
}
}
//判斷隊(duì)列是否為空
bool QueueEmpty(LinkQueue Q)
{
if (Q.front == Q.rear)
{
return true;
}
else
{
return false;
}
}
//隊(duì)列長(zhǎng)度
int QueueLength(LinkQueue Q)
{
QueuePtr p = Q.front->next;
int count = 0;
while (p)
{
count++;
p = p->next;
}
return count;
}
//清空
bool ClearQueue(LinkQueue& Q)
{
QueuePtr p = Q.front->next;
while (p)
{
QueuePtr q = p->next;
delete p;
p = q;
}
Q.front->next = NULL;
Q.rear = Q.front;
return true;
}
//銷(xiāo)毀
bool DestoryQueue(LinkQueue& Q)
{
while (Q.front) //頭指針不為空
{
//銷(xiāo)毀時(shí)尾指針空閑,可利用尾指針保存下一結(jié)點(diǎn)
Q.rear = Q.front->next;
delete Q.front; //釋放頭指針
Q.front = Q.rear; //將下一結(jié)點(diǎn)重新賦值給頭結(jié)點(diǎn)
}
return true;
}三、在主函數(shù)中測(cè)試
#includeusing namespace std;
#include"linkqueue.h" //包含頭文件
int main()
{
//1.創(chuàng)建隊(duì)列Q,并初始化
LinkQueue Q;
InitQueue(Q);
//2.插入
int arr[5] = { 1,2,3,4,5 };
for (int i = 0; i< 5; i++)
{
InsertQueue(Q, arr[i]);
}
cout<< "插入后元素為:"<< endl;
//3.遍歷
PrintQueue(Q);
//4.刪除
int e;
DeQueue(Q, e);
cout<< "元素"<< e<< "已刪除"<< endl;
cout<< "刪除后打印:"<< endl;
PrintQueue(Q);
//5.判斷隊(duì)列是否為空
if (QueueEmpty(Q))
{
cout<< "隊(duì)列為空"<< endl;
}
else
{
//6.隊(duì)列長(zhǎng)度
cout<< "隊(duì)列不為空,隊(duì)列長(zhǎng)為:"<< QueueLength(Q)<< endl;
}
//7.清空
ClearQueue(Q);
cout<< "清空后打印:"<< endl;
PrintQueue(Q);
//8.銷(xiāo)毀
DestoryQueue(Q);
system("pause");
return 0;
}
四、最終結(jié)果

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
網(wǎng)站欄目:利用C++實(shí)現(xiàn)隊(duì)列的表示和操作,插入、刪除、遍歷等(鏈?zhǔn)奖硎荆?創(chuàng)新互聯(lián)
網(wǎng)址分享:http://chinadenli.net/article8/ecdop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、Google、定制網(wǎng)站、小程序開(kāi)發(fā)、企業(yè)建站、用戶體驗(yàn)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)