1. 程式人生 > >實驗二 順序佇列和鏈佇列

實驗二 順序佇列和鏈佇列

順序佇列
#include<iostream>
using namespace std;
const int Queuesize=100;
template<class DataType>
class Cirqueue
{
public:
	Cirqueue(){front=rear=Queuesize-1;}
	~Cirqueue(){}
	void Enqueue(DataType x);//入隊
	DataType Dequeue();//出隊
	DataType Getqueue();//取隊頭元素
	int Empty(){front=rear?return 1:return 0;}//判空
private:
	DataType data[Queuesize];//存放佇列元素的陣列
	int front,rear;
};

template<class DataType>
void Cirqueue<DataType>::Enqueue(DataType x)
{
	if((rear+1)%Queuesize==front)
		throw"上溢";
	rear=(rear+1)%Queuesize;
	data[rear]=x;
}

template<class DataType>
DataType Cirqueue<DataType>::Dequeue()
{
	if(rear==front)
		throw"下溢";
	front=(front+1)%Queuesize;
	return data[front];
}

template<class Datatype>
DataType Cirqueue<DataType>::Getqueue()
{
	if(rear==front) throw"下溢";
	i=(front+1)%Queuesize;
	return data[i];
}


void main()
{
	Cirqueue<int>C;
	if (C.Empty())
 		cout<<"佇列為空"<<endl;
	else
		cout<<"佇列非空"<<endl;
	cout<<"元素10和15執行入隊操作:"<<endl;
	try
	{
		C.Enqueue(10);
		C.Enqueue(15);
	}
	catch(char*wrong)
	{
		cout<<wrong<<endl;
	}
	cout<<"檢視隊頭元素:"<<endl;
	cout<<C.Getqueue()<<endl;
	cout<<"執行出隊操作:"<<endl;
	try
	{
		C.Dequeue();
	}
	catch(char*wrong)
	{
		cout<<wrong<<endl;
	}
	cout<<"檢視隊頭元素:"<<endl;
	cout<<C.Getqueue()<<endl;
鏈佇列



#ifndef LinkQueue_H
#define LinkQueue_H

template<class DataType>
struct Node
{
	DataType data;
	Node<DataType>*next;
};

template<class DataType>
class LinkQueue
{
	public:
		LinkQueue();
		~LinkQueue();
		void EnQueue(DataType x);
		DataType DeQueue();
		DataType GetQueue();
		int Empty();
	private:
		Node<DataType>*front,*rear;
};
#endif;
#include"LinkQueue.h"

template<class DataType>
LinkQueue<DataType>::LinkQueue()
{
	Node<DataType>*s=NULL;
	s=new Node<DataType>;
	s->next=NULL;
	front=rear=s;
}

template<class DataType>
LinkQueue<DataType>::~LinkQueue()
{
	Node<DataType>*p=NULL;
	while(front!=NULL)
	{
		p=front->next;
		delete front;
		front=p;
	}
}

template<class DataType>
void LinkQueue<DataType>::EnQueue(DataType x)
{
	Node<DataType>*s=NULL;
	s=new Node<DataType>;
	s->data=x;
	s->next=NULL;
	rear->next=s;
	rear=s;
}

template<class DataType>
DataType LinkQueue<DataType>::DeQueue()
{
	Node<DataType>*p=NULL;
	int x;
	if(rear==front) throw"下溢";
	p=front->next;
	x=p->data;
	front->next=p->next;
	if(p->next==NULL)rear=front;
	delete p;
	return x;
}

template<class DataType>
DataType LinkQueue<DataType>::GetQueue()
{
	if(front!=rear)
		return front->next->data;
}

template<class DataType>
int LinkQueue<DataType>::Empty()
{
	if(front==rear)
		return 1;
	else 
		return 0;
}
#include<iostream>
using namespace std;
#include"LinkQueue.cpp"
void main()
{
	LinkQueue<int>Q;
	if(Q.Empty())
		cout<<"佇列為空"<<endl;
	else
		cout<<"佇列非空"<<endl;
	cout<<"元素10和15執行入隊操作:"<<endl;
	try
	{
		Q.EnQueue(10);
		Q.EnQueue(15);
	}
	catch(char*wrong)
	{
		cout<<wrong<<endl;
	}
	cout<<"檢視隊頭元素:"<<endl;
	cout<<Q.GetQueue()<<endl;
	cout<<"執行出隊操作:"<<endl;
	try
	{
		Q.DeQueue();
	}
	catch(char*wrong)
	{
		cout<<wrong<<endl;
	}
	cout<<"檢視隊頭元素"<<endl;
	cout<<Q.GetQueue()<<endl;
}