-3

I'm completely new to linked lists and have been give the below task:

"

Write a program that creates a Linked List of 10 int values (ranging from 1 to 10) using malloc().

" (There are further parts to this question but don't relate to what I'm stuck on)

I understand the concept of a linked list and I know how to use malloc(). However, the part I don't understand is nodes.

For example: how do you use malloc() on the nodes and the meaning of ->. If someone could explain how the nodes work to set up the linked list of 10 int values that would be great.

M.K
  • 1,464
  • 2
  • 24
  • 46
Ollie Parsons
  • 35
  • 1
  • 9
  • 2
    `After looking around I still don't know where to start` - start with reading your course materials. We will not do your homework for you. – SergeyA Feb 27 '19 at 15:51
  • @SergeyA I have and I can't get my head around it. Also if you read my post you'll see that I've asked for tips on how to get started and did not state anywhere that I want someone to do the whole thing for me. – Ollie Parsons Feb 27 '19 at 15:54
  • You can't simply say 'can't get my head around it' (I mean, you can, but it is not going to get you anywhere). You need to break problem down into smaller parts, and ask specific, pointed questions. Do you understand what a linked list is (in general)? Do you know how to allocate memory dynamically? Do you... As of now, the question is almost an iconic example of a bad SO question. – SergeyA Feb 27 '19 at 16:00
  • A Google search of "what is a linked list" and "linked list in C" should provide such a deluge of useful links, that it should give you plenty of ideas. – lurker Feb 27 '19 at 16:17
  • If you have successfully created the list, it seems that the next step (as provided in the instructions you give) is to traverse the list. – William Pursell Feb 27 '19 at 16:18
  • @SergeyA I've edited the question to hopefully make it clearer... – Ollie Parsons Feb 27 '19 at 16:18
  • A Google search on "using pointers in C" should help you with `->`. – lurker Feb 27 '19 at 16:19
  • If you don't understand `->`, then don't use it. Instead of `a->b`, you can write `(*a).b`. Instead of `a->b->c`, you can write `(*((*a).b)).c`. After you do that a bit, you'll start to appreciate the value of `->` – William Pursell Feb 27 '19 at 16:20
  • @lurker Thanks man. I've looked around on Google but all the examples seem to be more more complicated applications. Which does help a beginner! – Ollie Parsons Feb 27 '19 at 16:21
  • Google search "pointer to structures in C". One of the first hits: [C structures pointers](https://www.programiz.com/c-programming/c-structures-pointers) – lurker Feb 27 '19 at 16:25
  • You might like this answer that I did a while ago https://stackoverflow.com/a/20305243/1153938 – Vorsprung Feb 27 '19 at 16:31

2 Answers2

0

I recommend you to take a look into linux kernel linked list implementation. It is very simple and can be applicable for your task. Multiple open source projects use this implementation for their needs

Dmitry
  • 276
  • 2
  • 4
0

What you basically need to understand is that you need to create several nodes. Let's think about 4 nodes. Node A, B, C and D. So the use of malloc()is going to be to save in memory this 4 nodes (not in order, obviously).

It is a linked list, not a sequential list. This means that you should be able to "access" it like you would normally do in a sequential list, but they are not sequentially saved in memory (hardware speaking).

So: Your node A will have a pointer. This pointer will point to Node B. Same goes to Node B to node C, and Node C to Node D.

A->B->C->D

Remember ALL NODES must have whatever content you want, and a pointer to the next node, so you can access it from there.

This means that Node A can be in (imagine), position 4 in memory, Node B in position 16 in memory, Node C in position 2 in memory, and node D in position 300 in memory.

For example, an easy example using struct:

struct Node {
   int data;
   struct Node *next;
};

When you insert a new node, you just need to change the pointers:

A->B->C->D E (you want to insert it in the second position).

So you need to change pointers. Something like:

 E=A->nextNode; //A is no longer pointing to B
B=E->nextNode; //Now E is pointing to B

To use malloc as you are asking:

struct Node* A= malloc(sizeof(struct Node));   
  struct Node* B = malloc(sizeof(struct Node));  

Take a look in here to see it better

M.K
  • 1,464
  • 2
  • 24
  • 46
  • If it was a good explanation, upvote it! If it solved your question, mark it as an answer! Yes, every malloc for every node. Check the edited answer! @OllieParsons – M.K Feb 27 '19 at 16:37
  • Ok I see. So for my example I'd have to use 10 mallocs for each node. Then have the pointer to every node = NULL. And then assign the order using -> ?? Then to assign an int to each node do A = 1, B = 4 ect? – Ollie Parsons Feb 27 '19 at 16:44
  • `a->data=1`for example. Remember you are accessing a value of a `Struct`. `A=1`won't do. You can malloc it directly, but assigning it to NULL is to ensure nothing wrong happens with memory, and if something happens, it will point to NULL. You can do a list of nodes, and do all of that in a `for`but yeah, that would do! @OllieParsons – M.K Feb 27 '19 at 16:48
  • `(struct Node*)malloc(...)` is **bad** code. – SergeyA Feb 27 '19 at 16:50
  • I used the example that I linked in the answer so as he can understand it better. I think it is better to write some code he can look in a source, and it will be as much equal as possible. But yes, you are right. @SergeyA I do not think it is such a bad answer as to downvote it, really. – M.K Feb 27 '19 at 16:54
  • @M.K you might benefit from reading this link: https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – SergeyA Feb 27 '19 at 16:57
  • You are right and I should have written that in the first place so as he learns it is bad to do it. Still, I do not think it is that of a bad answer to down vote it. It is a **usefull** answer, well explained, with sources. And still... Well, it frustrates me actually. But thanks for your input! @SergeyA – M.K Feb 27 '19 at 17:02