What is linked list. Can I have some example?
What is linked list. Can I have some example?
A linked list is a series of objects that point to the next item in the list (or to the previous and next, in the case of a double linked list).
This is the building block of a single linked list. Create a pointer to a structure:Code:struct mystruct { struct mystruct *next; ... other stuff here... };
struct mystruct *list;
Allocate space for the first node, using malloc:
list = malloc( sizeof( struct mystruct ) );
Assign the other elements values.
Assign the 'next' pointer either NULL, or the value of the next element.
list->next = newNode( );
This assumes that we have a function called 'newNode( )' which returns a pointer to a freshly allocated structure of the same type.
This would in effect add one on to the next end of 'list'.
Visualize a linked list as:
list -> list1 -> list2 -> list3 -> etc...
Where you have a list place holder which always holds the "top of the list", and you then prepend everything onto it.
ptr = list;
list = newNode( );
list->next = ptr;
Here we've prepended a new node on to the top of the list. If you do this, and the very first node you've ever created's "next" element pointer is NULL, you can then easily cycle through the list untill you reach the end by looking for null:
for( ptr = list; ptr != NULL; ptr = ptr->next )
This will run through the entire list.
Quzah.
Hope is the first step on the road to disappointment.
Be more specific.Originally posted by kewell
What is linked list. Can I have some example?
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
>Can I have some example?
Here's an example. It works, but whether or not it's a good example of a linked list is left to you as an exercise.
-PreludeCode:#include <stdio.h> #include <stdlib.h> typedef struct list_single_node *link; typedef struct list_full_list llist; struct list_single_node { int item; link next; }; struct list_full_list { link root; link curr; int count; }; static void li_Init ( llist *l ) { l->root = l->curr = NULL; l->count = 0; } static void li_Add_End ( llist *l, int val ) { if ( l->root == NULL ) { l->curr = l->root = malloc ( sizeof *l->curr ); if ( l->curr != NULL ) { l->curr->item = val; l->curr->next = NULL; l->count++; } } else { l->curr->next = malloc ( sizeof *l->curr ); if ( l->curr->next != NULL ) { l->curr = l->curr->next; l->curr->item = val; l->curr->next = NULL; l->count++; } } } static void li_Print_List ( llist *l ) { l->curr = l->root; while ( l->curr != NULL ) { printf ( "%d%s", l->curr->item, l->curr->next != NULL ? "->" : "\n" ); l->curr = l->curr->next; } } static void li_Kill_List ( llist *l ) { link run; l->curr = l->root; while ( l->curr != NULL ) { run = l->curr->next; free ( l->curr ); l->curr = run; } l->count = 0; } int main ( void ) { llist list; li_Init ( &list ); while ( list.count < 10 ) li_Add_End ( &list, list.count ); li_Print_List ( &list ); li_Kill_List ( &list ); return 0; }
My best code is written with the delete key.
THANK YOU VERY MUCH FOR THOSE WHO GIVE ME THE BIG HELP!!!