为什么出错???能不能解释一下代码(队列的链表实现)

求大佬帮忙
队列的链表实现
头文件
#ifndef LINKQUEUE_H
#define LINKQUEUE_H
#include <iostream>
using namespace std;
/***链式队列,front指向队列附加头结点,rear指向最后一个元素,
和动态数组表示的队列不同,它的front指向第一个元素,rear指向下一个要插入的位置*/
template <class T>
struct Node
{
    T data;
    Node<T> *next;
    Node(Node<T> *p = NULL) :next(p) {}
    Node(const T& x, Node<T> *p = NULL) :data(x), next(p) {}
};
template <class T>
class LinkQueue
{
public:
    LinkQueue() :front(new Node<T>) { rear = front; }
    ~LinkQueue() { makeEmpty(); }
    void makeEmpty();
    bool EnQueue(const T& x);
    bool DeQueue(T& x);
    bool IsEmpty() { return front == rear; }
    bool getFront(T& x)const;
    int getSize()const;
    friend ostream& operator<< <T>(ostream&, LinkQueue<T>&);
protected:
    Node<T> *front, *rear;
};
template <class T>
void LinkQueue<T>::makeEmpty()
{
    Node<T> *p;
    while (front->next)
    {
        p = front->next;
        front->next = p->next;
        delete p;
    }
    front = rear = NULL;
};
template <class T>
bool LinkQueue<T>::EnQueue(const T& x)
{
    rear->next = new Node<T>(x);
    if (rear == NULL) return false;
    rear = rear->next;
    return true;
};
template <class T>
bool LinkQueue<T>::DeQueue(T& x)
{
    if (IsEmpty()) return false;
    Node<T> *p = front->next;
    front->next = p->next;
    x = p->data;
    //<span style = "white-space:pre">    < / span>if (rear == p)
    //    <span style = "white-space:pre">    < / span>   rear = front;
    delete p;
    return true;
};
template <class T>
bool LinkQueue<T>::getFront(T& x)const
{
    if (IsEmpty()) return false;
    x = front->next->data;
    return true;
};
template <class T>
int LinkQueue<T>::getSize()const
{
    Node<T> *p = front;
    int count = 0;
    while (p != rear)
    {
        count++;
        p = p->next;
    }
    return count;
};
template <class T>
ostream& operator<<(ostream& out, LinkQueue<T>& q)
{
    if (q.IsEmpty())
    {
        out << "The queue is Empty!" << endl;
        return out;
    }
    Node<T> *p = q.front->next;

    int i = 0;
    while (p)
    {
        cout << (++i) << ":" << p->data << endl;
        p = p->next;
    }
    return out;
};
#endif

 


源文件#include <iostream>
//#include "SeqQueue.h"
#include "LinkQueue.h"
using namespace std;
int main()
{
    //SeqQueue<int> q;
    LinkQueue<int> q;
    int a;
    cin >> a;
    while (a != 0)
    {
        q.EnQueue(a);
        cin >> a;
    }
    int x;
    for (int i = 1; i <= 5; i++)
    {
        q.DeQueue(x);
        cout << "第" << i << "次出列:" << x << endl;
    }
    cout << "出列后的队列:" << endl;
    cout << q;
    system("pause");
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值