//只带一个尾指针的队列循环链表
template<class T>
class CLinkQueue
{
public:
CLinkQueue() //构造函数
{
rear=new Node<T>;
rear->next=rear;
}
void EnQueue(T x); //入队
T DeQueue(); //出队
void SetNull(); //置空队
int GetLength(); //求队长
private:
Node<T>*rear; //队尾指针
}
//入队
template<class T>
void CLinkQueue<T>::EnQueue(T x)
{
new Node<T>*p=rear;
rear–>next=new Node<T>;
rear=rear->next;
rear->data=x;
rear->data=p;
}
//出队
template<class T>
T CLinkQueue<T>::DeQueue()
{
Node<T>*front=rear->next;
Node<T>*p=front->next;
if(!p)throw"Underflow";
front->next=p->next;
T x=p->next;
if(!(front->next))rear=front;
return x;
}
//置空队
template<class T>
void CLinkQueue<T>::SetNull()
{
rear->next=rear=new Node<T>;
rear->next=NULL;
}
//求队长
template<class T>
int CLinkQueue<T>::GetLength()
{
Node<T>*p=rear->next->next;
int i=0;
while(p)
{
i++;
p=p->next;
}
return i+1;
}
2021-03-29
最新推荐文章于 2024-10-16 10:35:43 发布