微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

【数据结构】顺序队列的实现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;
}



清空:




获得队头元素:




队列长度:




出队:




入队:




退出系统:


原文地址:https://www.jb51.cc/datastructure/382649.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐