链队列的存储结构
将对头指针front指向链队列的头结点(头节点为空,不存数据),队尾指针rear指向终端结点。元素从队尾进入,队首出列。
元素为空时,队尾指针指向队头指针。
链式队列模版实现:
功能:
1 创建
2 遍历
4 入队,出队
5 获取队尾队首队中某位置值
6 修改队尾队首队中某位置值
7 删除队尾队首队中某位置值
8 清空复制等
节点定义
template
struct Link {
T data;
struct Link *next;
构造函数
Link(){this->next = nullptr;}
Link(const T &data){this->data = data; this->next = nullptr;}
Link(const T &data, struct Link *next){this->data = data; this->next = next;}
Link(const struct Link &node){this->data = node.data; this->next = node.next;}
};
别名定义
template
using LinkNode = struct Link
队列定义
template
class LinkQueue {
private:
LinkNode
LinkNode
public:
构造函数
LinkQueue() {this->front = this->rear = new LinkNode
LinkQueue(const T &data){this->front = new LinkNode
LinkQueue(const LinkNode
LinkQueue(const LinkQueue
LinkQueue & operator=(const LinkQueue
~LinkQueue();
void clear();
bool isEmpty();
int length();
void show();
bool GetFrontNode(T &data);
bool GetRearNode(T &data);
bool GetPosNode(T &data, int pos);
bool SetFrontNode(const T &data);
bool SetRearNode(const T &data);
bool SetPosNode(const T &data, int pos);
bool PushRearNode(const T &data);
bool PopFrontNode(T &data);
bool DelFrontNode();
bool DelRearNode();
bool DelPosNode(int pos);
/*
function:find the position of node which equal to data in the list
data:the data which compared
return value:the position which equal to data;
this function need edit accoding the T
*/
int GetFirstNodePos(T &data);
/*
function:find the position of node which equal to data in the list
data:the data which compared
pos:the array which storage the position
return value:the num which equal to data;
this function need edit accoding the T
*/
int GetNodePos(T &data,int *pos);
};
成员函数实现:
复制构造函数:
template
LinkQueue
{
ULOGE("LINKQUEUE destruct");
this->front = this->rear = new LinkNode
this->rear->next = nullptr;
if(queue.rear == queue.front){
ULOGW("empty queue");
this->front->next = nullptr;
return;
}
LinkNode
//this->front->next = p2;
//p2 = p2->next;
while(p1->next != nullptr){
p3 = new LinkNode
p2->next = p3;
p2 = p2->next;
p1 = p1->next;
}
p3 = new LinkNode
p2->next = p3;
this->rear = p3;
this->rear->next = nullptr;
}
=函数符重载
template
LinkQueue
{
ULOGE("LINKQUEUE111 destruct");
this->front = this->rear = new LinkNode
this->rear->next = nullptr;
if(queue.rear == queue.front){
ULOGW("empty queue");
this->front->next = nullptr;
return *this;
}
LinkNode
//this->front->next = p2;
//p2 = p2->next;
while(p1->next != nullptr){
p3 = new LinkNode
p2->next = p3;
p2 = p2->next;
p1 = p1->next;
}
p3 = new LinkNode
p2->next = p3;
this->rear = p3;
this->rear->next = nullptr;
return *this;
}
析构函数
template
LinkQueue
{
LinkNode
while(p != nullptr){
p = p->next;
delete this->front;
this->front = p;
}
}
清除函数
template
void LinkQueue
LinkNode
LinkNode
while(p1 != nullptr){
p1 = p1->next;
delete p2;
p2 = p1;
}
this->rear = this->front;
this->front->next = nullptr;
}
判断是否为空
template
bool LinkQueue
{
if(this->front == this->rear)
return true;
return false;
}
获取长度
template
int LinkQueue
{
int len = 0;
LinkNode
while(p != nullptr){
p = p->next;
++len;
}
return len;
}
遍历
template
void LinkQueue
{
LinkNode
std::cout<<"The sum of data in the queue is: "<
while(p != nullptr){
std::cout<
p = p->next;
}
std::cout<
获取队头队尾及pos处值
template
bool LinkQueue
{
return GetPosNode(data, 0);
}
template
bool LinkQueue
{
return GetPosNode(data, length()-1);
}
template
bool LinkQueue
{
if(pos < 0){
ULOGW("invalue pos");
return false;
}
int pos1 = pos;
LinkNode
while((p != nullptr)&& pos){
--pos;
p = p->next;
}
if(p == nullptr){
ULOGW("no ", pos1, "th node");
return false;
}
data = p->data;
return true;
}
修改队头队尾及pos位置值
template
bool LinkQueue
{
return SetPosNode(data, 0);
}
template
bool LinkQueue
{
return SetPosNode(data, length()-1);
}
template
bool LinkQueue
{
if(pos < 0){
ULOGW("invalue pos");
return false;
}
int pos1 = pos;
LinkNode
while((p != nullptr)&& pos){
--pos;
p = p->next;
}
if(p == nullptr){
ULOGW("no ", pos1, "th node");
return false;
}
p->data = data;
return true;
}
入队
template
bool LinkQueue
{
LinkNode
this->rear->next = p;
this->rear = p;
}
出队
template
bool LinkQueue
{
if(this->rear == this->front){
ULOGW("empty queue");
return false;
}
LinkNode
data = p->data;
if(p != this->rear){
this->front->next = p->next;
}else{
this->rear = this->front;
this->front->next = nullptr;
}
delete p;
return true;
}
删除队头队尾或pos处的值
template
bool LinkQueue
{
return DelPosNode(0);
}
template
bool LinkQueue
return DelPosNode(length()-1);
}
template
bool LinkQueue
{
if(pos < 0){
ULOGW("invalue pos");
return false;
}
int pos1 = pos;
LinkNode
LinkNode
p1 = p1->next;
while((p1 != nullptr)&& pos){
--pos;
p2 = p1;
p1 = p1->next;
}
if(p1 == nullptr){
ULOGW("no ", pos1, "th node");
return false;
}
if(p1 != this->rear){
p2->next = p1->next;
}else{
this->rear = p2;
this->rear->next = nullptr;
}
delete p1;
return true;
}
如有问题欢迎大家指正。
源码下载地址:https://download.csdn.net/download/zhouchao_0321/10678106