0

How to implement push_front() method for a singly linked list as its member function? The code below does not compile (error: lvalue required as left operand of assignment), because you cannot assign to this pointer. What is a way round this?

#include<algorithm>
using namespace std;

class ListElem{
public:
    ListElem(int val): _val(val){}
    ListElem *next() const { return _next; }
    void next(ListElem *elem) { _next = elem; }
    void val(int val){ _val = val; }
    int val() const { return _val;}
    void print();
    void push_front(int);
private:
    ListElem *_next;    
    int _val;   
};

void ListElem::push_front(int val)
{
    ListElem *new_elem = new ListElem(val); //new node
    new_elem->next( this ); // new node points to old head
    this = new_elem; // make new node the new head, error!
    return;
}

void ListElem::print()
{
    ListElem *pelem = this;
    while(ListElem *pnext_elem = pelem->next())
    {   
        cout << pelem->val() << ' ';
        pelem = pnext_elem;    
    }   
    cout << pelem->val() << endl;
}

int main()
{
    //initialization
    ListElem *head = new ListElem(1);
    ListElem *elem = head;
    for (int ix = 2; ix < 10; ++ix)
    {   
        ListElem *elem_new = new ListElem(ix);
        elem -> next(elem_new);
        elem = elem_new;    
    }   
    head->print();  

    //insert at the beginning
    head->push_front(7);
    head->print();
}
5
  • 2
    push_front is a method on the list, all I see is a list node class. Commented Aug 27, 2013 at 10:49
  • @Borgleader, I thought a head of a linked list is the list itself (book). Is this not a good approach? Commented Aug 27, 2013 at 10:57
  • 1
    I wouldn't say "often". It certainly isn't how it's done in the std library. And it doesn't support push_front. Commented Aug 27, 2013 at 11:00
  • 1
    What would you expect head->next()->push_front(0) to do? Commented Aug 27, 2013 at 11:12
  • @molbdnilo, you are right, it makes no sense to implement this method as a member of a node. Commented Aug 27, 2013 at 11:17

3 Answers 3

2

Logically, push_front() must be a method of List class and not of a ListElement class

Sign up to request clarification or add additional context in comments.

2 Comments

I see, so I can't implement push_front() if my linked list is defined as the head noded?
you should have a class List that uses this objects as its nodes.
1

You're using this incorrectly. You want to have a static member called, say, ListElem *head and use that where you were using this. You'll also have to initialise it.

1 Comment

That would mean you can only have one list in your entire program.
1

If you really want to do it that way, you can do it like this:

void ListElem::push_front(int val)
{
    ListElem *new_elem = new ListElem(_val);
    _val = val;
    new_elem->next(_next);
    _next = new_elem;
}

This will replace the data in the "current" node with the new data, and move the "current" data to the new node, which will yield the same list content.
But it's not really correct to conflate a list with its nodes.

The book you linked takes a very non-OO approach to the whole thing (both the Java and the C++ examples look like transliterated C), and conflating the type of a list with the type of its nodes is pretty certain to lead to bugs later.

For instance, if you do this

ListElem* x = head;
head->push_front(99);

then the contents of *x will have changed, which isn't really what you would expect.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.