0

I'm writing a class called Playlist which performs different operations on PlaylistNodes. I looked online and tried to implement push_back and push_frontmethods, but I was unsuccessful.

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:

ID 44: song2
ID 33: song1
chqrlie
  • 131,814
  • 10
  • 121
  • 189
MZP
  • 51
  • 1
  • 5

2 Answers2

1

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. I'm not sure what "prevToCurr" does but I don't think lists use something like that so I would get rid of it:

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.

  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.

You don't give enough info so I'm going to guess your PlaylistNode::insertNext sets the next node. 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).

DanyAlejandro
  • 1,440
  • 13
  • 24
0

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. 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.

Mark B
  • 95,107
  • 10
  • 109
  • 188