Data Structure Learning (2) -- create a linked list by using the method of header insertion and tail insertion for single-chain table operations

Source: Internet
Author: User

A linked list is also a linear table. Unlike an ordered table, it is not stored continuously in the memory. In C, linked lists are implemented through pointer-related functions. A single-chain table is one of the linked lists. A single-chain table is a data domain in its node and only a pointer domain pointing to the next node. Two methods are available for creating a single-chain table: Header insertion and tail insertion.

The so-called header insertion method gradually inserts the node into the head of the linked list according to the reverse method of the node. Conversely, the tail plug method gradually inserts the node to the end of the linked list in the order of nodes. Relatively speaking, the header insertion method is simpler than the tail Insertion Algorithm, but the generated linked list is in reverse order, that is, the first input node is actually the last node of the linked list. For the sake of habit, we usually use the tail plug method to create a linked list. The following code implements the header and tail plug methods. The code is successfully debugged in Linux.

# Include <stdio. h> # include <stdlib. h> typedef struct link {char data; struct link * Next;} linklist; linklist * createlist_front (); // create a single-chain table linklist * createlist_end () using the header insertion method (); // create a single-chain table void showlinklist (linklist * h) by means of the end plug; // The output display list int main (void) {int choice; linklist * head; // head = (linklist *) malloc (sizeof (linklist); While (1) {printf ("create a single-chain table \ n"); printf ("1. use the header insertion method to create a single-chain table \ n "); printf (" 2. create a single-chain table \ n "); printf (" 3. output \ n "); print F ("4. exit \ n "); printf (" make the choice: \ n "); scanf (" % d ", & choice); Switch (choice) {// head Insertion Method Case 1: head = createlist_front (); break; // tail plugging Case 2: Head = createlist_end (); break; // output linked list Case 3: showlinklist (head); break; // exit the program case 4: Return 0; break; default: break;} return 1;} linklist * createlist_front () {linklist * head, * P; char ch; head = NULL; printf ("input character data in turn ('#' indicates that the input ends): \ n"); CH = getchar (); While (Ch! = '#') {P = (linklist *) malloc (sizeof (linklist); P-> DATA = CH; P-> next = head; head = P; ch = getchar (); // The simple core of the header Insertion Algorithm is P-> next = head; head = P;} return head;} linklist * createlist_end () {linklist * head, * P, * E; char ch; head = NULL; E = NULL; printf ("Please input character data in sequence ('#' indicates that the input ends ): \ n "); CH = getchar (); While (Ch! = '#') {P = (linklist *) malloc (sizeof (linklist); P-> DATA = CH; If (Head = NULL) // first judge whether the input is the first node {head = P;} else {e-> next = P; // e always points to the last node} e = P; ch = getchar ();} If (E! = NULL) // If the linked list is not empty, the next node of the last node is empty {e-> next = NULL;} return head; // The tail insertion method is more complex than the head insertion method, in the program, two judgments are made to determine the first node and the last node respectively. And consumes one more pointer variable E .} Void showlinklist (linklist * h) {linklist * P; P = H; while (P! = NULL) {printf ("% C", p-> data); P = p-> next;} printf ("\ n ");}

The code above shows that the tail insertion method is indeed more complex than the header insertion method, and there are two more judgments. But this can be solved. By adding a header node, this node does not store data domains, but stores pointer domains pointing to the next node. In this way, two judgments can be avoided. The entire process should be clear. The following is the implementation code for adding a header node and a suffix insertion method:

# Include <stdio. h> # include <stdlib. h> typedef struct list {char data; struct list * Next;} linklist; linklist * createlist_end (); // create the linked table void showlinklist (linklist * h) by means of tail insertion ); // output the display list int main (void) {linklist * head; printf ("Create a linked list (simplified version) \ n") using the tail plug method "); printf ("Please input character data in sequence ('#' indicates the end of the input): \ n"); Head = createlist_end (); // create a linked list showlinklist (head ); // output linked list} linklist * createlist_end () {linklist * head, * P, * E; char ch; head = (linklist *) malloc (S Izeof (linklist); E = head; // Let E point to the header node CH = getchar (); While (Ch! = '#') {P = (linklist *) malloc (sizeof (linklist); P-> DATA = CH; e-> next = P; // Add the new node to the end of the table E = P; // point the pointer to the new node CH = getchar ();} e-> next = NULL; // leave the return head empty in the pointer field of the End Node;} void showlinklist (linklist * h) {linklist * P; P = H-> next; while (P! = NULL) {printf ("% C", p-> data); P = p-> next;} printf ("\ n ");}

After adding a header node, should the code be clearer?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.