34. Array of Linked Lists-more functions included-example.
As you know pointers is a powerful tool of C language. You can manipulate data stored in RAM memory via functions not having the need to copy the data to the functions. Simply you have to insert at the functions pointers pointing to the data and you can now manipulate the data as they are staying at their memory place.
The philosophy of pointers is also applied and about Linked List. To manipulate the data stored at the nodes of a Linked List via a function we have simply to insert to the function a pointer pointing to the Linked List
Since the head node of a list is a pointer to a structure genarated by malloc() function, to be able to refer to the list via pointer we need an other pointer pointing to the pointer of the head node: a pointer to a pointer that points to a structure node.
struct node **head
At the example we make an array of six lists struct node far ** a;
All pointers of all nodes of all lists are saved to a two dimensions array
struct node far *x; so we are able to refer to each one node.
indexes of nodes:
0 1 2 3 4 ...
the a is "a"-> "b"-> "c"
the a is "a"-> "b"-> "c"-> "d"-> "e"
the a is "e"-> "b"-> "c"-> "d"
New functions are included:
1. struct node far* AppendNode(struct node far** headRef, char* num );
adds nodes to a list.
example :AppendNode(a,c0) ;
we add strings like:
2. int Length(struct node far* head);
finds the length of a list.
3. void printnode( struct node *ptr );
prints a node.
4. void printlist( struct node ** head );
prints a list.
example: printlist (a);
5. void insert_at_middle( struct node far **head, char* name, int i );
inserts a new node ,name, before node with index i, and re-connect the "brokrn chain" of the list.
example: insert_at_middle (a, "abc",2 );
6. void search_in_list(char* num, struct node **prev);
search the list for the string num.
example: search_in_list("b", a);
7. struct node * initnode( char *name );
initiates a new node using malloc().
example: initnode ("abc");
8. void deletenode( struct node far **head, int i );
Deletes a specified node and reconnect the broken chain.
example: deletenode (a,2);
9. void delete_all_after_node( struct node far **head,int i );
deletes all the nodes after the ith node.
example: delete_all_after_node (a,1);
10. void deletelist( struct node far **head );
deletes all the list.
11. unsigned int linked_list_index (struct node far **z );
returns the index of the list.
example: unsigned int linked_list_index (a); returns 2
Remember that places for lists are found by malloc() and &(a)+1!= &a.
The C code. Result at Hyperterminal.