Two methods of creating a singly linked list: head insertion method and tail insertion method

Method 1: Head plug method

Basic idea: define a pointer l of a linked list type. The pointer l points to the first address of the linked list, not the first number of the linked list. The next linked list type pointed to by the pointer l is the first number of the linked list. The addends in the linked list are added to the first position in the linked list (that is, the position pointed to by the pointer l).


Code:

It's best to look at the code and simulate the process on paper.

#include<bits/stdc++.h>
using namespace std
typedef struct Node
{
    int value;
    struct Node *next;
}node,*linkedlist;
linkedlist linkedlistcreath() //returns the address of the linked list
{
    node *l=(node *)malloc(sizeof(node));
    l->next=NULL;
    int number;
    while (scanf("%d",&number)!=EOF)
    {
        node *p=(node*)malloc(sizeof(node) ); //Create a new node structure and apply for space for it
        p->value=number; //Assign the new node structure
        p->next=l->next; //Assign the node pointed to by p (that is, l The node pointed to, that is, the second node of the linked list)
        l->next=p; //Update the node pointed to by l to point p
    }
    return l; //Return the address of the head node

}


The second method: tail insertion

Basic idea: First define a pointer l of a linked list type. The pointer l points to the first address of the linked list, not the first number of the linked list. The next linked list type pointed to by the pointer l is the first number of the linked list, and then Define an r pointer to ensure that the r pointer always points to the node at the last position of the linked list, and then add the newly added node to the back of the node pointed to by the r pointer.


code show as below:

It's best to look at the code and simulate the process on paper.

#include<bits/stdc++.h>
using namespace std;
typedef struct Node
{
    int value;
    struct Node *next;
} node,*linkedlist;
linkedlist linkedlistcreatt() //returns the address of the linked list
{
    node *l=( node*)malloc(sizeof(node));
    l->next=NULL;
    node *r; //r always points to the last node structure of the linked list
    r=l; //this place is the assignment between addresses
, so the operation on r is equivalent to the operation on l, that is, the operation of     int number on the last node structure of the linked list
;     while (scanf("%d",&number)!=EOF)
    {
        node *p=(node*)malloc(sizeof (node)); //Create a new node structure and apply for space
        p->value=number; //Assign the new node structure
        r->next=p; //Insert p into the last node structure of the linked list the back of
        p->next=NULL; //At this time p is the last of the linked list, assign
        r=p to the next of p; //Let r equal to the last node structure of the linked list, that is, the p node
    }
    return l; // Returns the address of the head node

}


Guess you like

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