C語言數(shù)據(jù)結(jié)構(gòu)之鏈隊(duì)列的基本操作
1.隊(duì)列的定義
隊(duì)列 (Queue)是另一種限定性的線性表,它只允許在表的一端插入元素,而在另一端刪除元素,所以隊(duì)列具有先進(jìn)先出(Fist In Fist Out,縮寫為FIFO)的特性。在隊(duì)列中,允許插入的一端叫做隊(duì)尾(rear),允許刪除的一端則稱為隊(duì)頭(front)。 假設(shè)隊(duì)列為q=(a1,a2,…,an),那么a1就是隊(duì)頭元素,an則是隊(duì)尾元素。隊(duì)列中的元素是按照a1、a2、…、an的順序進(jìn)入的, 退出隊(duì)列也必須按照同樣的次序依次出隊(duì),也就是說,只有在a1、a2、…、an-1都離開隊(duì)列之后,an才能退出隊(duì)列。
2.隊(duì)列的表示和實(shí)現(xiàn)

鏈隊(duì)列可以定義如下:
#define TRUE 1
#define FALSE 0
typedef struct QNode{
QElemType data;
struct QNode *next;
}QNode, *QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
(1) 初始化操作
Status InitQueue(LinkQueue &Q)
{
Q.front = Q.rear = (Queueptr) malloc(sizeof(QNode));
if(!Q.front) exit ( OVERFLOW);
Q.front ->next = NULL;
return OK;
}
(2)銷毀隊(duì)列
Status DestroyQueue(LinkQueue &Q)
{
while(Q.front) {
Q.rear = Q.front->next;
free (Q.front);
Q.front = Q.rear;
}
return OK;
}
(3) 入隊(duì)操作
Status EnQueue (LinkQueue &Q, QelemType e)
{
p= (QueuePtr) malloc(sizeof(QNode));
if (!p) exit ( OVERFLOW);
p->data = e; p->next = NULL;
Q.rear -> next =p;
Q.rear = p;
return OK;
}
(4) 出隊(duì)操作
Status DeQueue (LinkQueue &Q, QelemType &e)
{
if ( Q.front == Q.rear) return ERROR;
p=Q.front->next;
e=p->data;
Q.front->next =p->next;
if (Q.rear == p) Q.rear = Q.front;
free(p);
return OK;
}
附錄完整代碼:
#include<iostream>
using namespace std;
#define OK 1
#define FALSE 0
typedef int QElemType;
typedef int Status;
typedef struct QNode{
QElemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr font;
QueuePtr near;
}LinkQueue;
Status InitQueue(LinkQueue &Q)
{
Q.font=(QueuePtr)malloc(sizeof(QNode));
if(!Q.font) exit(FALSE);
Q.font->next=NULL;
Q.near=Q.font;
return OK;
}
Status QueueEmpty(LinkQueue Q)
{
if(Q.font->next) return OK;
return FALSE;
}
Status EnQueue(LinkQueue &Q,QElemType e)
{
QueuePtr p=(QueuePtr)malloc(sizeof(QNode));
p->data=e;
Q.near->next = p;
Q.near = Q.near->next;
p->next = NULL;
return OK;
}
Status DeQueue(LinkQueue &Q,QElemType &e)
{
if(!Q.font->next) return FALSE;
QueuePtr p;
p=Q.font->next;
e=p->data;
Q.font->next=p->next;
if(Q.near==p) Q.near=Q.font; //當(dāng)是最后一個元素時(shí),Q.font=NULL,Q.near=Q.font
free(p);
return OK;
}
Status ClearQueue(LinkQueue &Q)
{
QueuePtr p;
p=Q.font->next;
QueuePtr q;
while(p)
{
q=p;
p=p->next;
Q.font->next=p;
free(q);
}
Q.near=Q.font;
return OK;
}
void menu()
{
cout<<"初始化隊(duì)列:1"<<endl;
cout<<"入隊(duì):2"<<endl;
cout<<"出隊(duì):3"<<endl;
cout<<"清空隊(duì)列:4"<<endl;
cout<<"退出:5"<<endl;
}
int main()
{
LinkQueue Q;
while(true)
{
int n;
menu();
scanf("%d",&n);
int e=-1;
switch(n)
{
case 1:
InitQueue(Q);
continue;
case 2:
printf("請輸入一個元素");
scanf("%d",&e);
EnQueue(Q,e);
continue;
case 3:
DeQueue(Q,e);
printf("\n出隊(duì)元素%d\n",e);
continue;
case 4:
ClearQueue(Q);
printf("清空成功\n");
continue;
default:
break;
}
if(n==5)break;
}
}
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
linux C 打印錯誤信息和標(biāo)準(zhǔn)輸入輸出詳細(xì)介紹
這篇文章主要介紹了linux C 打印錯誤信息和標(biāo)準(zhǔn)輸入輸出詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2016-12-12
詳解圖的應(yīng)用(最小生成樹、拓?fù)渑判?、關(guān)鍵路徑、最短路徑)
這篇文章主要介紹了圖的應(yīng)用(最小生成樹、拓?fù)渑判?、關(guān)鍵路徑、最短路徑),需要的朋友可以參考下2015-08-08
詳解基于C++實(shí)現(xiàn)約瑟夫環(huán)問題的三種解法
約瑟夫環(huán)問題是算法中相當(dāng)經(jīng)典的一個問題,其問題理解是相當(dāng)容易的,并且問題描述有非常多的版本,并且約瑟夫環(huán)問題還有很多變形,通過這篇約瑟夫問題的講解,一定可以帶你理解透徹2021-06-06

