Basic usage of linked list

Introduction of linked list concept

struct ListNode
{
    int num;
    ListNode* next;
};

Create an unordered linked list, enter the data of each node in turn, and end with -1. Read the data in a loop, if the data is not -1, apply for a new node, assign the data to the new node, and add the node at the end of the linked list. When adding nodes, it is necessary to divide the situation, (1) the first node (2) the middle node * (3) the tail node

ListNode* Creat()
{
    ListNode *p1,*p2,*head;
    int a;
    head =NULL;
    cin>>a;
    while(a!=-1)
    {
        p1=new ListNode;
        p1->num = a;
        if(head==NULL)
        {
            head =p2=p1;
        }
        else
        {
            p2->next=p1;
            p2=p1;
        }
        cin>>a;
    }
    if(head!=NULL)
        p2->next=NULL;
    return head;
}

The output linked list, the function passes the parameters through the head node, here we should pay attention to use const modification as much as possible to avoid the function from modifying the node data

void print(const ListNode* head)
{
    const ListNode *p;
    p=head;
    while(p!=NULL)
    {
        cout << setw(4)<<p->num;
        p=p->next;
    }
    cout<<endl;
}

Find a specific node, find the node with the data value x in the linked list, and return the pointer of the node, here also use const modified parameters

const ListNode* Search(const ListNode* head,int x)
{
    const ListNode* p;
    p=head;
    while(p!=NULL)
    {
        if(p->num == x)
            return p;
        p=p->next;
    }
    return NULL;
}

To delete a node, such as a node whose value is equal to x, the following program only deletes the first node whose value is x. In the process of deletion, it should be divided into different situations. (1) If it is an empty linked list, it will directly return NULL (2) If the deleted node happens to be the head node, set the next node as the head node (3) If it is another node, delete it normally. The deletion process is also traversed from the beginning node. Here, two pointers are used, one is used to traverse and the other is used to save the previous node. In this way, when deleting, the next node of the previous node can be used to point to the node behind the node.

ListNode* Delete_one_node(ListNode* head,int x)
{
    ListNode *p1,*p2;
    if(head == NULL)
    {
        cout<<"the list is null"<<endl;
        return NULL;
    }
    p1=head;
    while(p1->num!=x && p1->next!= NULL)
    {
        p2=p1;
        p1=p1->next;
    }
    if(p1->num == x)
    {
        if(p1==head)
            head=p1->next;
        else
            p2->next=p1->next;
        delete p1;
        cout<<"delete finished"<<endl;
    }
    else
        cout<<"sorry,dont find the node x"<<endl;
    return head;
}

The release list is also the head node of the parameter list. Here, a pointer is also used to release the nodes one by one, starting from the head node, assigning the head node to the new node, moving the head node back one bit each time, deleting the node, It is equivalent to deleting the original head node

void Delete_all(ListNode* head)
{
    ListNode *p;
    while(head)
    {
        p=head;
        head = head->next;
        delete p;
    }
}

Inserting a node, the premise of this function is that before the function is executed, a linked list with node values ​​in ascending order has been established. After inserting a new node, the linked list is still guaranteed to be in ascending order. When inserting, we must also consider a variety of situations. (1) The linked list is an empty linked list, and the new node is directly used as the head node. (2) Before the head node is inserted, two nodes are still used here, and one p1 is judged from the head node. Traversal, a p2 always points before p1, inserted before the head node, directly inserts the new node as the head node (3) in the middle of the linked list (also before the p1 node), and directly inserts the next of p2 to the new node (4) After the tail node, be careful to assign the next node of the new node to NULL

ListNode* Insert(ListNode* head,ListNode *p)
{
    ListNode *p1,*p2;
    if(head==NULL)
    {
        head=p;
        p->next=NULL;
        return head;
    }
    p1=head;
    while(p->num>p1->num && p1->next !=NULL)
    {
        p2=p1;
        p1=p1->next;
    }
    if(p->num <= p1->num)
    {
        p->next=p1;
        if(head==p1)
            head=p;
        else
            p2->next = p;
    }
    else
    {
        p1->next=p;
        p->next=NULL;
    }
    return head;
}

 

Guess you like

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