队列记住的是back的地址
构造时back指向back,表示空队列;
#include<iostream>
#include<ctime>//...
#include<stdlib.h>//随机数要的
using namespace std;
template<typename T>
class listNode//只是一个节点
{
public:
T NodeData;
listNode *next;
listNode(T data, listNode * ne){ NodeData = data; next = ne; }
listNode(){ NodeData = NULL; next = NULL; }
};
template<typename T>
class Queue
{
public:
Queue();
Queue(const Queue<T> & or);
void enqueue(const T & item);//
void dequeue();//
const T & front() const;
bool empty() const;//
void display() const;
Queue & operator =(const Queue<T> &or);
~Queue();
private:
listNode<T>* myBack;
};
template<typename T>
Queue<T>::Queue(){ myBack = new listNode<T>(); myBack->next = myBack; }//这个myback已经成了最简单的循环列表,指向它自己,这个myback都是留着不放东西的,也将是是否为空的判断依据!
template<typename T>
Queue<T>::Queue(const Queue<T>& or)
{
myBack = new listNode<T>();
myBack->next = myBack;
listNode<T> *pr = or.myBack->next,*pt=myBack->next;
myBack->NodeData = or.myBack->NodeData;
myBack->next = myBack;
while (pr !=or.myBack)
{
pt = new listNode<T>(NULL, myBack->next);
myBack->NodeData = pr->NodeData;
myBack->next = pt;
myBack = pt;
pr = pr->next;
}
}
template<typename T>
Queue<T>::~Queue()
{
listNode<T> *pr=myBack->next,*pt;
while (pr != myBack)
{
pt = pr->next;
delete pr;
pr = pt;
}
}
template<typename T>
void Queue<T>::enqueue(const T & item)//添加咯,感觉是除了构造最重要的函数
{ //这里是这样的:先新建一个节点记住myback的next,这样才能保证是个循环的圈~(因为最后这个节点会变成myback);
listNode<T> *pr;
pr = new listNode<T>(NULL, myBack->next);//记住myback的下一个就是第一个的地址;
myBack->NodeData = item;//把要添加的节点的值赋给当前的最后一个;
myBack->next = pr;//指向新节点,将是新的myback;
myBack = pr;//这样myback就后移了
}//其实就是将新节点插在myback前面,而且没有遍历链表。
template<typename T>
bool Queue<T>::empty() const { return myBack == myBack->next; }//就和刚构造的时候一样,当就剩下一个myback的时候哦就是空了;
template<typename T>
void Queue<T>::dequeue()//删除第一个节点。也就是myback间隔跳了一个,然后哦把跳过的那一个的空间给还了;
{
listNode<T> *pr;
try
{
if (empty())//先判断是不是空的,空的不能删,给它报错
throw out_of_range("Queue<>::dequeue() :empty queue");
else
{
pr = myBack->next;//pr记住myback的next(其实就是第一个了),为了跳过第一个有了第二句。。
myBack->next = pr->next;//跳过了
delete pr;//还空间
}
}
catch (exception const& ex)//抓异常的
{
cerr << "Exception: " << ex.what() << endl;
}
}
template<typename T>
const T & Queue<T>::front() const
{
listNode<T> *pr;
pr = myBack->next;
try
{
if (empty())
throw out_of_range("Queue<>::dequeue() :empty queue");
else
return pr->NodeData;
}
catch (exception const& ex)
{
cerr << "Exception: " << ex.what() << endl;
}
}
template<typename T>
void Queue<T>::display() const
{
listNode<T> *pr=myBack->next;
if (empty())
{
cout << "Queue<>::dequeue() :empty queue\n";
return;
}
while (pr!=myBack)
{
cout << pr->NodeData<<" ";
pr = pr->next;
}
cout << endl;
}
template<typename T>
Queue<T> & Queue<T>::operator =(const Queue<T> &or)
{
Queue<T>::~Queue();
myBack = new listNode<T>();
myBack->next = myBack;
listNode<T> *pr = or.myBack->next, *pt = myBack->next;
myBack->NodeData = or.myBack->NodeData;
myBack->next = myBack;
while (pr != or.myBack)
{
pt = new listNode<T>(NULL, myBack->next);
myBack->NodeData = pr->NodeData;
myBack->next = pt;
myBack = pt;
pr = pr->next;
}
return *this;
}
//测试文件
#include"Queue.h"
using namespace std;
int main()
{
Queue<int> que;
int i = 1;
int num;
srand(unsigned int(time(0)));
cout << "随机产生的从0~99的数字20个:" << endl;
for (; i <= 20; i++)
{
num = rand() % 100;
cout << num << " ";
que.enqueue(num);
}
cout << endl<<endl<<endl;
system("pause");
que.display();
system("pause");
Queue<int> que2(que), que3 = que;
//que2.display();
//que3.display();
//system("pause");
cout << "开始删除\n";
while (!que.empty())
{
cout << que.front() <<endl;
que.dequeue();
}
cout << endl;
system("pause");
que.dequeue();
system("pause");
que.display();
system("pause");
cout<<que.front();
system("pause");
cout << "que2 以及que3 未被删\n";
que2.display();
que3.display();
system("pause");
return 0;
}