Linked lists don't have "indexes", because a linked list structure is not random access. You can find out the position of an element in your linked list by iterating through it with a linear search, but what you need to remember is the memory address of your node which you wish to perform an operation on (eg, insert/delete) - any other value is completely worthless.
Handling a linked list is a real test of how well you understand memory management and pointers. You cannot do what you're asking without using a pointer variable to remember your "current" node.
Typically, creating a linked list involves dynamically created memory. Its almost unheard of to create nodes as named variables. Here's a brief example of creation/insertion to a linked list.
CODE
#include <stdlib.h>
struct node
{
int data;
struct node* next;
};
/*
* push_back() - Take a pointer to the list head's placeholder.
* and the data to be inserted to the list
*
* Create a new node with malloc -
* If the node is created successfully then store data in the
* node. The new node becomes the head of the list.
*
*/
void push_back(struct node** head, int i)
{
struct node* newnode = malloc( sizeof( struct node ) );
if( newnode != NULL )
{
newnode->next = *head;
newnode->data = i
*head = newnode;
}
}
int main(void)
{
/* Create a pointer to the list's head.
* The list starts out empty, so the head starts as NULL.
*
* This is essentially a placeholder to remember where
* the list head resides in memory.
*/
struct node* list_head = NULL;
/* Add 3 elements to the list, with data 1, 2 and 3 */
push_back( &list_head, 1 );
push_back( &list_head, 2 );
push_back( &list_head, 3 );
/* TODO: Clean the list up using free() */
}
This post has been edited by Bench: 25 Nov, 2007 - 04:48 AM