數據結構學習:c++實現隊列的順序存儲結構

本部分內容是用c++實現隊列的順序存儲結構:

一、首先我們要弄明白的幾個內容:

1、實現隊列的順序存儲結構是非常的簡單的,是用固定長度的數組進行實現。

2、明白兩個指針:front和rear。

front:隊頭指針,rear:隊尾指針;tips:rear不一定大於front.如下圖:

在這裏插入圖片描述

3、三個等式:

(1)隊列爲空的條件:

 front == rear

(2)隊列滿的條件:

(rear + 1)% QueueSize == front

(3)隊列長度計算:

 size = (rear - front + QueueSize) % QueueSize

二、實現代碼:

1、首先是頭文件(類定義)部分:

#pragma once
#include <iostream>
#define QUEUESIZE 100
using namespace std;

/******************************
 *c++實現隊列的順序存儲結構
 *******************************/

template <class ElemType>
class Queue
{
public:
	Queue();                          //構造和析構
	~Queue(); 
	int getSize();                    //獲取長度
	void enQueue(ElemType &e);        //隊尾插入元素
	void deQueue();                   //刪除隊頭元素
	void display() const;             //輸出整個隊列元素
	bool isEmpty() const;             //判斷是否爲空隊
	bool isFull() const;              //判斷隊是否已滿

private:
	ElemType queue[QUEUESIZE]; 
	int front;                //隊頭、隊尾指針      
	int rear;                 
	int m_size;               //隊列長度
};

2、其次是函數定義部分:

#include "Queue.h"

//構造函數

template <class ElemType>
Queue<ElemType>::Queue():front(0),rear(0),m_size(0){
}

//析構函數

template <class ElemType>
Queue<ElemType>::~Queue(){
}

//獲取隊列的長度

template<class ElemType>
int Queue<ElemType>::getSize(){
	this->m_size = (rear - front + QUEUESIZE) % QUEUESIZE;
	return m_size;
}

//隊尾插入元素

template<class ElemType>
void Queue<ElemType>::enQueue(ElemType &e){
	if ((rear + 1) % QUEUESIZE == front) {
		cout << "隊列已滿,不能插入新元素" << endl;
		return;
	}else {
		queue[rear] = e;
		rear = (rear + 1) % QUEUESIZE;  //指針向後移一個位置
	}
}

//刪除隊頭元素

template<class ElemType>
void Queue<ElemType>::deQueue(){
	if (front == rear) {
		cout << "空隊列,無元素" << endl;
		return;
	}
	ElemType e;
	e = queue[front];
	this->front = (this->front + 1) % QUEUESIZE;
}

//輸出整個隊列元素

template<class ElemType>
void Queue<ElemType>::display() const{
	if (front == rear) {
		cout << "隊列中無元素" << endl;
		return;
	}else if (rear > front) {
		for (size_t i = front; i < rear; ++i) 
			cout << queue[i] << " ";
	}else {
		for (size_t j = front; j < QUEUESIZE -1; ++j)
			cout << queue[j] << " ";
		for (size_t k = 0; k < rear; ++k)
			cout << queue[k] << " ";
	}
}

//判斷隊空

template<class ElemType>
bool Queue<ElemType>::isEmpty() const {
	if (this->m_size == 0)
		return true;
	return false;
}

//判斷隊滿

template<class ElemType>
bool Queue<ElemType>::isFull() const{
	if (this->m_size == 0)
		return false;
	return true;
}

3、最後是函數測試部分(main函數):

#include "Queue.cpp"

int main()
{
	Queue<int> queue;
	cout << "*********************" << endl;
	cout << "新初始化的隊列的長度爲:" << queue.getSize() << endl;
	cout << "隊是否爲空:" << boolalpha << queue.isEmpty() << endl;
	cout << "*********************" << endl;
	for (int i = 0; i < 10; ++i)
		queue.enQueue(i);
	cout << "插入元素後隊列的長度:" << queue.getSize() << endl;
	cout << "隊是否爲空:" << boolalpha << queue.isEmpty() << endl;
	cout << "整個隊列元素:" << endl;
	queue.display();
	cout << endl;
	cout << "*********************" << endl;
	queue.deQueue();
	cout << "刪除元素後的的隊列的長度:" << queue.getSize() << endl;
	cout << "隊是否爲空:" << boolalpha << queue.isEmpty() << endl;
	cout << "整個隊列元素:" << endl;
	queue.display();
	cout << endl;
	system("pause");
	return 0;
}

4、最後是測試結果:

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章