📄 linkedqueue.h
字号:
#pragma once
#define INVALID_SIZE 1
#define NO_MEMORY 2
#define INDEX_OVER_BOUNDS 3
template<class T>class LinkedQueue;
template<class T>
class Node
{
friend class LinkedQueue<T>;
private:
T data;
Node<T> * link;
Node(T t,Node<T>*n = NULL):data(t),link(n){}
};
////////////////////////////////////////////////////////////
template<class T>
class LinkedQueue
{
public:
LinkedQueue(){front = rear = NULL;};
~LinkedQueue();
public:
bool IsEmpty(){return front == NULL;}
LinkedQueue<T>&Add(const T&);
LinkedQueue<T>&Delete(T&);
T GetFirst();
T GetLast();
int GetSize();
private:
Node<T>*front;
Node<T>*rear;
};
/////////////////////////////////////////////////////////
template<class T>
LinkedQueue<T>::~LinkedQueue()
{
try{
while(true)
{
T i;
this->Delete(i);
}
}catch(int){}
}
template<class T>
LinkedQueue<T>&LinkedQueue<T>::Add(const T&t)
{
Node<T>*n = new Node<T>(t);
if(front == NULL)
{
front = rear = n;
}
else
{
rear->link = n;
rear = n;
}
return *this;
}
template<class T>
LinkedQueue<T>&LinkedQueue<T>::Delete(T&t)
{
if(this->IsEmpty())throw INDEX_OVER_BOUNDS;
t = front->data;
Node<T>*temp = front;
front= front->link;
delete temp;
return *this;
}
template<class T>
T LinkedQueue<T>::GetFirst()
{
if(this->IsEmpty())throw INDEX_OVER_BOUNDS;
return front->data;
}
template<class T>
T LinkedQueue<T>::GetLast()
{
if(this->IsEmpty())throw INDEX_OVER_BOUNDS;
return rear->data;
}
template<class T>
int LinkedQueue<T>::GetSize()
{
Node<T>*front = this->front;
int count = 0;
while(front)
{
front=front->link;
count++;
}
return count;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -