数据结构与算法分析-C++描述 第3章 队列ADT(循环队列的两种实现)

         像栈一样,队列(queue)也是一种表,与栈不同的是,队列的进队(enQueue)是在尾部(rear)进行,队列的出队(deQueue)是在头部(front)进行。

         队列的实现:1)队列的数组实现;2)队列的链表表实现;

        循环队列:解决队列空指针和内存空置问题,提高空间利用率;

                          判断为空: front == rear

                         判断队满:front == (rear + 1) % maxSize

                         获取大小:(rear - front + maxSize)% maxSize

        队列的应用:1)常见的排队算法;2)打印机打印文字;3)计算机网络请求等;4)图论算法

        实例:使用数组和链表实现循环队列(注:用单链表表示的链式队列特别适合于数据元素变动较大的情形,而且不存在溢出的情况)

       参考链接:循环队列和链式队列(C++实现)

1、seqQueue.h

//seqQueue.h
#ifndef SEQQUEUE_H_
#define SEQQUEUE_H_

#include

template
class SeqQueue{
private:
	dataType* data;
	int front, rear;
	int maxSize;
public:
	SeqQueue(int max = 10);
	~SeqQueue(){delete[] data;};
	bool deQueue(dataType &d);
	bool enQueue(const dataType &d);
	void clear(){front = rear = 0;};
	bool isEmpty() const{return front == rear;};
	bool isFull() const{return front == (rear + 1) % maxSize;}; 
	int getSize() const{return (rear - front + maxSize) % maxSize ;};
};

//SeqQueue constructor
template
SeqQueue::SeqQueue(int max){
	data = new dataType[max];
	front = rear = 0;
	maxSize = max;
}

//deQueue the data
template
bool SeqQueue::deQueue(dataType &d){
	if(isEmpty()){
		return false;
	}
	else{
		d = data[front];
		front = (front + 1) % maxSize;
		return true;
	}
}

//enQueue the data
template
bool SeqQueue::enQueue(const dataType &d){
	if(isFull()){
		return false;		
	}
	else{
		data[rear] = d;
		rear = (rear + 1) % maxSize;
		return true;
	}
}

#endif

2、linkQueue.h

//linkQueue.h
#ifndef LINKQUEUE_H_
#define LINKQUEUE_H_

#include
template
struct Node{
	dataType data;
	Node* next;
	Node(dataType &d, Node *n = NULL):data(d), next(n){};
};

template
class LinkQueue{
private:
	Node *front, *rear;
public:
	LinkQueue():front(NULL), rear(NULL){};
	~LinkQueue(){clear();};
	void clear();
	int getSize();
	bool isEmpty();
	bool deQueue(dataType &d);
	bool enQueue(dataType &d);
};

//clear the Node data
template
void LinkQueue::clear(){
	Node *temp;
	while(front != NULL){
		temp = front;
		front = front -> next;
		delete front;
	}
}

//get size of LinkQueue
template
int LinkQueue::getSize(){
	int num = 0;
	Node *temp = front;
	while(temp != NULL){
		num++;
		temp = temp -> next;
	}
	return num;
}

//judge whether the LinkQueue is empty
template
bool LinkQueue::isEmpty(){
	return front == NULL;
}

//deQueue the data
template
bool LinkQueue::deQueue(dataType &d){
	if(isEmpty()){
		return false;
	}
	Node *temp = front;
	d = front -> data;
	front = front -> next;
	delete temp;
	return true;
}

//enQueue the data
template
bool LinkQueue::enQueue(dataType &d){
	if(front == NULL){
		front = rear = new Node(d);
	}
	else{
		rear = rear -> next = new Node(d);
	}
	return true;
}

#endif

3、main.cpp

//main.cpp
#include
#include
#include"seqQueue.h"
//#include"linkQueue.h"

using namespace std;

void menu(){
	cout << "************ *MENU* *************" << endl;
	cout << "********* *1 enQueue* ***********" << endl;
	cout << "********* *2 deQueue* ***********" << endl;
	cout << "********* *3 getSize* ***********" << endl;
	cout << "********* *4 clear* *************" << endl;
	cout << "********* *5 exit* **************" << endl;
}

//void excute(int num, LinkQueue *q)
void excute(int num, SeqQueue *q){
	switch(num){
		int temp;
		case 1 : 
			cout << "Enter the enQueue number : ";
			cin >> temp;
			q -> enQueue(temp);
			cout << temp << " has been enQueued " << endl;
			break;
		case 2 :
			q -> deQueue(temp);
			cout << "deQueue the number into temp : " << temp << endl;
			break;
		case 3 :
			cout << "the queue size is : " << q -> getSize() << endl;
			break;
		case 4 :
			cout << "I have clear the queue " << endl;	
			break;
		case 5 :
			cout << "I will exit the program !" << endl;
			exit(1);
		default:
			cout << "only 1 ~ 5 is avaible, Wrong operation !" << endl;
			exit(1);
	}
}

int main()
{
    //LinkQueue *queue = new LinkQueue
	SeqQueue *queue = new SeqQueue;
	menu();
	int temp;
	while(true){
		cout << "choise your operation : ";
		cin >> temp;
		cin.get();
		excute(temp, queue);
	}
	cout << "done . " << endl;
	delete[] queue;
	return 0;
}

practice makes perfect!

你可能感兴趣的:(C++,数据结构与算法分析-C++描述)