鏈隊列的簡單實現

隊列(queue)是一種先進先出(first in first out,FIFO)的線性表,只允許在一端進行插入(入隊)操作,在另一端進行刪除(出隊)操作。允許入隊操作的一端稱爲隊尾,允許出隊操作的一端稱爲隊頭。

一個鏈隊列應有兩個分別指向隊頭和隊尾的指針。如果從隊列中退出一個元素,必須從單鏈表的第一個結點中取出隊頭元素,並刪除此結點,而入隊的新元素是存放在隊尾處的,也就是單鏈表的最後一個元素的後面,並且吃結點將成爲新的隊尾。

說明:鏈隊列適合數據元素個數變動比較大的情形,一般不存在溢出的問題,如果程序中要使用多個隊列,最好使用鏈隊列,這樣將不會出現存儲分配的問題,也不必進行數據元素的移動。

LinkQueue.h

#ifndef LINKQUEUE_H_
#define LINKQUEUE_H_
#include <iostream>
using std::cout;
using std::endl;
using std::ostream;
template<typename T>
struct Node
{
	T data;
	Node<T> *next;
	Node();
	Node(T item, Node<T> *link=NULL);
};
template <typename T>
Node<T>::Node()
{
	next=NULL;
}
template <typename T>
Node<T>::Node(T item, Node<T> *link=NULL)
{
	data=item;
	next=link;
}
template <typename T>
class LinkQueue
{
protected:
	Node<T> *front, *rear;
	void Init();
public:
	LinkQueue();
	virtual ~LinkQueue();
	int Length() const;
	bool IsEmpty();
	void Clear();
	void OutQueue(T &e);
	void GetHead(T &e) const;
	LinkQueue<T> &InQueue(const T &e);
	LinkQueue(const LinkQueue<T> &copy);
	LinkQueue<T> &operator=(const LinkQueue<T> &copy);
};
template <typename T>
void LinkQueue<T>::Init()
{
	front=new Node<T>;
	rear=front;
}
template<typename T>
LinkQueue<T>::LinkQueue()
{
	Init();
}
template<typename T>
LinkQueue<T>::~LinkQueue()
{
	Clear();
	delete front;
}
template <typename T>
int LinkQueue<T>::Length() const
{
	Node<T> *tempPtr;
	int count=0;
	for(tempPtr=front; tempPtr!=NULL; tempPtr=tempPtr->next)
	{
		++count;
	}
	return (count-1);
}
template <typename T>
bool LinkQueue<T>::IsEmpty()
{
	return front==rear;
}
template <typename T>
void LinkQueue<T>::Clear()
{
	Node<T> *tempPtr;
	while(front->next!=NULL)
	{
		tempPtr=front->next;
		front->next=tempPtr->next;
		delete tempPtr;
	}
}
template <typename T>
void LinkQueue<T>::OutQueue(T &e)
{
	Node<T> *tempPtr=front;
	if(front->next!=NULL)
	{
		tempPtr=front->next;
		e=tempPtr->data;
		front->next=tempPtr->next;
		delete tempPtr;
	}
	else
	{
		cout<<"隊列爲空,沒有數據出隊!"<<endl;
	}
}
template <typename T>
void LinkQueue<T>::GetHead(T &e) const
{
	Node<T> *tempPtr=front;
	if(front->next!=NULL)
	{
		tempPtr=front->next;
		e=tempPtr->data;
	}
	else
	{
		cout<<"隊列爲空,沒有數據出隊!"<<endl;
	}
}
template <typename T>
LinkQueue<T> &LinkQueue<T>::InQueue(const T &e)
{
	Node<T> *newPtr=new Node<T>;
	newPtr->data=e;
	rear->next=newPtr;
	rear=newPtr;
	return *this;
}
template <typename T>
LinkQueue<T>::LinkQueue(const LinkQueue<T> &copy)
{
	Init();
		Node<T> *tempPtr;
		T e;
		for(tempPtr=copy.front->next; tempPtr!=NULL; tempPtr=tempPtr->next)
		{
			e=tempPtr->data;
			InQueue(e);
		}
}
template <typename T>
LinkQueue<T> &LinkQueue<T>::operator=(const LinkQueue<T> &copy)
{
	if(&copy != this)
	{
		Init();
		Node<T> *tempPtr;
		T e;
		for(tempPtr=copy.front->next; tempPtr!=NULL; tempPtr=tempPtr->next)
		{
			e=tempPtr->data;
			InQueue(e);
		}
	}
	return *this;
}
template <typename T>
ostream &operator <<(ostream &os, LinkQueue<T> &LQ)
{
	T e;
	cout<<"隊列爲:";
	int Len=LQ.Length();
	for(int i=0; i<Len; i++)
	{
		LQ.OutQueue(e);
		cout<<e<<" ";
	}
	cout<<endl;
	return os;
}
#endif

LinkQueue.cpp

// LinkQueue.cpp : 定義控制檯應用程序的入口點。
//

#include "stdafx.h"
#include "LinkQueue.h"
#include<iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	LinkQueue<int> LQ;
	LQ.InQueue(1);
	LQ.InQueue(2);
	LQ.InQueue(3).InQueue(4).InQueue(5);
	int e;
	LQ.GetHead(e);
	LinkQueue<int> copy(LQ);
	cout<<"複製的隊列1"<<copy;
	copy=LQ;
	cout<<"複製的隊列2"<<copy;
	cout<<"頭結點"<<e<<endl;
	cout<<LQ;
	system("pause");
	return 0;
}

結果略!
發佈了70 篇原創文章 · 獲贊 44 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章