循環隊列以及鏈隊列的實現

隊列是一種操作受限的線性表,因此它跟線性表一樣,有順序隊列和鏈式隊列兩種存儲結構。首先,對於順序隊列,用的比較多的是循環順序隊列,簡稱循環隊列。關於循環隊列,闊以參考下面這篇博客的內容,講解的非常清晰:https://www.cnblogs.com/curo0119/p/8608606.html。在此我不再過多贅述,只貼出自己實現的循環隊列代碼,如下所示:

//以下代碼是有關循環隊列的一些操作 
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

typedef int QElemType;
typedef int Status;

#define MAXSIZE 5
#define OK 1
#define ERROR 0
#define TRUE 1
#define FLASE 0

//循環隊列定義 
typedef struct{
	QElemType *base;
	int front;
//尾指針指向隊尾元素的下一位置
	int rear;
	int maxsize;
}SqQueue;

//隊列初始化 
Status InitSqQueue(SqQueue &Q){
	Q.base=new QElemType[MAXSIZE];
	if(!Q.base) return ERROR;
	Q.front=Q.rear=0;
	Q.maxsize=MAXSIZE;
	return OK;
}

//判斷隊列是否爲空(注意:循環隊列爲空的條件是頭尾指針相等,但其值不一定爲零) 
Status EmptySqQueue(SqQueue Q){
	if(Q.front==Q.rear) return TRUE;
	else return FLASE; 
}

//求循環隊列的長度 
Status SqQueueLength(SqQueue Q){
	if(Q.front==Q.rear) return ERROR;
	return((Q.rear-Q.front+Q.maxsize)%Q.maxsize);
}

//出隊
Status DeSqQueue(SqQueue &Q,QElemType &e){
	//首先判斷隊列是否爲空 
	if(EmptySqQueue(Q)) return ERROR;
	e=Q.front;
	Q.front=(Q.front+1)%Q.maxsize;
	return OK;
}

//入隊
Status EnSqQueue(SqQueue &Q,QElemType e){
	//首先判斷是否隊滿
	if((Q.rear+1)%Q.maxsize==Q.front) return ERROR;
	Q.base[Q.rear]=e;
	Q.rear=(Q.rear+1)%Q.maxsize;
	return OK;
}

//測試
int main(){
	SqQueue Q;
	InitSqQueue(Q);
	cout<<EmptySqQueue(Q)<<endl;
	EnSqQueue(Q,1);
	cout<<SqQueueLength(Q)<<endl;
	cout<<EmptySqQueue(Q)<<endl;
} 

若隊列長度難以估計,則宜採用鏈隊列,相關操作代碼如下:

//以下代碼是有關鏈式隊列的一些操作 
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

typedef int QElemType;
typedef int Status;

#define MAXSIZE 5
#define OK 1
#define ERROR 0
#define TRUE 1
#define FLASE 0

//定義鏈式隊列中節點的結構 
typedef struct Qnode{
	QElemType data;
	struct Qnode *next;
}Qnode,*QnodePtr;

//定義鏈隊列
typedef struct{
	QnodePtr front;
	QnodePtr rear;
}LinkQueue;

//初始化鏈隊列
Status InitLinkQueue(LinkQueue &Q){
	Q.front=Q.rear=new Qnode;
	if(!Q.front) exit(ERROR);
	return OK;
} 

//入隊
Status EnLinkQueue(LinkQueue &Q,QElemType e){
	if(!Q.front) exit(ERROR);
	Qnode *q=new Qnode;
	if(!q) return ERROR;
	q->data=e;
	Q.rear->next=q;
	Q.rear=q;
	return OK;
}

//出隊
Status DeLinkQueue(LinkQueue &Q,QElemType &e){
	//首先判斷隊列是否爲空 
	if(Q.front==Q.rear) return ERROR;
	//注意當隊列中只有一個元素時,需要作爲特殊情況考慮 
	if(Q.front->next==Q.rear){
		Qnode *temp=Q.rear;
		e=temp->data;
		Q.rear=Q.front;
		Q.front->next=NULL;
		delete temp;
	}
	else{
		Qnode *temp=Q.front->next;
		e=temp->data;
		Q.front->next=temp->next;
		delete temp;	
	}
	return OK;
}

//判斷隊列是否爲空
Status EmptyLinkQueue(LinkQueue Q){
	if(Q.front==Q.rear) return TRUE;
	else return FLASE;
} 

//求隊列長度 
Status LengthLinkQueue(LinkQueue Q){
	if(EmptyLinkQueue(Q)) return ERROR;
	int length=0;
	while(Q.front!=Q.rear){
		length++;
		Q.front=Q.front->next;
	}
	return length;
}

//測試 
int main(){
	LinkQueue(Q);
	InitLinkQueue(Q);
	cout<<EmptyLinkQueue(Q)<<endl;
	EnLinkQueue(Q,3);
	cout<<EmptyLinkQueue(Q)<<endl;
	cout<<LengthLinkQueue(Q)<<endl;
	int e;
	DeLinkQueue(Q,e);
	cout<<e<<endl;
	cout<<EmptyLinkQueue(Q)<<endl;
	cout<<LengthLinkQueue(Q)<<endl;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章