一个关于链表队列的小问题

来源:4-7 带有尾指针的链表:使用链表实现队列

Mapple_

2019-07-07

波波老师,这是我看了您的课自己写的一个链表队列。我的这个链表队列在出队的时候却有问题,我自己也debug了,发现了问题,就是在出队的时候(错误就在下面这个出队的函数)debug告诉我这个front是空的,然后front->next时错误。可是我已经在入队时候的第一个节点的地址就让 front=tail=new Node(e)了;按道理来说这个front不是空呀? 有点搞不明白了,希望波波老师教我下,谢谢!

入队的代码:

 void enqueue(T e){
         if(tail==NULL){
             tail=new  Node(e);
             front=tail;
         }
         else {
             tail->next=new  Node(e);
             tail=tail->next;
         }
         size++;
    }

出队的代码:

T dequeue(){
        assert(!isEmpty());
        Node *retnode=front;
        front=front->next;
        retnode->next=NULL;
        if(front==NULL)
            tail=NULL;
        size--;
        return retnode->e;
    }

全部代码如下:

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cassert>
using namespace std;

template<typename T>
class LinkQueue{

private:
    int size;
    struct Node{
        T e;
        Node *next;
        Node(){
            this->e=NULL;
            this->next=NULL;
        }
        Node(T e){
            this->e=e;
            this->next=NULL;
        }
        Node(T e,Node *next){
            this->e=e;
            this->next=next;
        }
    };
    Node *tail,*front;

public:
    LinkQueue(){
        this->size=0;
        this->tail=this->front=NULL;
    }

    int getSize(){
        return this->size;
    }

    bool isEmpty(){
        return size==0;
    }

    void enqueue(T e){
         if(tail==NULL){
             tail=new  Node(e);
             front=tail;
         }
         else {
             tail->next=new  Node(e);
             tail=tail->next;
         }
         size++;
    }


    T dequeue(){
        assert(!isEmpty());
        Node *retnode=front;
        front=front->next;
        retnode->next=NULL;
        if(front==NULL)
            tail=NULL;
        size--;
        return retnode->e;
    }


    void PrintLinkQueue(){
        assert(!isEmpty());
        cout<<"front ";
        while(front!=NULL){
            cout<<front->e;
            if(front->next)
                cout<<"->";
            front=front->next;
        }
        cout<<" tail"<<endl;
    }
};

int main()
{

    LinkQueue <int>*arr=new LinkQueue<int>();
    for(int i=1;i<6;i++){
        arr->enqueue(i);
    }
    arr->PrintLinkQueue();

    //此处出队有问题
    cout<<arr->dequeue()<<endl;
    arr->PrintLinkQueue();

    return 0;
}
写回答

1回答

liuyubobobo

2019-07-07

你在PrintLinkQueue的过程中,一直在移动front,遍历结束以后,front指向了空。


其实,你已经发现了在dequeue的时候,front是空了,又确定了初始入队的时候,front肯定不为空,那这之间逻辑,一定在什么时候,改变了front。一个简单的调试方式是:

LinkQueue <int>*arr=new LinkQueue<int>();
for(int i=1;i<6;i++){
    arr->enqueue(i);
    // 每次都在这里看一下,front是不是为空?
}

arr->PrintLinkQueue();

// 如果上面循环里每次入队,front都不为空,到这里,front却为空
// 问题肯定出在上面的arr->PrintLinkQueue();
cout<<arr->dequeue()<<endl;


著名侦探小说中福尔摩斯的观点,很适合debug。

排除所有不可能,剩下的即使再不可思议,也是事实。


继续加油!:)

0
1
Mapple_
非常感谢!
2019-07-07
共1条回复

玩转数据结构

动态数组/栈/队列/链表/BST/堆/线段树/Trie/并查集/AVL/红黑树…

6221 学习 · 1705 问题

查看课程