数据结构之数组存储循环队列(C++实现)

数据结构之数组存储循环队列(C++实现)

  本实验程序用于验证循环队列的基本操作算法,包括:入队、出队、取队头元素、取队尾元素、判队空或满、显示队列元素等。为了用户了解循环队列的循环特性,在基本操作中,增加了显示队尾或对头指针内容的功能。

附循环队列逻辑结构图

数据结构之数组存储循环队列(C++实现)_第1张图片

    循环队列的定义及操作(CirQueue.h)如下:

#pragma once
template
class CirQueue
{
private:
	T *base;                             //储存空间基址
	int front;                           //对头指针
	int rear;                            //队尾指针
	int queuesize;                        //队列容量
public:
	CirQueue(int m);                     //构造空队列
	~CirQueue();                         //析构函数,释放链队各节点存储空间
	void EnQueue(T x);
	T DeQueue();
	T GetHead();
	T GetLast();
	int QueueEmpty();
	int QueueFull();
	void ClearQueue();
	void Pointer();
	void QueueTranverse();
};

template
inline CirQueue::CirQueue(int m)
{
	base = new T[m];
	if (base == NULL)
	{
		cout << "队列创建失败,退出!";
		exit(1);
	}
	front = rear = 0;
	queuesize = m;
}

template
inline CirQueue::~CirQueue()
{
	delete[] base;
	rear = 0;
	front = 0;
	queuesize = 0;

}

template
inline void CirQueue::EnQueue(T x)
{
	if ((rear + 1) % queuesize == front) throw"上溢,无法入队!";
	base[rear] = x;
	rear = (rear + 1) % queuesize;
}

template
inline T CirQueue::DeQueue()
{
	T x;
	if (front == rear) throw"下溢,无法出队!";
	x = base[front];
	front = (front + 1) % queuesize;
	return x;
}

template
inline T CirQueue::GetHead()
{
	T x;
	if (front == rear)throw"队空!";
	x = base[front];
	return x;
}

template
inline T CirQueue::GetLast()
{
	T x;
	if (front == rear)throw"队空!";
	x = base[rear-1];
	return x;
}

template
inline int CirQueue::QueueEmpty()
{
	if (front == rear)
		return 1;
	else
		return 0;
}

template
inline int CirQueue::QueueFull()
{
	if ((rear + 1) % queuesize == front)
		return 1;
	else
		return 0;
}

template
inline void CirQueue::ClearQueue()
{
	front = rear = 0;
}

template
inline void CirQueue::Pointer()
{
	cout << "队首front=" << front << endl;
	cout << "队尾rare=" << rear << endl;

}

template
inline void CirQueue::QueueTranverse()
{
	int i = front;
	while (i != rear)
	{
		cout << base[i] << '\t';
		i = (i + 1) % queuesize;
	}
	cout << endl;
}
程序结构图如下:

数据结构之数组存储循环队列(C++实现)_第2张图片CirQueue_main主调程序


// CirQueue.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"


#include
#include
#include
#include"CirQueue.h"
using namespace std;
char pause;
int main()
{
	int e;
	CirQueueq(10);
	system("cls");
	int choice;
	do
	{
		cout << "1-元素入队\n";
		cout << "2-元素出队\n";
		cout << "3-取队头元素\n";
		cout << "4-取队尾元素\n";
		cout << "5-置队空\n";
		cout << "6-测队空\n";
		cout << "7-测队满\n";
		cout << "8-显示首尾位置\n";
		cout << "9-输出队元素\n";
		cout << "10-退出\n";
		cout <<"Enter Choice:";
		cin >> choice;
		switch (choice)
		{
		case 1:
			cout << "请输入要插入的元素值:";
			cin >> e;
			cout << endl;
			q.EnQueue(e);
			cout << e << "如对成功!" << endl;
			cin.get(pause);
			system("pause");
			break;
		case 2:
			try {
				e = q.DeQueue();
				cout << "出队元素为:" << e << endl;
			}
			catch (char *eer)
			{
				cout << eer << endl;
			}
			cin.get(pause);
			system("pause");
			break;
		case 3:
			try {
				e = q.GetHead();
				cout << "队头元素为<
编译环境:windows 7;VisualStdio2015



你可能感兴趣的:(数据结构,数据结构,C++,循环队列)