1. 程式人生 > >數據結構之循環隊列(C++版)

數據結構之循環隊列(C++版)

銷毀 tro lists ron include ring 基址 void delete

#include <iostream>
#include <stdlib.h>
#include <string>
#define MAXLISTSIZE 100 //預設的存儲空間最大容量

using namespace std;
typedef string ElemType;
typedef struct
{
ElemType *elem; //存儲空間基址
int rear; //隊尾指針
int front; //隊頭指針
int queuesize; //允許的最大存儲空間,以元素為單位
}Queue;

void InitQueue(Queue &Q);
void DestroyQueue(Queue &Q);
void EnQueue(Queue &Q);
void DeQueue(Queue &Q);
void GetElem(Queue Q);
void QueueLength(Queue Q);
void QueueEmpty(Queue Q);
void QueueFull(Queue Q);

int main(void)
{
Queue Q;
int z;
cout << "+---------------------------+" << ‘\n‘;
cout << "|---------循環隊列----------|" << ‘\n‘;
cout << "+---------------------------+" << ‘\n‘;
cout << "提示:為保證您的操作得到保存,請按正常順序退出系統^_^" << ‘\n‘;
do
{
cout << ‘\n‘ << ‘\t‘ << ‘\t‘ << ‘\t‘ <<"--------------------------------" << ‘\n‘;
cout << ‘\t‘ << ‘\t‘ << ‘\t‘ <<"+ 主菜單 |" << ‘\n‘;
cout << ‘\t‘ << ‘\t‘ << ‘\t‘ <<"+--------------------------------" << ‘\n‘;
cout << ‘\t‘ << ‘\t‘ << ‘\t‘ <<"+ [1]----循環隊列初始化 |" << ‘\n‘;
cout << ‘\t‘ << ‘\t‘ << ‘\t‘ <<"+ [2]----循環隊列的銷毀 |" << ‘\n‘;
cout << ‘\t‘ << ‘\t‘ << ‘\t‘ <<"+ [3]----循環隊列的入隊 |" << ‘\n‘;
cout << ‘\t‘ << ‘\t‘ << ‘\t‘ <<"+ [4]----循環隊列的出隊 |" << ‘\n‘;
cout << ‘\t‘ << ‘\t‘ << ‘\t‘ <<"+ [5]----循環隊列的存取 |" << ‘\n‘;
cout << ‘\t‘ << ‘\t‘ << ‘\t‘ <<"+ [6]----循環隊列的長度 |" << ‘\n‘;
cout << ‘\t‘ << ‘\t‘ << ‘\t‘ <<"+ [7]----循環隊列的判空 |" << ‘\n‘;
cout << ‘\t‘ << ‘\t‘ << ‘\t‘ <<"+ [8]----循環隊列的判滿 |" << ‘\n‘;
cout << ‘\t‘ << ‘\t‘ << ‘\t‘ <<"+ [0]----退出系統 |" << ‘\n‘;
cout << ‘\t‘ << ‘\t‘ << ‘\t‘ <<"--------------------------------" << ‘\n‘;
cout << "請輸入您的選擇:";
cin >> z;
switch(z)
{
case 0 : break;
case 1 : InitQueue(Q);break;
case 2 : DestroyQueue(Q);break;
case 3 : EnQueue(Q);break;
case 4 : DeQueue(Q);break;
case 5 : GetElem(Q);break;
case 6 : QueueLength(Q);break;
case 7 : QueueEmpty(Q);break;
case 8 : QueueFull(Q);break;
default:cout << "無效選項!" << ‘\n‘;system("pause");
}
}
while(z!= 0);
}

void InitQueue(Queue &Q)
{
//構造一個最大存儲空間為 maxsize 的空隊列 Q
int maxsize;
cout << "請輸入順序隊列的最大存儲空間:";
cin >> maxsize;
if(maxsize == 0)
maxsize = MAXLISTSIZE;
Q.elem = new ElemType[maxsize]; //為循環隊列分配存儲空間
if(!Q.elem)
{
cout << "存儲分配失敗" << endl; //存儲分配失敗
system("pause");
return;
}
Q.queuesize = maxsize;
Q.front = Q.rear = 0;
system("pause");
}//InitQueue

void DestroyQueue(Queue &Q)
{
if(Q.queuesize == 0)
{
cout << "隊列已銷毀,請選擇1初始化棧" << endl;
system("pause");
return;
}
delete[] Q.elem;
Q.queuesize = 0;
cout << "循環隊列已銷毀" << endl;
system("pause");
}

void EnQueue(Queue &Q)
{
ElemType e;
int n, j;
if(Q.queuesize == 0)
{
cout << "隊列已銷毀,請選擇1初始化棧" << endl;
system("pause");
return;
}
if((Q.rear + 1) % Q.queuesize == Q.front)
{
cout << "該隊列已滿,無法入隊" << endl;
system("pause");
return;
}
cout << "請輸入入隊元素個數(元素個數小於等於" << Q.queuesize - ((Q.rear-Q.front+Q.queuesize) % Q.queuesize) -1 << "): " ;
cin >> n;
cout << "請輸入入隊的元素:";
for(j = 0; j < n; j++)
{
cin >> e;
Q.elem[Q.rear] = e;
Q.rear = (Q.rear+1) % Q.queuesize;
}
system("pause");
}

void DeQueue(Queue &Q)
{

ElemType e;
int n, j;
if(Q.queuesize == 0)
{
cout << "隊列已銷毀,請選擇1初始化棧" << endl;
system("pause");
return;
}
if (Q.front == Q.rear)
{
cout << "隊為空,無法出隊" << endl;
system("pause");
return;
}
cout << "請輸入出隊元素個數(元素個數小於等於" << ((Q.rear-Q.front+Q.queuesize) % Q.queuesize) << "): ";
cin >> n;
cout << "出隊的元素為: ";
for(j = 0; j < n; j++)
{
e = Q.elem[Q.front];
cout << e << ‘ ‘;
Q.front = (Q.front+1) % Q.queuesize;
}
cout << endl;
system("pause");
}

void GetElem(Queue Q)
{
if(Q.queuesize == 0)
{
cout << "隊列已銷毀,請選擇1初始化棧" << endl;
system("pause");
return;
}
if (Q.front == Q.rear)
{
cout << "隊列為空,無法獲取隊首元素" << endl;
system("pause");
return;
}
cout << Q.elem[Q.front] << endl;
system("pause");
}

void QueueLength(Queue Q)
{
if(Q.queuesize == 0)
{
cout << "隊列已銷毀,請選擇1初始化棧" << endl;
system("pause");
return;
}
cout << "循環隊列的長度:" << ((Q.rear-Q.front+Q.queuesize) % Q.queuesize) << endl;
system("pause");
}

void QueueEmpty(Queue Q)
{
if(Q.queuesize == 0)
{
cout << "隊列已銷毀,請選擇1初始化棧" << endl;
system("pause");
return;
}
if (Q.front == Q.rear)
cout << "隊列為空" << endl;
else
cout << "隊列不為空" << endl;
system("pause");
return;
}

void QueueFull(Queue Q)
{
if(Q.queuesize == 0)
{
cout << "隊列已銷毀,請選擇1初始化棧" << endl;
system("pause");
return;
}
if ((Q.rear + 1) % Q.queuesize == Q.front)
cout << "該隊列已滿" << endl;
else
cout << "該隊列未滿" << endl;
system("pause");
return;
}

數據結構之循環隊列(C++版)