0

Please delete.

I want to implement a linked list. Unfortunately I'm not sure whether I'm on the right track.

#include <iostream>
using namespace std;

class Node {
    friend class List;
public:
    int value;
private:
    Node *next;
};

class List {
public:
    List ();
    ~List ();
    Node * first() const;
    Node * next(const Node * n) const;
    void append (int i);

    Node* head;
};

List::List() {
    Node* head = new Node();
}

List::~List() {
    while(head != NULL) {
        Node * n = head->next;
        delete head;
        head = n;
    }
}

Node * List::first() const {
    return head; // this could also be wrong
}

Node * List::next(const Node * n) const {
    return n + 1; // ERROR
}

void List::append(int i) {
    Node * n = new Node;
    n->value = i;
    n->next = head;
    head = n;
}

int main(void) {
    List list;
    list.append(10);

    return 0;
}

When I try to return an element in next() I get this error:

In member function ‘Node* List::next(const Node*) const’:|
error: invalid conversion from ‘const Node*’ to ‘Node*’ [-fpermissive]|

Could somebody please help me?

EDIT:
I've updated the error-line.

2
  • What are you trying to do in List::next? Are you trying to advance n nodes? Commented Apr 30, 2013 at 1:35
  • My fault. I'have updated this function, but still get an error. Commented Apr 30, 2013 at 1:38

2 Answers 2

2

I think what you mean to be doing is returning the Node's next:

Node * List::next(const Node * n) const {
   return n->next;
}

You would use pointer arithmetic if this were an array where the size of each object was constant, but linked lists can't use pointer arithmetic. If you have an iterator, you could use the '++' operator to get the next object, but with this just stick to returning the node's next field.

I'm assuming this will also work because even though next is declared as private, you've made List a friend.

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

4 Comments

Do you also know, how to insert a new value between other values? Something like insert (Node * n, int i);. The new value should be before n.
Sure. Save the next value of n, create a new Node with the value for i, then set n->next to the new node and the new node's next to the old value of n's next.
I get an infinit loop here: Node * temp = n->next; Node * n2 = new Node; n2->value = i; n->next = head; n2->next = temp;
You don't want to be setting it to the head - you're inserting this node in some arbitrary location remember. You want to be setting it to your new node.
0

You are thinking that consecutive nodes are in consecutive blocks of memory, and they are not. Linked lists have nodes in random places in memory, which is why "next" points to the NEXT node. You cannot increment or add as you are trying (well you can, but semantically it would be incorrect.)

2 Comments

Could you please tell me how to improve it?
You would have to cycle through all the nodes in a for loop until you get to the requested node

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.