【数据结构】顺序队列的实现(c++)

前端之家收集整理的这篇文章主要介绍了【数据结构】顺序队列的实现(c++)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

文件


#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;
}



清空:




获得队头元素:




队列长度:




出队:




入队:




退出系统:


原文链接:/datastructure/382649.html

猜你在找的数据结构相关文章