C++数据结构—队列的实现

队列篇

     1、采用循环队列,队列的顺序存储以一个一维数组和一个记录队列头元素位置的变量front和一个记录尾元素位置的变量rear来表示队列的入队和出队。

      加入一个元素rear+1,删除一个元素front+1.

     循环队列中,由于入队时尾指针向前追赶头指针;出队时头指针向前追赶尾指针,造成队空和队满时头尾指针均相等。因此,无法通过条件front==rear来判别队列是"空"还是"满"。

解决这个问题的方法至少有两种:

① 另设一布尔变量以区别队列的空和满;

②另一种方式就是数据结构常用的: 队满时:(rear+1)%n==front,n为队列长度(所用数组大小),由于rear,front均为所用空间的指针,循环只是逻辑上的循环,所以需要求余运算。如图情况,队已满,但是rear(5)+1=6!=front(0),对空间长度求余,作用就在此6%6=0=front(0)。

类型定义采用环状模型来实现队列,各数据成员的意义如下:

front指定队首位置,删除一个元素就将front顺时针移动一位;

rear指向元素要插入的位置,插入一个元素就将rear顺时针移动一位;

length存放队列中元素的长度

typedef struct{

int front;//头指针,队非空时指向队头元素

int rear;//尾指针,队非空时指向队尾元素的下一位置

int length;//计数器,记录队中元素总数DataTypedata[MaxSize];

}Queue;


2、程序如下

.h文件

由于使用类模板,所以声明与定义应放在同一h文件下

////////
//循环队列实现
//front指向首元素,删除一个元素front顺时针移动一位,rear指向尾元素,增加一个元素
//rear顺时针移动一位,length存储队列中元素的数目,
//front,为头指针,队非空时指向队头元素
//rear,为尾指针,队非空是指向队尾元素的下一个位置
//队列存储的元素个数为maxsize-1,
//当front和rear初值均赋值为-1时,此时front指向头元素前一个位置,
////////////
#ifndef QUEUE_H
#define QUEUE_H
#include<iostream>
#include<cassert>
using namespace std;
template<class T>
class Queue
{
public:
	Queue(int num);
	~Queue();
	bool isEmpty();
	void setEmpty();
	bool isFull();
	void en_Queue(const T &a);
	void de_Queue();
	void front_element();
	int QueueLength();
	void show();
private:
    int maxsize;
	int front;
	int rear;
	int m_length;
	T *myarray;
};

template<typename T>
Queue<T>::Queue(int num)
{
	maxsize = num;
	front = rear = 0;
	m_length = 0;
	myarray = new T[maxsize];
}

template<typename T>
Queue<T>::~Queue()
{
	delete[]myarray;
	myarray = NULL;
}

template<typename T>
bool Queue<T>::isEmpty()//判空
{
	if (front == rear)
	{
		return true;
	}
	else
	{
		return false;
	}
}
template<typename T>
void Queue<T>::setEmpty()//置空
{
	front = rear = 0;

}
//由于循环队列的循环只是逻辑上的循环,需要取余运算进行比较
template<typename T>
bool Queue<T>::isFull()//判满  
{
	//还有一种设一布尔值以区别
	if ((rear + 1) % maxsize == front)
	{
		return true;
	}
	else
		return false;
}
template<typename T>
void Queue<T>::en_Queue(const T &a)//入队
{
	if (isFull() == true)
	{
		cout << "Queue已满" << endl;
	}
	else
	{
		myarray[rear] = a;
		rear = (rear + 1) % maxsize;
	
		m_length++;
	}
}
template<typename T>
void Queue<T>::de_Queue()//出队
{
	if (isEmpty() == true)
	{
		cout << "Queue为空" << endl;
	}
	else
	{
		front = (front + 1) % maxsize;
		m_length--;
	}
}
template<typename T>
void Queue<T>::front_element()//取队列头元素
{
	if (isEmpty()==true)
	{
		cout << "Queue为空" << endl;
		
	}
	else
	{
		cout<<"队列头元素为:"<<myarray[front]<<endl;
	}
}

template<typename T>
void Queue<T>::show()//打印队列
{
	if (isEmpty()==true)
	{
		cout << "Queue为空" << endl;
	}
	else
	{
		cout << "队列元素为: ";
		for (int i = front; i < rear; i++)
		{
			cout << myarray[i] << "  ";
		}
		cout << endl;
	}
}
template<typename T>
int Queue<T>::QueueLength()//返回队列长度
{
	return m_length;
}
#endif

main函数测试

#include"Queue.h"
#include<iostream>
#include<cassert>
using namespace std;

int main()
{
	Queue<int> Q(5);
	/*Q.setEmpty();*/
	Q.en_Queue(1);
	Q.en_Queue(2);
	Q.en_Queue(3);
	Q.en_Queue(4);

	Q.show();
	Q.en_Queue(7);
	//Q._Queue();
	Q.show();
	Q.front_element();
	cout<<"队列长度:"<<Q.QueueLength()<<endl;
	system("pause");
	return 0;
}

3、运行结果

猜你喜欢

转载自blog.csdn.net/zlb666/article/details/81986151