# Thread: Linked Lists for Dummies

1. ## Linked Lists for Dummies

I'm stuck on linked lists. I'm confused as to how to create a new linked list --- there are loads of examples on the internet of how to iterate through a linked list that ALREADY exists, and there are examples of how to create a linked list manually with separately named nodes, or how to insert a node into a list that already exists. However, for the purposes of my homework (which involves a MUCH more complicated task than simply implementing a linked list), I'm going to need to iterate through some data, creating a linked list from it. Actually, I'm going to have to create a hash table, but BABY STEPS!

What I would LOVE is if someone gave me a simple, dummy explanation of how to implement a linked list of 1, 2, 3.

For my purposes, I am assuming that a struct node is

Code:
```typedef struct node
{
int i;
struct node* next;
}
node;```
Okay? From what I've gathered, the head node contains no value, and is declared and allocated to point to the next node as such:
Code:
`node* head = malloc(sizeof(node));`
Soo to point it to the next node... We do something like this?

Code:
`head->next = node* new;`
And we'd have to allocate that as well, and give it a value

Code:
```new = malloc(sizeof(node));

new->i = 1;```
[/CODE]

Okay, good. Have I successfully made a list of 1? That's about as far as I can get. Then I want to link the node new to the next node.
Code:
`new->next = new;`
??????
Here is where I get lost. Suppose that I'm actually doing this in a for loop, so I want to be able to do this 3 times. This doesn't seem right to me??

Can someone please help and show me a simple, bare bones way to do this? Also, please let me know if I have gotten ANY of this right. I am so confused!
I am NOT asking anyone to do my homework for me. This isn't even what my assignment is, but I can't do my assignment without grasping this concept.

2. Here is an example which adds nodes to the front of a list.
Code:
```#include <stdio.h>
#include <stdlib.h>

struct node {
int          value;
struct node *next;
};

struct node *prepend ( struct node *head, int value ) {
// allocate a new node
struct node *new = malloc( sizeof(*new) );

// fill everything in
new->value = value;
new->next = NULL;

// link to the existing linked list
new->next = head;
return new;
}

void print ( struct node *head ) {
while ( head != NULL ) {
printf("%d ", head->value );
head = head->next;
}
printf("\n");
}

int main ( ) {
struct node *head = NULL;
int i;
for ( i = 1 ; i <= 3 ; i++ ) {
head = prepend(head,i);
}
print(head);
return 0;
}```
I would suggest the next thing you should try is
struct node *append ( struct node *head, int value )
to add a node to the end of the list, so it prints 1 2 3 when you run the code.

3. ## Thank you for your help!

I tried implementing append this way, but I get a segfault. Do you have any suggestions?
Code:
```void append(int i)
{
cursor = first;

while (cursor != NULL)
{
cursor = cursor->next;
}

node* temp = malloc(sizeof(node));
temp->i = i;
temp->next = NULL;
cursor->next = temp;

}```

4. > cursor->next = temp;
What is cursor always going to be, on exit from the while loop?

You're trying to find the last node, not fall off the end.

Also, why are cursor and first global variables all of a sudden?

5. Originally Posted by Salem
> cursor->next = temp;
What is cursor always going to be, on exit from the while loop?

You're trying to find the last node, not fall off the end.

Also, why are cursor and first global variables all of a sudden?
You're right. I changed it to

Code:
```void append(int i)
{
cursor = first;

while (cursor->next != NULL)
{
cursor = cursor->next;
}

node* temp = malloc(sizeof(node));
temp->i = i;
temp->next = NULL;
cursor->next = temp;

}```
Buuut I still get a segfault.

They're global variables because I'm calling them in all sorts of functions (prepend, append, insert, find i, etc...)

6. That code could fail either if malloc happens to return NULL, or if first was NULL to begin with.
It should be okay once you take care of those cases.

Do at least try to avoid globals though.

7. > They're global variables because I'm calling them in all sorts of functions (prepend, append, insert, find i, etc...)
Look at my first example - did it use global variables?
Did it need global variables?

> Buuut I still get a segfault.
Because first is NULL when the program starts, and you have an empty list!

Popular pages Recent additions