1. 程式人生 > >迴圈線性佇列的基本操作(帶實驗資料)

迴圈線性佇列的基本操作(帶實驗資料)

#include <iostream>
#include <cstring>
using namespace std;

bool overflow=0;
struct Queue {
	int date[300];
	int head;
	int tail;
	int len;
}queue;

void InitQueue(Queue &Q)
{
	Q.head=0;
	Q.tail=0;
	Q.len=0;
	cout<<"Initial successfully!"<<endl;
}

void DestroyQueue(Queue &Q)
{
	Q.head=-1;
	Q.tail=-1;
	Q.len=-1;
	memset(Q.date,0,sizeof(Q.date));
	cout<<"Destroy succeed."<<endl;
}

void ClearQueue(Queue &Q)
{
	if (Q.len==-1) {
		cout<<"Queue isn't exist."<<endl;
	}
	else {
		Q.head=0;
		Q.tail=0;
		Q.len=0;
		memset(Q.date,0,sizeof(Q.date));
		cout<<"Clear successfully!"<<endl;
	}
}

void QueueEmpty(Queue &Q)
{
	if (Q.len==-1)
	cout<<"Queue isn't exist."<<endl;
	else if (Q.len==0)
	cout<<"TRUE"<<endl;
	else
	cout<<"FALSE"<<endl;
}

void QueueLength(Queue &Q)
{
	if (Q.len==-1)
	cout<<"Queue isn't exist."<<endl;
	else 
	cout<<"Queue length is "<<Q.len<<" ."<<endl;
}

void GetHead(Queue &Q,int &e)
{
	if (Q.len==0) {
		cout<<"Queue is empty."<<endl;
	}
	else {
		e=Q.date[Q.head];
		cout<<e<<endl;
	}
}

void EnQueue(Queue &Q,int e)
{
	Q.date[Q.tail++]=e;
	if (Q.tail==300) {
		overflow=!overflow;	//Խ���ж� 
	}
	Q.tail%=300;
	Q.len++;
	cout<<"Insert successfully!"<<endl;
	if (Q.len==300) {
		cout<<"The queue is full."<<endl;
	}
}

void DeQueue(Queue &Q,int &e)
{
	e=Q.date[Q.head];
	Q.date[Q.head++]=0;
	if (Q.head==300) {
		overflow=!overflow;	//Խ���ж� 
	}
	Q.head%=300;
	Q.len--;
	cout<<"Delete succeed.The value is "<<e<<" ."<<endl;
	if (Q.len==0)
	cout<<"Queue is empty."<<endl;	
}

void QueueTraverse(Queue &Q)
{
	if (overflow==1) {
		for (int i=Q.head;i<300;i++) {
			cout<<Q.date[i]<<" ";
		}
		for (int i=0;i<Q.tail;i++) {
			cout<<Q.date[i]<<" ";
		}
		cout<<endl;
	}
	else {
		for (int i=Q.head;i<Q.tail;i++) {
			cout<<Q.date[i]<<" ";
		}
		cout<<endl;
	}
}

int main()
{
	cout<<"Please enter what you want to do."<<endl
	<<"1.Initial a queue."<<endl
	<<"2.Destroy the queue."<<endl
	<<"3.Clear the queue."<<endl
	<<"4.Is the queue empty?"<<endl
	<<"5.Length of the queue."<<endl
	<<"6.The first number of the queue."<<endl
	<<"7.Insert a number."<<endl
	<<"8.Delete a number."<<endl
	<<"9.Print the queue."<<endl;
	
	queue.len=-1;
	int n,e;
	while (cin>>n) {
		switch (n) {
			case 1:
				InitQueue(queue);
				for (int i=0;i<300;i++) {
					EnQueue(queue,i);					
				}
				break;
			case 2:
				DestroyQueue(queue);
				break;
			case 3:
				ClearQueue(queue);
				break;
			case 4:
				QueueEmpty(queue);
				break;
			case 5:
				QueueLength(queue);
				break;
			case 6:
				if (queue.len==-1)
				cout<<"Queue isn't exist."<<endl;
				else
				GetHead(queue,e);
				break;
			case 7:
				if (queue.len==-1)
				cout<<"Queue isn't exist."<<endl;
				else  {
					cout<<"Please enter the number you want to insert."<<endl;
					cin>>e;
					EnQueue(queue,e);	
				}
				break;
			case 8:
				if (queue.len==-1)
				cout<<"Queue isn't exist."<<endl;
				else if (queue.len==0)
				cout<<"Queue is empty."<<endl;
				else
				DeQueue(queue,e);
				break;
			case 9:
				if (queue.len==-1)
				cout<<"Queue isn't exist."<<endl;
				else if (queue.len==0)
				cout<<"Queue is empty."<<endl;
				else
				QueueTraverse(queue);
				break;
		}
	}
	
	return 0;
}