【資料結構】順序佇列的實現(c++)

zhaoyaqian552發表於2015-05-28

標頭檔案:


#pragma once

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

template<class Type>
class SeqQueue
{
public:
	SeqQueue(size_t sz = INIT_SZ);
	~SeqQueue();
public:
	bool empty()const;
	bool full()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>
SeqQueue<Type>::SeqQueue(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>
SeqQueue<Type>::~SeqQueue()
{
	destory();
}

// 判斷佇列是否滿了,順序佇列呈現虛滿的狀態
template<class Type>
bool SeqQueue<Type>::full()const
{
	return (tail >= capacity);
}

// 判斷佇列是否為空
template<class Type>
bool SeqQueue<Type>::empty()const
{
	return (tail == 0);
}

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

// 入隊
template<class Type>
bool SeqQueue<Type>::push(const Type &x)
{
	if (full())
	{
		cout << "the queue is full,can not enter!" << endl;
		return false;
	}
	else
	{
		base[tail] = x;
		tail++;
		return true;
	}
}

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

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

// 求佇列長度
template<class Type>
int SeqQueue<Type>::length()const
{
	return (tail - head);
}

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

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

// 摧毀佇列
template<class Type>
void SeqQueue<Type>::quit_system(Type &x)
{
	x = 0;
}



主函式:


#include "SeqQueue.h"

int main()
{
	SeqQueue<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;
}



清空:




獲得隊頭元素:




佇列長度:




出隊:




入隊:




退出系統:



相關文章