数据结构-队列:循环队列与链队列的C++模板类实现

C++模板类实现循环队列和链队列

实现的功能: 

1.入列,2.出列,3.显示对头,4.判空,5.判满,6.清空队列,7.求队列长度。


循环队列的模板类实现代码:

//循环队列******************************************************************************
#include 

using namespace std;

#define QUEUE_SIZE 100

template 
class CirQueue
{
public:
	CirQueue(){ queue_front = QUEUE_SIZE - 1; queue_rear = QUEUE_SIZE - 1; queue_size = 0; }
	~CirQueue(){}
	void EnQueue(DataType index);
	void DeQueue();
	DataType GetQueue();
	bool Empty(){ if (queue_front == queue_rear) return true; else return false; }
	bool Full(){ if ((queue_rear + 1) % QUEUE_SIZE == queue_front) return true; else  return false; }
	void clearQueue(){ while (queue_front != queue_rear) { data[queue_rear] = 0; queue_rear--; queue_size = 0; } }
	int queue_size;
private:
	DataType data[QUEUE_SIZE];
	int queue_front, queue_rear;
};
template 
void CirQueue::EnQueue(DataType queue_index)//从对尾入队
{
	if (Full() == true) { std::cout << "队列已满,无法入列" << std::endl; }
	else
	{
		queue_rear = (queue_rear + 1) % QUEUE_SIZE;     //循环入队
		data[queue_rear] = queue_index;
		queue_size++;
	}
}
template 
void CirQueue::DeQueue()                    //对头元素出队
{
	if (Empty() == true) std::cout << "队列已空,无法出列" << std::endl;
	else
	{
		queue_front = (queue_front + 1) % QUEUE_SIZE; //循环出队
		std::cout << "已出列:" << data[queue_front] << std::endl;
		queue_size--;
	}

}
template 
DataType CirQueue::GetQueue()
{
	if (Empty() == true)
	{
		std::cout << "队列已空,队列中无数据" << std::endl;
		return -1;
	}
	else
	{
		int queue_temp = (queue_front + 1) % QUEUE_SIZE;
		std::cout << "队列头:" << data[queue_temp] << std::endl;
		return data[queue_temp];
	}
}
//主函数
int main()
{
	CirQueuequeue;

	int select = 10;
	int Item = 0;
	while (select)
	{
		std::cout << std::endl;
		cout << "*****************************" << endl;
		cout << "[1]入列          [2]出列    *" << endl;
		cout << "[3]获取对头元素  [4]清空队列*" << endl;
		cout << "[5]队列长度                 *" << endl;
		cout << "*****************************" << endl;
		cout << "请选择:>";
		cin >> select;
		switch (select)
		{
		case 1:
			cout << "请输入要入列的值:>";
			cin >> Item; getchar();
			queue.EnQueue(Item);
			cout << Item << " 已入列" << endl;
			break;
		case 2:
			queue.DeQueue();
			break;
		case 3:
			queue.GetQueue();
			break;
		case 4:
			queue.clearQueue();
			cout << "队列已清空" << endl;
			break;
		case 5:
			std::cout << "队列长度:" << queue.queue_size << std::endl;
			break;
		default:
			break;
		}
	}

	return 0;
}

链队列的模板类实现代码:

//链队列******************************************************************************
#include 

using namespace std;

#define QUEUE_SIZE 100

template 
class LinkQueueNode
{
public:
	DataType data;
	LinkQueueNode* next;
};
template 
class LinkQueue
{
public:
	LinkQueue();
	~LinkQueue(){}
	void EnQueue(DataType queue_index);
	void DeQueue();
	DataType GetQueue();
	bool Empty(){ if (queue_front == queue_rear) return true;else return false; }
	void clearQueue();
	int queue_size;
	
private:
	LinkQueueNode *queue_front, *queue_rear;
	DataType queue_first;
};

template 
LinkQueue::LinkQueue()
{
	LinkQueueNode *queue_node = (LinkQueueNode *)malloc(sizeof(LinkQueueNode));
	queue_node->next = NULL;
	queue_front = queue_rear = queue_node;
	queue_size = 0;
}

template 
void LinkQueue::EnQueue(DataType queue_index)//从对尾入队
{
	LinkQueueNode *addNode = (LinkQueueNode *)malloc(sizeof(LinkQueueNode));
	addNode->data = queue_index;
	addNode->next = NULL;
	queue_rear->next = addNode;
	queue_rear = addNode;
	queue_size++;
}

template 
void LinkQueue::DeQueue()                    //对头元素出队
{
	if ( Empty() ) std::cout << "队列已空,无法出列" << std::endl;
	else
	{
		DataType queue_x;
		LinkQueueNode *queue_p;
		queue_p = queue_front->next;
		queue_x = queue_p->data;
		queue_front->next = queue_p->next;
		if (queue_p->next == NULL)
			queue_rear = queue_front;
		delete queue_p;
		queue_size--;
		std::cout << "已出列:" << queue_x << std::endl;
	}
}

template 
DataType LinkQueue::GetQueue()
{
	if (Empty())
	{
		std::cout << "队列已空,队列中无数据" << std::endl;
		return -1;
	}
	else
	{
		std::cout << "对头:" << queue_front->next->data << std::endl;
		return queue_front->next->data;
	}
}

template 
void LinkQueue::clearQueue()
{
	while (queue_front != queue_rear) 
	{
		DataType queue_x;
		LinkQueueNode *queue_p;
		queue_p = queue_front->next;
		queue_x = queue_p->data;
		queue_front->next = queue_p->next;
		if (queue_p->next == NULL)
			queue_rear = queue_front;
		delete queue_p;
	}
	queue_size = 0;
}
//主函数
int main()
{
	LinkQueuequeue;

	int select = 10;
	int Item = 0;
	while (select)
	{
	    	std::cout << std::endl;
			cout << "*****************************" << endl;
			cout << "[1]入列          [2]出列    *" << endl;
			cout << "[3]获取对头元素  [4]清空队列*" << endl;
			cout << "[5]队列长度                 *" << endl;
			cout << "*****************************" << endl;
			cout << "请选择:>";
			cin >> select;
			switch (select)
			{
			case 1:
				cout << "请输入要入列的值:>";
				cin >> Item; getchar();
				queue.EnQueue(Item);
				cout << Item << " 已入列" << endl;
				break;
			case 2:
				queue.DeQueue();
				break;
			case 3:
				queue.GetQueue();
				break;
			case 4:
				queue.clearQueue();
				cout << "队列已清空" << endl;
				break;
			case 5:
				cout << "队列长度:" << queue.queue_size << endl;
				break;
			default:
				break;
			}
	}

	return 0;
}


你可能感兴趣的:(数据结构)