The basic Operation-----Graph analysis of single linked list

Source: Internet
Author: User
Tags assert prev

The first thing we need to think about is why we need a single linked list.
What are the advantages of a single linked list and a sequential table?
In a sequential table, when we need a header or insert an element in the middle of a sequential table, we must move the following element one by one back and insert the element that needs to be inserted.
But this is obviously less efficient, so we think of a single linked list of the structure, can be in the physical address of the discontinuous data connection, need to connect, then need to have a save the next element address of the pointer, after thinking, found no such built-in type for us to use, So then we need to customize a type, in C language we can use the structure to construct.

What we're talking about here are headless single linked lists.
Single linked lists are divided into two types: headless single linked list and head single linked list
Headless single linked list, that is, phead-is just a pointer to the first node of the linked list.

Single-linked list of leading nodes: only the data of the header node does not hold the information.

Come down and we'll talk about some of the details of headless single linked lists: For the sake of understanding, I will try to use graphs to express the specific operation:
Build a node First:

1. Initialize single linked list:
Code implementation:

void Initlistnode (Pnode *phead)
{
    assert (phead);
    *phead = NULL;
}

2. Building a node:

Pnode Buynode (DataType _data)
{
    pnode node = (pnode) malloc (sizeof (node));
    if (node)
    {
         node->data = _data;
         Node->next = NULL;
    }
    return node;
}

3. Tail plug
Consider the factor: when the list is empty, direct the phead point to the new node.
When the list is not empty, iterate through the list, find the last linked list, and connect to the back of the last linked list.
Diagram when the list is not empty:

Code implementation:

void pushback (Pnode *phead,datatype _data)
{
    assert (phead);
    Pnode NewNode = Buynode (_data);
    if (NULL = = (*phead))
    {
        *phead = NewNode;
        return;
    }
    Pnode pcur = *phead;
    while (Pcur->next)
    {
        pcur = pcur->next;
    }
    Pcur->next = NewNode;
}

4. End deletion
Considerations: When the linked list is empty, return directly
When there is only one node in the list, free is phead and empty.
When there are multiple nodes in the list, iterate through the list, find the last node (Pcur->next = NULL), and save the last node's information.
As shown in the figure:

Code implementation:

void Popback (Pnode *phead)
{
    assert (phead);
    if (NULL = = (*phead))
    {return
        ;
    }
    else if (NULL = = (*phead)->next)
    {free
        (*phead);
        *phead = NULL;
    }
    else
    {
        Pnode pcur = *phead;
        Pnode prev = pcur;
        while (Pcur->next)
        {
            prev = pcur;
            Pcur = pcur->next;
        }
        Free (pcur);
        Prev->next = NULL;
    }

5. Head Plug
Factors to consider:
When the list is empty: direct the phead to the new node.
When a linked list has a node or multiple nodes:
As illustrated:

Code implementation:

void Pushfront (Pnode *phead,datatype _data)
{
    assert (phead);
    Pnode NewNode = Buynode (_data);
    if (NULL = = (*phead))
    {
        *phead = NewNode;
    }
    else
    {
        if (NewNode)
        {
            newnode->next = *phead;
            *phead = NewNode;
        }
    }

6. Head deletion
Factors to consider:
When the list is empty: return directly, no need to delete
When a list is not empty: when there is only one node: free this node and empty the Phead.
When there are multiple nodes: as shown
Analysis shows that there is a node and multiple nodes can use the same logic

Code implementation:

void Popfront (Pnode *phead)
{
    assert (phead);
    if (NULL = = (*phead))
    {return
        ;
    }
    Pnode Pdel = *phead;
    *phead = (*phead)->next;
    Free (Pdel);
}

7. Reverse print single linked list (recursive):
The process that a function executes when using recursion:

Code implementation:

void Printfromtailtofront (Pnode phead)
{
    if (phead)
    {
        printfromtailtofront (phead->next);
        printf ("%d->", Phead->data);
    }

8. Find a node with a value of data, if present, return to the location, otherwise, return null
You just need to iterate through the list.
Code implementation:

Pnode Find (Pnode phead, DataType _data)
{
    if (null = Phead)
    {return
        null;
    }
    Pnode pcur = phead;
    while (Pcur)
    {
        if (Pcur->data = = _data)
        {return
            pcur;
        }
        Pcur = pcur->next;
    }
    return NULL;
}
Insert a node
void Insertnode (Pnode pos,datatype _data)
{
    if (pos)
    {
        Pnode NewNode = Buynode (_ data);
        if (NewNode)
        {
            Newnode->next = pos->next;
            Pos->next = NewNode;
        }
    }

9. Insert a node (because it is a single linked list, so it can only be inserted at the back of the POS position)
Factors to consider:
① Check parameters (whether the linked list exists, whether the POS position is empty)
② when POS location is not empty: as shown

Code implementation:

void Insertnode (Pnode pos,datatype _data)
{
    if (pos)
    {
        Pnode newNode = Buynode (_data);
        if (NewNode)
        {
            Newnode->next = pos->next;
            Pos->next = NewNode;
        }
    }

10. Delete a node at the POS location:
Factors to consider:
① list is empty and Pos is empty, return directly
②pos is not null and POS is 1 o'clock, this can be converted to delete the first node, the operation step with the top of the head delete
③ when the POS is not the first node, the analysis is as shown:

Code implementation:

void Erase (pnode* phead, Pnode Pos)
{
    assert (phead);
    if ((null = *phead) && (null = = pos))
    {return
        ;
    }
    if ((*phead) = = pos)
    {
        *phead = pos->next;
        Free (POS);
    else
    {
        Pnode pcur = *phead;
        while (Pcur->next!= pos)
        {
            pcur = pcur->next;
        }
        Pcur->next = pos->next;
        Free (POS);
    }


11. Delete a node in a single linked list that has a value of _data:
Code implementation:

void Remove (pnode* phead, DataType _data)
{
    assert (phead);
    Erase (Phead, find (*phead,_data));
}

12 Delete all nodes in the single list that are _data:
Need to consider:
① the value of the first node is _data, because deleting the first node requires modifying the Phead value, so it is time to handle the value of the first node separately _data.
Deleting the first node is the head deletion mentioned above.
② if the value of the first node is not _data, you can handle it directly.
The following processing of the first value is not _data (where the _data is 2):

Code implementation:

void RemoveAll (Pnode *phead, DataType _data)
{
    assert (phead);
    if (NULL = = (*phead))
    {return
        ;
    }
    Pnode Pdel = *phead;
    if ((*phead)->data = = _data)
    {
        *phead = (*phead)->next;
        Free (Pdel);
    }
    Pnode pcur = *phead;
    Pnode prev = pcur;
    while (Pcur)
    {
        if (Pcur->data = = _data)
        {
            Prev->next = pcur->next;
            Free (pcur);
            Pcur = prev->next;
        }
        else
        {
            prev = pcur;
            Pcur = pcur->next;
        }}}    

13. Print single linked list:
Code implementation:

void Printlist (Pnode phead)
{
    Pnode pcur = phead;
    while (Pcur)
    {
        printf ("%d", pcur->data);
        Pcur = pcur->next;
    }
    printf ("\ n");
}

14. Get the number of nodes in a single linked list:
Code implementation:

size_t Size (pnode phead)
{
    size_t count = 0;
    while (Phead)
    {
        Phead = phead->next;
        count++;
    }
    return count;
}

The order of single linked list will be summed up in the following questions of single chain surface.

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.