0

I am trying to understand the concept of linked lists. So far this is what I know and where I'm having problems to understand.

//create node
struct list
{
int id; //member var
list* next; //pointer to link next list item
}

//int main()

//create list head and set it to NULL
list* head = NULL;

//instantiate list node
list* newList = new list;

//insert a list
newList->id = 20;
newList->next = NULL;

This I do not really understand what's going on.

newList->next = head;
head = newList;
17
  • 2
    YOU DON'T USE new IN C++! Commented Mar 6, 2015 at 19:15
  • 10
    @πάνταῥεῖ: stop talking rubbish. if you want to give good practice advices, please phrase your sentences accordingly. Commented Mar 6, 2015 at 19:16
  • 2
    @sasha NO! malloc is for evil void pointers from C, not C++. Commented Mar 6, 2015 at 19:34
  • 2
    @πάνταῥεῖ if you are implementing a linked list as part of learning about data structures and the algorithms that go with them (which seems to be the case here), then pointers and dynamically allocated memory pretty much goes along with that. Commented Mar 6, 2015 at 19:35
  • 1
    @πάνταῥεῖ: telling me all this is totally counter-productive... I guess you're probably going to advise this to many posters, so if you want to help them, please phrase a good advice once (and not a strict rule which will confuse the censored out of beginners - FYI their books are likely full of new) and copy-paste it each time you need it. Or, if you prefer, you can post a Q&A and link that. I hope you won't continue this discussion with me because then you've completely missed my point. Commented Mar 6, 2015 at 20:46

2 Answers 2

6

NOTE: The memory "locations" I will reference in this answer are there purely for example are not meant to mimic the actual location these pointers might or might not ever point to.

Draw these relationships out on paper to visualize the results. Let's break it down by lines.

list *head = NULL;

And here is our visualization:

 *head (0x00)
+-----------+
|           |
|   NULL    |
|           |
+-----------+

Now, we follow these next lines:

list *newList = new list;
newList->id = 20;
newList->next = NULL;

And that visualization:

 *head (0x00)      *newList (0x3a)
+-----------+     +----+------+
|           |     | id | next |
|   NULL    |     +----+------+
|           |     | 20 | NULL |
+-----------+     +----+------+

And finally we end with your last bit:

newList->next = head;

And that alters the visualization thusly (reordered for clarity):

 *newList (0x3a) +->*head (0x00)
+----+------+    | +-----------+
| id | next |    | |           |
+----+------+    | |   NULL    |
| 20 | head------+ |           |
+----+------+      +-----------+

This has created the "link" that gives a LinkedList it's name. You link nodes together by some form of a reference. So what you've done is created a "head" or beginning of the list, and then you've created a secondary node in the list and placed it (logically) before head. Normally you'd then reassign your reference to newList to head since it's the new beginning of the list.

The next step would likely be (and I'm sure this what you meant with the erroneous bit that I ask about at the end of this question):

head = newList;

Which now changes the visualization to this:

 *head (0x3a)  +---> (0x00)
+----+------+  |   +-----------+
| id | next |  |   |           |
+----+------+  |   |   NULL    |
| 20 | 0x00----+   |           |
+----+------+      +-----------+

Also, what about the following line?

head = n; // What is 'n'? Where did you get it from? It doesn't appear anywhere else in your sample

EDIT

I altered the visualization to more accurately reflect what would be realistic. I wasn't paying attention to what I was doing when I posted the original answer so many thanks to José and his comment for bringing it to my attention the visualization was inaccurate.

In addition to altering the visuals I added a bit more information and wanted to take it a step further in saying that here's how you would use this linked list to loop over it's records.

list *node = head;
while (node != NULL) {
    std::cout << "The id is " << node->id << std::endl;
    node = node->next;
}
Sign up to request clarification or add additional context in comments.

3 Comments

n was a typo, I fixed it. Thanks.
Actually, this list *head = NULL; isn't creating anything, merely declaring a pointer and making it point to nowhere. But no object is created here.
@JoséErnestoLaraRodríguez Fixed the answer now.
1
newList->next = head;
head = newList;

You start out with head set to NULL.
Then, you create a new node and let its next point to head, which is NULL.
Then, you set head to be the new node. Now, head points to a list that has one item in it.

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.