C++_013_数据结构_队列类

包含的主要知识点

1.队列类的写法。
2.深拷贝函数,深拷贝构造函数。
3.operator=的深拷贝重载。

运行截图



队列类头文件

#pragma once
#include<iostream>
#include<ostream>
using namespace std;
#define MaxSize 100//规定100 个为最大容量。

using namespace std;

template<typename T>
class Queue;

template<typename T>
ostream& operator <<(ostream &out, Queue<T> a);

template<typename T>
struct Node;

template<typename T>
ostream& operator <<(ostream &out, Node<T> a);
//队列只要头尾的操作,没必要写成双链表。这里我写成双链表形式,只是为了展示双链表在应用中的写法罢了。
template<typename T>
struct Node
{
	T Data;
	Node<T> * Front;
	Node<T> * Next;
	friend ostream& operator << <>(ostream &out, Node<T> a);
};

//不带头结点的写法。
template<typename T>
class Queue
{
public:
	Queue();//默认构造函数。
	Queue(T a[], int length);//含参构造函数。
	Queue(Queue<T> &a);//深拷贝构造函数。
	~Queue();//析构函数。
private:
	Node<T> *First;//有头指针,就可以不用头结点了。
	Node<T> *Tail;//队尾。
	int Size;//队长。
public:
	void InitQueue();//初始化队列。
	void DestroyQueue();//销毁队列。使之只剩下头结点。
	void EnQueue(T Obj);//将 Obj 插入队列。
	T DeQueue();//删除队首。
	T GetFront();//获取队首元素。
	bool IsEmpty();//判断队列是否为 空。
	bool IsFull();//判断队列是否满。
	int GetSize();//获取队列元素个数。
	friend ostream& operator << <> (ostream &out, Queue<T> a);
	Queue<T>& operator = (Queue<T> &a);
};

template<typename T>
ostream& operator << (ostream &out, Queue<T> a)
{
	int count = a.Size;
	Node<T> *p = a.First;
	while (count-- > 0)
	{
		out << p->Data << "  ";
		p = p->Next;
	}
	out << endl;
	return out;
}

template<typename T>
inline ostream& operator << (ostream &out, Node<T> &a)
{
	out << a.Data;
}

队列类源文件

#include "stdafx.h"
#include "Queue.h"

template<typename T>//无参数构造函数
Queue<T>::Queue()
{
	Node<T> *NewNode = new Node<T>;
	Tail = First = NewNode;
	NewNode->Front = NewNode->Next = NewNode;
	Size = 0;
}

template<typename T>//含参数构造函数
Queue<T>::Queue(T a[], int length)
{
	if (length > MaxSize || length<1)
	{
		throw "Too many or less nodes.\n";
	}

	Node<T> *NewNode = new Node<T>;
	NewNode->Front = NewNode->Next = NewNode;
	Tail = First = NewNode;//队首指针为此地址的下一个。
	for (this->Size = 0; this->Size < length; this->Size++)
	{
		NewNode = new Node<T>;
		NewNode->Data = a[this->Size];
		NewNode->Front = Tail;
		NewNode->Next = First;
		Tail->Next = NewNode;
		Tail = NewNode;
		First->Front = NewNode;
	}
	First = First->Next;
};

template<typename T>//默认深拷贝构造函数
Queue<T>::Queue(Queue<T>& a)
{
	Node<T> *cursor = a.First;
	Node<T> *NewNode = new Node<T>;
	Tail = First = NewNode;
	NewNode->Front = NewNode->Next = NewNode;

	for (this->Size = 0; this->Size < a.Size; this->Size++)
	{
		NewNode = new Node<T>;
		NewNode->Data = cursor->Data;

		NewNode->Front = Tail;
		NewNode->Next = First;
		Tail->Next = NewNode;
		Tail = NewNode;
		First->Front = NewNode;
		cursor = cursor->Next;
	}
	First = First->Next;
};

template<typename T>
Queue<T>::~Queue()
{
	Node<T> *cursor = First = First->Front;
	while (Size-- >0)
	{
		First = cursor->Next;
		delete cursor;
		cursor = First;
	}
	delete cursor;
}

template<typename T>
inline void Queue<T>::InitQueue()
{
	cout << "初始化队列,这个函数暂时用不到初始化的功能" << endl;
}

template<typename T>
void Queue<T>::DestroyQueue()
{
	Node<T> *cursor = First;
	while (Size-- > 0)
	{
		First = cursor->Next;
		delete cursor;
		cursor = First;
	}
	First->Next = First->Front = First;
}

template<typename T>
void Queue<T>::EnQueue(T Obj)
{
	if (this->Size + 1 > MaxSize)
	{
		cout << "循环队列满了不能插入" << endl;
		return;
	}
	Node<T> * NewNode = new Node<T>;
	NewNode->Data = Obj;
	NewNode->Front = Tail;
	NewNode->Next = Tail->Next;
	Tail->Next = NewNode;
	Tail = NewNode;
	Tail->Next->Front = NewNode;
	Size++;
}

template<typename T>
T Queue<T>::DeQueue()
{
	if (Size <= 0)
	{
		throw"there is too less data to be delete";
	}
	Node<T> *cursor = First;
	T obj = First->Data;
	First = First->Next;
	cursor->Front->Next = cursor->Next;
	cursor->Next->Front = cursor->Front;
	delete cursor;
	Size--;
	return obj;
}

template<typename T>
T Queue<T>::GetFront()
{
	return First->Data;
}

template<typename T>
bool Queue<T>::IsEmpty()
{
	return Size > 0 ? false : true;
}

template<typename T>
bool Queue<T>::IsFull()
{
	return Size >= 100 ? true : false;
}

template<typename T>
int Queue<T>::GetSize()
{
	return Size;
}

template<typename T>
Queue<T>& Queue<T>::operator=(Queue<T>& a)
{
	Queue<T> RETURN;
	RETURN.First = RETURN.Tail = new Node<T>;
	NewNode->Front = NewNode->Next = NewNode;
	Node<T> *cursor = a.First;
	Node<T> *NewNode;
	for (RETURN.Size = 0; RETURN.Size<a.Size; RETURN.Size++)
	{
		NewNode = new Node<T>;
		NewNode->Data = cursor->Data;

		NewNode->Front = RETURN.Tail;
		NewNode->Next = RETURN.Tail->Next;
		RETURN.Tail->Next = NewNode;
		RETURN.Tail = NewNode;
		RETURN.Tail->Next->Front = NewNode;
		cursor = cursor->Next;
	}
	RETURN.First = RETURN.First->Next;
	return RETURN;
}

主函数验证

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

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

#include "stdafx.h"
#include<iostream>
#include"Queue.cpp"
#include<ostream>
using namespace std;

int main()
{
	int arr[3] = { 1,2,3 };
	Queue<int> a(arr,3);
	cout <<"原始队列a为:  "<< a;
	Queue<int> b(a);
	cout << "拷贝构造函数b: "<<b;
	
	a.EnQueue(4);
	cout << "插入元素4 后: " << a;
	a.EnQueue(5);
	cout<< "插入元素5后: " << a;
	a.DeQueue();
	cout << "队首出队后:" << a;

	Queue<int> c = a;
	cout << "opeartor=函数c: " << c;
	c.DestroyQueue();
	

	getchar();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值