【資料結構】迴圈佇列的實現(c++)

zhaoyaqian552發表於2015-05-28

標頭檔案:


#pragma once

#include <iostream>
#include <assert.h>
using namespace std;

template<class Type>
class CQueue
{
public:
	CQueue(size_t sz = INIT_SZ);
	~CQueue();
public:
	bool full()const;
	bool empty()const;
	void show()const;
	bool push(const Type &x);
	bool pop();
	void gettop(Type &x);
	int length()const;
	void clear();
	void destory();
	void quit_system(Type &x);
private:
	enum{ INIT_SZ = 8 };
	Type *base;
	int capacity;
	int head;
	int tail;
};

template<class Type>
CQueue<Type>::CQueue(size_t sz = INIT_SZ)
{
	capacity = sz > INIT_SZ ? sz : INIT_SZ;
	base = new Type[capacity];
	assert(base != NULL);
	head = 0;
	tail = 0;
}

template<class Type>
CQueue<Type>::~CQueue()
{
	destory();
}

template<class Type>
bool CQueue<Type>::full()const
{
	// 浪費一個空間
	if ((tail + 1) % capacity == head)
		return true;
	else
		return false;
}

template<class Type>
bool CQueue<Type>::empty()const
{
	return (tail == head);
}

// 顯示
template<class Type>
void CQueue<Type>::show()const
{
	if (tail == head)
	{
		cout << "the queue is empty!" << endl;
		return;
	}
	for (int i = head; i != tail; i = (i + 1) % capacity)
	{
		cout << base[i] << endl;
	}
}

// 入隊
template<class Type>
bool CQueue<Type>::push(const Type &x)
{
	if (full())
	{
		cout << "the queue is full,can not enter!" << endl;
		return false;
	}
	else
	{
		base[tail] = x;
		tail = (tail + 1) % capacity;// 讓佇列迴圈起來
		return true;
	}
}

// 出隊
template<class Type>
bool CQueue<Type>::pop()
{
	if (empty())
	{
		cout << "the queue is empty,can not pop!" << endl;
		return false;
	}
	else
	{
		head = (head + 1) % capacity;
		return true;
	}
}

// 獲得隊頭
template<class Type>
void CQueue<Type>::gettop(Type &x)
{
	x = base[head];
}

// 佇列的長度
template<class Type>
int CQueue<Type>::length()const
{
	if ((tail - head) > 0)
		return (tail - head);
	else
		return(head - tail);
}

// 清空佇列
template<class Type>
void CQueue<Type>::clear()
{
	head = tail = 0;
}

// 摧毀佇列
template<class Type>
void CQueue<Type>::destory()
{
	delete[]base;
	base = NULL;
	capacity = head = tail = 0;
}

// 退出系統
template<class Type>
void CQueue<Type>::quit_system(Type &x)
{
	x = 0;
}



主函式:


#include "CQueue.h"

int main()
{
	CQueue<int> myqueue;
	int input = 1;
	int value;
	while (input)
	{
		cout << "****************************************************" << endl;
		cout << "*       [1] show                 [2] push          *" << endl;
		cout << "*       [3] pop                  [4] gettop        *" << endl;
		cout << "*       [5] length               [6] clear         *" << endl;
		cout << "*       [7] destory              [8] quit_syntem   *" << endl;
		cout << "****************************************************" << endl;
		cout << "please choose:";
		cin >> input;
		switch (input)
		{
		case 1:
			myqueue.show();
			break;
		case 2:
			cout << "please enter the number:";
			while (cin >> value, value != -1)
			{
				myqueue.push(value);
			}
			break;
		case 3:
			myqueue.pop();
			break;
		case 4:
			myqueue.gettop(value);
			cout << value << endl;
			break;
		case 5:
			cout << myqueue.length() << endl;
			break;
		case 6:
			myqueue.clear();
			break;
		case 7:
			myqueue.destory();
			break;
		case 8:
			myqueue.quit_system(input);
			break;
		default:
			break;
		}
	}
	return 0;
}




清空:




獲得隊首元素:




求長度:




出隊:




入隊:




退出系統:



相關文章