# How to 'index' the nodes in linked list?

Hello!

I am taking a distance course of C programming and I can't figure out what the tutor explains about this. (Communication over the e-mails)

This is about the linked list and about inserting the new node in the middle of the linked list. So.. what I think is the each existing nodes should have index number of it.

The user types where s/he wants to insert the new node by,

[code]
printf("Insert node at index (1st node has index 0): ");
scanf("%d", &index);
[/code]

And the following code will spot the location and insert the new node into the desired location.

[b][red]But I don't see the explanation or any code how to index the nodes (existing nodes) in the first place. And I've been asking this to the tutor but he doesnt understand my question.[/b][/red]

[b]main function and the other functions are ommited[/b]
[code]
struct stockRecord* addStockCodeAt(struct stockRecord* list, int index,
int code, char* name) {

struct stockRecord* temp;
struct stockRecord* newnode;

/* search for the location */
temp = getNodeAt(list, index);

/* allocate memory block and assign parameter values to it */
newnode = (struct stockRecord*)malloc(sizeof(struct stockRecord));
newnode->code = code;
strcpy(newnode->name, name);
newnode->next = NULL;

/* insert the new node */
if (temp == NULL) /* if the linked list is empty */
return newnode;
else {
newnode->next = temp->next;
temp->next = newnode;
}
return list;
}
struct stockRecord* getNodeAt(struct stockRecord* list, int index) {
struct stockRecord* temp;

if (list == NULL)
return NULL;

temp = list;
while (temp->next != NULL) {
/* if the node is found */
if (index <= 0)
break;
index--;
temp = temp->next;
}
return temp;
}
[/code]

On the first function, the value of the index gets passed from the calling function. This value is the user input value where s/he wants the new node at.

If the user input is 2, then the programme has to insert the new node after the node indexed 2.

But, [b]HOW DO YOU FIRST INITIALISE THE NODES[/b]?? And because this -how- is missing from the entire code, I dont know how. And I am asking my tutor if the code is missing how or if I am stupidly missing it, he doesn't get my question so I don't know if I am wrong or the code is wrong....

Can we index each node when each time the node gets created like this?

[code]
newnode(....)malloc(....); //create a newnode
newnode->name = name; //assing the user input value to the newnode
newnode->code = code;
index++; //[red]Can we do this....?[/red]
newnode = index; //[red]So that the newnode will holds the index value..?[/red]
[/code]

• : Hello!
:
: I am taking a distance course of C programming and I can't figure out what the tutor explains about this. (Communication over the e-mails)
:
: This is about the linked list and about inserting the new node in the middle of the linked list. So.. what I think is the each existing nodes should have index number of it.
:
: The user types where s/he wants to insert the new node by,
:
: [code]
: printf("Insert node at index (1st node has index 0): ");
: scanf("%d", &index);
: [/code]
:
: And the following code will spot the location and insert the new node into the desired location.
:
: [b][red]But I don't see the explanation or any code how to index the nodes (existing nodes) in the first place. And I've been asking this to the tutor but he doesnt understand my question.[/b][/red]
:
: [b]main function and the other functions are ommited[/b]
: [code]
: struct stockRecord* addStockCodeAt(struct stockRecord* list, int index,
: int code, char* name) {
:
: struct stockRecord* temp;
: struct stockRecord* newnode;
:
: /* search for the location */
: temp = getNodeAt(list, index);
:
: /* allocate memory block and assign parameter values to it */
: newnode = (struct stockRecord*)malloc(sizeof(struct stockRecord));
: newnode->code = code;
: strcpy(newnode->name, name);
: newnode->next = NULL;
:
: /* insert the new node */
: if (temp == NULL) /* if the linked list is empty */
: return newnode;
: else {
: newnode->next = temp->next;
: temp->next = newnode;
: }
: return list;
: }
: struct stockRecord* getNodeAt(struct stockRecord* list, int index) {
: struct stockRecord* temp;
:
: if (list == NULL)
: return NULL;
:
: temp = list;
: while (temp->next != NULL) {
: /* if the node is found */
: if (index <= 0)
: break;
: index--;
: temp = temp->next;
: }
: return temp;
: }
: [/code]
:
: On the first function, the value of the index gets passed from the calling function. This value is the user input value where s/he wants the new node at.
:
: If the user input is 2, then the programme has to insert the new node after the node indexed 2.
:
: But, [b]HOW DO YOU FIRST INITIALISE THE NODES[/b]?? And because this -how- is missing from the entire code, I dont know how. And I am asking my tutor if the code is missing how or if I am stupidly missing it, he doesn't get my question so I don't know if I am wrong or the code is wrong....
:
: Can we index each node when each time the node gets created like this?
:
: [code]
: newnode(....)malloc(....); //create a newnode
: newnode->name = name; //assing the user input value to the newnode
: newnode->code = code;
: index++; //[red]Can we do this....?[/red]
: newnode = index; //[red]So that the newnode will holds the index value..?[/red]
: [/code]
:

I'm not sure why you want an index value for each node, but yeah you could make another member variable inside the node struct to save it, if it is something the application need.

Not that since you can't access a linked list directly by using an index value, like you could with an array, it doesn't make sense to save an index for that purpose. This is because array items are stored adjacent in memory, while a node in a linked list could be allocated anywhere. So you have to go through the linked list and search for the node you want every time you access it, which is one of the disadvantages of linked lists.

As for initialization: when the program starts, you usually have an empty linked list which you fill with nodes in runtime. Almost all linked lists uses dynamic allocation, so the nodes get set to a value when you add them.

• The nodes aren't indexed.

The GetNodeAt function just iterates through the list, requesting the 'next' node n times.
Here's a typical disavantage (as opposed to arrays for example) of a linked list. It takes time to access a particular node in the list.

Greets,
Eric Goldstein
http://www.gvh-maatwerk.nl