简体   繁体   English

链表的Push_front和push_back方法似乎删除了一个节点

[英]Push_front and push_back methods for linked list seems to remove a node

I'm writing a class called Playlist which performs different operations on PlaylistNodes . 我正在写一个类中调用Playlist其上执行不同的操作PlaylistNodes I looked online and tried to implement push_back and push_front methods, but I was unsuccessful. 我在网上查看并尝试实现push_backpush_front方法,但未成功。

PlaylistNode *PlaylistNode::insert_next(PlaylistNode *p) {
    PlaylistNode *tmp = nullptr;

    tmp = this->next;
    this->next = p;
    p->next = tmp;

    return p;
}

Playlist::Playlist() {
    head = new PlaylistNode;
    prevToCurr = head;
    tail = head;
    size = 0;
}

Playlist *Playlist::push_back(PlaylistNode *p) {
    PlaylistNode *tmp;

    tmp = tail;
    tmp->insert_next(p);
    tail = p;

    prevToCurr = tail;

    size++;
    return this;
}

Playlist *Playlist::push_front(PlaylistNode *p) {
    size++;

    PlaylistNode *tmp = head;
    head = p;
    head->insert_next(tmp);

    return this;
}

When I run : 当我跑步时:

play.push_front(node1);
play.push_front(node2);
play.push_front(node2);

then print the linked list, I only get 2 nodes: 然后打印链表,我只有2个节点:

ID 44: song2
ID 33: song1

Your initialization method (constructor) is not doing what it's supposed to. 您的初始化方法(构造函数)未执行预期的工作。 When constructing this type of list, head and tail must both point to null as the list is empty. 构造此类型的列表时,头和尾都必须都指向null,因为列表为空。 I'm not sure what "prevToCurr" does but I don't think lists use something like that so I would get rid of it: 我不确定“ prevToCurr”的作用,但是我不认为列表使用类似的东西,所以我会摆脱它:

Playlist::Playlist() {
    head = null;
    tail = null;
    size = 0;
}

For simplicity, Handle push_front() with 2 cases: when the list is empty and when the list has nodes. 为简单起见,请使用2种情况处理push_front():列表为空时以及列表具有节点时。

  1. If the list is empty all you have to do is make head and tail point to the new node. 如果列表为空,则只需将头和尾指向新节点即可。
  2. When the list is not empty, you make the previous head point to the new node and update the list head. 如果列表不为空,则使前一个头指向新节点并更新列表头。

Would look like this: 看起来像这样:

Playlist *Playlist::push_front(PlaylistNode *p) {
    if (size == 0) {
        head = p;
        tail = p;
    }
    else {
        head->insertNext(p);
        head = p;
    }
    size++;
    return this;
}

Your push_back() method would be implemented almost the same way as the push_front, updating tail instead of head and making the new tail point to the old one, I'm sure you can figure it out. 您的push_back()方法的实现方式与push_front几乎相同,只是更新tai​​l而不是head并使新的tail指向旧的方法,我相信您可以弄清楚。

You don't give enough info so I'm going to guess your PlaylistNode::insertNext sets the next node. 您没有提供足够的信息,所以我将猜测您的PlaylistNode :: insertNext设置了下一个节点。 In this case, what you do here is set the next pointer to the node provided as argument: 在这种情况下,您要做的是设置指向作为参数提供的节点的下一个指针:

PlaylistNode *PlaylistNode::insert_next(PlaylistNode *p) {
    this->next = p;
    return this;
}

That should more or less work, as long as you're creating your PlaylistNodes correctly (don't push the same node 2 times, you will end up with a node pointing next to itself as stated in the comments). 只要正确创建PlaylistNodes,这应该或多或少起作用(不要将同一个节点压入2次,最终将导致节点指向其自身旁边,如注释中所述)。

As noted in the comments this is hard to figure out without all the code, so we have to infer. 正如注释中指出的那样,如果没有所有代码,这很难弄清楚,因此我们必须进行推断。 Working backwards from what could cause this, we assume that the node passed to push_front has a null next pointer. 从可能导致这种情况的原因向后工作,我们假设传递给push_front的节点的next指针为空。 Then your insert_next function will take that new node's null pointer and incorrectly update the inserted node's next pointer to null regardless of what it was before. 然后,您的insert_next函数将采用该新节点的空指针,并将插入的节点的next指针错误地更新为空,而不管它之前是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM