1. Create a single-chain table
Assume that the data type of a node in a linear table is a character. We enter these two nodes one by one and use the linefeed '\ n' as the terminator of the input condition. There are two common methods to dynamically create a single-chain table:
(1) Table creation using the header plug-in method
① Algorithm ideas
From an empty table, read data repeatedly, generate a new node, store the read data in the data field of the new node, and then insert the new node into the table header of the current linked list until the end mark is read.
For more information, see animation demonstration]
Note:
The node order of the linked list generated by this method is opposite to the input order.
② Specific Algorithm Implementation
1 linklist creatlistf (void)
2 {// return the header pointer of a single-chain table
3 char ch;
4 linklist head; // header pointer
5 listnode * s; // working pointer
6 head = NULL; // The start of the linked list is null.
7 CH = getchar (); // read 1st characters
8 while (Ch! = '\ N '){
9 S = (listnode *) malloc (sizeof (listnode); // generate a new node
10 s-> DATA = CH; // put the read data into the data domain of the new node
11 S-> next = head;
12 head = s;
13 CH = getchar (); // read the next character
14}
15 return head;
16}