Two methods for creating a singly linked list (head insertion method and tail insertion method)

Linear table can be divided into sequential storage structure and chain storage structure

The creation of a sequential storage structure is actually the initialization of an array, that is, the process of declaring an array of type and size and assigning a value. The singly linked list is different from the sequential storage structure. The storage location of each data does not need to be centralized like an array. It can be very scattered and is a dynamic structure. For each linked list, the size and position of the space occupied by it do not need to be pre-allocated and delineated, and can be generated instantly according to system conditions and actual needs. Therefore, the process of creating a singly linked list is a process of dynamically generating a linked list . That is, from the initial state of the "empty list", each element node is established at one time and inserted into the linked list one by one.

Entire table creation of singly linked list

There are two main methods for creating a singly linked list , namely head insertion method and tail insertion method . These two methods are introduced below.


Head insertion method to create a singly linked list (with head node)

Steps to create a singly linked list by head insertion method:

1. Declare a pointer variable p and a counter n;

2. Initialize an empty linked list L;

3. Let the pointer of the head node of L point to NULL, that is, create a singly linked list with the head node;

4. Loop:

  •     Generate a new node and assign it to p;
  •     Randomly generate a number and assign it to the data field of p;
  •     Insert p between the head node and the previous new node.

The code implemented by the header insertion method is as follows:

#include<stdio.h>
#include<malloc.h>
#include<time.h>
#include<stdlib.h>

struct node //Create a structure
    {
        int data; //elementtype represents a data type, which may be int/char, etc.
        struct node *next; //next pointer, used for linked list structure to point to the next node
    };

typedef struct node node; //Redefine the type of struct node to node

 node* Creat(int Count) //Create a linked list
    {
        node *p,*head;
        int n;
        srand(time(NULL)); //Generate seed
        head = (node*)malloc(sizeof(node));
		head->data = Count;
        head->next = NULL; //The head node stores the length of the linked list
        for (n = 0;n < Count;n++)
            {
                p = (node*)malloc(sizeof(node));
                p->data = rand()%1000;
                p->next = head->next; //The key two lines of code created by the head insertion method singly linked list
                head->next = p;  
            }
        return(head);
    }


void List(node* head) //Print the linked list
    {
        node * p1;
        p1 = head;
        while(p1!= NULL)
        {
            printf("%4d",p1->data);
            p1 = p1->next;
        }
    }


int main() //main function
    {
	node* head;
        int Num;

	printf("Enter the number of linked list nodes:\n");
	scanf("%d",&Num);
	head = Create(Num);
	printf("\n");
	printf("List:\n");
	List(head);
	printf("\n");
    }

In this algorithm code, we actually use the method of cutting the queue, which is to always let the new node be in the first position. As shown below:


Each new node is inserted at the position after the head node. The so-called insertion is actually to solve two problems for the newly established node: who does it point to and who points to it , respectively in the code by

p->next = head->next; //The new node points to the next node of the original head node
head->next = p; //The head node points to the new node

These two lines of code solve these two problems, then the establishment of the linked list is completed. Since the new node is always in the first position, this method is called the head plug method.

Header insertion method to create a singly linked list result display:


Although this method realizes the creation of a linked list, because it is head insertion, the order of the linked list is reversed, and the order of the linked list created by the tail insertion method described below is in positive order.

Tail interpolation to create a singly linked list

#include<stdio.h>
#include<malloc.h>
#include<time.h>
#include<stdlib.h>

struct node //create structure
    {
        int data; //elementtype represents a data type, which may be int/char, etc.
        struct node *next; //next pointer, used for linked list structure to point to the next node
    };
    
typedef struct node node; //Redefine the type of struct node to node

 node* Creat(int Count) //Create a linked list
    {
        node *p1,*p2,*head;
        int n;
        srand(time(NULL));//Generate seed
        head = p1 = (node*)malloc(sizeof(node)); //p1 is a pointer to the end of the table
		head->data = rand()%1000;
        head->next = NULL;
        for (n = 1;n < Count;n++)
            {
                p2 = (node*)malloc(sizeof(node)); //p2 is the newly applied node
                p2->data = rand()%1000;
                p1->next = p2; //Point the pointer of the end of the table to the new node
                p1 = p2; //Define the current new node as the terminal node at the end of the year table
            }
        p1->next = NULL; //The pointer of the final end node after the end of the loop is assigned NULL
        return(head);
    }
	
	
void List(node* head) //Print the linked list
    {
        node * p1;
        p1 = head;
        while(p1!= NULL)
        {
            printf("%4d",p1->data);
            p1 = p1->next;
        }
    }
	
	
int main() //main function
    {
	node* head;
    int Num;
	
	printf("Enter the number of linked list nodes:\n");
	scanf("%d",&Num);
	head = Create(Num);
	printf("\n");
	printf("List:\n");
	List(head);
	printf("\n");
    }

In this algorithm, every new node is inserted into the end of the table, so it is called the tail insertion method. Pay attention to understanding the code p1=p2. Its function is: as long as the new node is inserted, then he will becomes the tail node. Then keep repeating this operation. The process is as follows:

Tail interpolation method to create a singly linked list result display:


Obviously, the linked list created by the tail insertion method is in positive order.




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325933480&siteId=291194637