#pragma once
//非循环链队
#ifndef My_Head_H
#define My_Head_H
#include "G://code/c++/myhead.h"
#endif // !My_Head_H
template <typename ElemType>
class LinkQueue
{
private:
class Queue_Node
{
public:
ElemType data;
Queue_Node* next;
};
typedef Queue_Node* Nodepointer;
public:
//把队列置空
void clear();
//出队列(删除链队队头结点)
Status deQueue(ElemType& e);
//进队列(在队尾加入结点)
void inQueue(ElemType e);
//求链队中结点个数
int Get_Length();
//以随机数填充i个结点
void Random_Fill_Queue(int i);
//打印所有结点
void Display_Queue();
LinkQueue();
~LinkQueue();
protected:
Nodepointer rear; //队尾指针
Nodepointer front;//队首指针
};
template<typename ElemType>
void LinkQueue<ElemType>::clear()
{
Nodepointer q;
Nodepointer p = front;
while (p)
{
q = p;
p = p->next;
delete q;
}
front = NULL;
rear = NULL;
}
template<typename ElemType>
Status LinkQueue<ElemType>::deQueue(ElemType& e)
{
if (!front) return ERROR;
Nodepointer p = front;
e = p->data;
front = front->next;
delete p;
if (!front) rear = NULL;
return OK;
}
template<typename ElemType>
void LinkQueue<ElemType>::inQueue(ElemType e)
{
Nodepointer p = new Queue_Node;
p->data = e;
p->next = NULL;
if (!front)
{
front = p;
rear = p;
}
else
{
rear->next = p;
rear = p;
}
}
template<typename ElemType>
int LinkQueue<ElemType>::Get_Length()
{
int length = 0;
Nodepointer p = front;
while (p)
{
length++;
p = p->next;
}
return length;
}
template<typename ElemType>
void LinkQueue<ElemType>::Random_Fill_Queue(int i)
{
for (int count = 0; count < i; count++)
{
inQueue(random(100));
}
}
template<typename ElemType>
void LinkQueue<ElemType>::Display_Queue()
{
cout << "Here is all Queue data:" << endl;
Nodepointer p = front;
while (p)
{
cout << "->" << p->data;
p = p->next;
}
cout << endl;
}
template <typename ElemType>
LinkQueue<ElemType>::LinkQueue()
{
front = NULL;
rear = NULL;
}
template <typename ElemType>
LinkQueue<ElemType>::~LinkQueue()
{
clear();
}
09-29