Data Structure and Algorithm Design - Head Insertion and Tail Insertion of Linked Lists

One: Linked list components
Linked list is composed of several nodes, and each node is divided into two parts: data field and pointer field. The data field is used to store data elements, and the pointer stores the address of the next node (next data) to facilitate finding the data.

struct Link{
    
    
    int date;
    struct Link *next;
};

Two: Create a linked list
1. Tail insertion method creation

struct Link * create_Link_tail(struct Link *head)
{
    
    
    struct Link* tail = (struct Link*)malloc(sizeof(Link));
    tail = head;
    int len;
    cout<<"len:"<<endl;
    cin>>len;
    for(int i=0;i<len;++i)
    {
    
    
        struct Link* temp = (struct Link*)malloc(sizeof(Link));
        cin>>temp->date;
        tail->next=temp;
        tail = temp;
    }
    tail->next =NULL;
    return head;
}

Create a transition pointer that is exactly the same as the incoming head pointer, always pointing to the new node temp (that is, always pointing to the end of the linked list), and you can add a new node behind the head pointer without moving the head pointer.

2. Head insertion method

struct Link* create_Link_head(struct Link* head)        
{
    
    
    int len;
    cout<<"len:"<<endl;
    cin>>len;
    for(int i=0;i<len;++i)
    {
    
    
        struct Link* temp = (struct Link*)malloc(sizeof(Link));
        cin>>temp->date;
        temp->next = head->next;
        head->next = temp;
    }
    return head;
}

The next pointer of the newly created node points to head->next (that is, the first Link node), then the newly created node naturally becomes the new first node, that is, head->next.

Three: linked list traversal

void traverse_Link(struct Link* head)       //遍历链表
{
    
    
    struct Link* ptr = (struct Link*)malloc(sizeof(Link));
    ptr = head->next;
    while(ptr!=NULL)
    {
    
    
        cout<<ptr->date<<endl;
        ptr = ptr->next;
    }
}

Create a temporary pointer to point to head->next, that is, the first element, head is the head, not the first. Then use the while loop to judge whether ptr is empty, if not, take out the date value inside, and exit the loop if it is empty, and the traversal ends.

Four: Summary
The linked list and my code above are very simple and easy to understand. If you don’t understand, you can try this method: If you create a node: Link jiedian1; the next node of this node is jiedian2. Rote memorization: jiedian->next refers to jiedian2, that is, jiedian1->next->date is jiedian2->date. Remember to look at the code and it will be obvious.

The whole code is as follows:

#include <iostream>
using namespace std;

struct Link{
    
    
    int date;
    struct Link *next;
};

struct Link * create_Link_tail(struct Link *head)
{
    
    
    struct Link* tail = (struct Link*)malloc(sizeof(Link));
    tail = head;
    int len;
    cout<<"len:"<<endl;
    cin>>len;
    for(int i=0;i<len;++i)
    {
    
    
        struct Link* temp = (struct Link*)malloc(sizeof(Link));
        cin>>temp->date;
        tail->next=temp;
        tail = temp;
    }
    tail->next =NULL;
    return head;
}

struct Link* create_Link_head(struct Link* head)        
{
    
    
    int len;
    cout<<"len:"<<endl;
    cin>>len;
    for(int i=0;i<len;++i)
    {
    
    
        struct Link* temp = (struct Link*)malloc(sizeof(Link));
        cin>>temp->date;
        temp->next = head->next;
        head->next = temp;
    }
    return head;
}

void traverse_Link(struct Link* head)       //遍历链表
{
    
    
    struct Link* ptr = (struct Link*)malloc(sizeof(Link));
    ptr = head->next;
    while(ptr!=NULL)
    {
    
    
        cout<<ptr->date<<endl;
        ptr = ptr->next;
    }
}

int main(int argc, char const *argv[])
{
    
    
    struct Link *Head = (struct Link*)(malloc(sizeof(Link)));
    create_Link_head(Head);
    traverse_Link(Head);
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_46061085/article/details/120471862