Basic principle and interface implementation of linked list

Posted by rachae1 on Tue, 01 Feb 2022 00:35:49 +0100

Linked list summary review

1. Basic concept of linked list

1.1 difference between linked list and array

  • Linked list and array are linear storage structures, which are not fundamentally different in logic, because the data stored in logic is continuous, but the data stored in array is continuous in logic and physics, and the linked list is not continuous in physics.
  • Because the linked list is physically discontinuous, the linked storage structure can only be accessed sequentially from the pointer. Due to the physical continuity of the array, the random access of data can be realized.
  • The disadvantage of array is that it requires a lot of data handling when inserting and deleting elements, so it is not suitable for frequent addition and deletion of data structures.

1.2 advantages and disadvantages of linked list and array

  • The configuration diagram is as follows:
  • Both structures have their own advantages and disadvantages, so we should choose the appropriate application scenario and give full play to their respective advantages.

1.3 related terms of chain storage

  • Node: the storage image of the data element (the smallest storage unit). It consists of data field and pointer field.
  • Classification of linked list:
    • Single linked list: a linked list whose node has only one pointer field becomes a single linked list.
    • Double linked list: a linked list with two pointer fields at a node
    • Circular linked list: a linked list connected end to end.
  • Header pointer, header node and primitive node
    • The pointer to the header node is the header pointer
    • The head of the linked list is used to store some node related information.
    • Initial node: the first node that really stores data information.

1.4 two forms of linked list storage structure

  • No lead node
    • The header pointer directly points to the initial node.
  • Leading node
    • The header pointer points to the header node.
  • The configuration diagram is as follows:

1.5 discuss several issues

  • How to represent an empty table
    • When there is a header node, the pointer field of the header pointer is empty, which means that it is an empty table.
    • When there is no header node, a null header pointer indicates an empty table.

2. Research on single linked list

2.1 data structure of single linked list

typedef struct _tag_LinkListNode
{
    ElemType data;
    LinkListNode *next;
}LinkListNode;

typdef struct  _tag_LinkListHeader
{
    int List_lenth;
    LinkListNode *next;
}LinkListHeader;//As the head node, the data field can be used to store the length of the linked list. This node is not used at present.
//Since each node is composed of a data field and a pointer field pointing to the same data type, a structure is used to define a node.
//Using Elemtype, a user-defined data type, to define the data field can flexibly expand the linked list.

2.2 basic operation of single linked list

2.2.1 initialization of linked list

1.Algorithm steps:
- utilize malloc The function requests the memory space of the capacity of the header node.
- The data field of the initialization header node is 0 and the pointer field is NULL
- Take the first address as the return value and pass it to the later header pointer.

2.realization
LinkListNode* LinkList_Init(void)
{
    LinkListNode* new = (LinkListNode*)malloc(sizeof(LinkListNode));
    new->data = 0;
    new->next = NULL;
    return new;
}

2.2.2 judge whether the linked list is empty

1.Algorithm idea
- Whether the pointer field of the head node is empty.
- Whether the linked list pointer is empty or not is not required.
2.realization
Status LinkList_IfEmpty(LinkList *list)
{
    if(NULL != list)
    {
        if (NULL == list->next)
            return Flase;
    }
    return True;
}

2.2.3 destruction of single linked list

1.Algorithm idea
- The head pointer moves continuously to the next node.
- Save the node address that has not been released yet.
- Release the previous node.
2.code
Status List_Destroy(LinkList *list)
{
    LinkList *tmp;
    while(NULL != list)    
    {
        tmp = list;
        list = list->next;
        free(list);
    }
    reurn True;
}

2.2.4 empty the linked list

1. Idea (keep the header pointer and header node, and remove other nodes)
- Continuously switch the last node to the first node.
- Release the singled out initial node,Until the new initial node is empty.
2.algorithm
Status LinkList_Empty(LinkList* list)
{
    LinkListNode *current;
    if (NULL != list)
    {
        current = list->next; //Point to initial node
        while(NULL != current)
        {
            list->next = list->next->next;
            free(current);
            current = list->next;
        }
    }
    return True;
}

2.2.5 find the length of the linked list

1.Algorithm idea
- Start from the initial node and count all nodes in turn.
2.Algorithm implementation
- Count from the initial node and find it all the way NULL
3.code
int LinkList_Lenth(LinkList *list)
{
    int i = 0;
    if (NULL != list)
    {
        LinkListNode *current = list->next;
        while(NULL != current)
        {
            i++;
            current = current->next;
        }
    }
    return i;
}

2.2.6 take the ith value of the single chain list

1.thinking
- Single linked list is a sequential storage structure, so it can only traverse from the beginning of the node, and then take out the data.
2. algorithm
- Get the initial node. Move it several times on the basis of the initial node, and the temporary pointer points to which node.
- The 0-th element does not need to be moved. The first element needs to be moved once,...The law of analogy is that the first element moves several times.
- utilize for perhaps while loop
3. code
void* LinkList_GetEle(LinkList *list,int index)
{
    void *tmp = NULL;
    if(NULL != list)
    {
        if ((0 < index) && (index < LinkList_GetLenth(list))) //inspect
        {
            LinkListNode *current = list->next;//Point to current node
            for(int i=0;i<index;i++)
            {
                current = current->next;
            }
            tmp = (void*)&current->data;
        }
    }
    return tmp;
}

4.Code version 2:(Better search without edge checking in advance)
void* LinkList_GetEle(LinkList *list,int index)
{
    void *tmp = NULL;
    if(NULL != list)
    {
        LinkNode *currnet = list->next;//Get initial node location
        int i = 0;
        while((NULL != current)&&(i<index))
        {
            current = current->next;
            i++;
        }
        //NULL== current indicates that the maximum value is out of bounds; I > index indicates that the minimum value is out of bounds. For example, index is a negative number
        if((NULL != current) && (i == index))
        {
            tmp = (void*)&current->data;
        }
    }
    return tmp;
}

2.2.7 insert nodes at appropriate positions

1.thinking
- Search the previous node of the node to be inserted first
- Insert this node
2.Algorithm steps
- First find a[i-1]Storage location for p
- Generate a new data field e New node for s
- Insert new node
    - The pointer field of the new node points to the node a[i]
    - node a[i-1]Point to new node
3.code
int LinkList_Insert(LinkList *list,int i,ElemType e)   
{
    int j = 0;
    if (NULL != list)
    {
        LinkNode *new = (LinkNode*)malloc(sizeof(LinkNode));
        LinkNode *current = list;
        while(current && (j < i))
        {
            j++;
            current = current->next;
        }
        if(current && (j == i))
        {
            new->next = current->next;
            current->next = new;
        }
    }
    return j;
}

2.2.8 delete nodes at appropriate locations

1.Algorithm steps
- find a[i-1]Location of p,Save the node to be deleted a[i]Your address.
- a[i-1]point a[i+1]
- delete a[i]
2.code
int LinkList_Del(LinkList *list,int i)
{
    int tmp = 0;
    if (NULL != list)
    {
        LinkNode *new;
        LinkNode *current = list;
        int j = 0;
        while(current && (j < i))
        {
            j++;
            current = current->next;
        } 
        if (current && (j == i))
        {
            new = current->next;
            current->next = current->next->next;
            free(new);
            tmp = 1;
        }            
    }
    return tmp;
}

2.2.9 search by value

1.Algorithm steps
- Move node pointer
- Compare data fields
2.code
version1:return the index of Element
int LinkList_Search(LinkList *list,ElemType e)
{
    if (NULL != list)
    {
        LinkNode *current = list->next;
        int i = 1;//Point to the initial node, index = 1;
        while(current && (e != current->data))
        {
            current = current->next;
            i++;
        }
    }
    if (!current)  i = 0;
    return i;
}
version2:return the addr of node
LinkNode* LinkList_Search(LinkList,ElemType e)
{
    if (NULL != list)
    {
        int i = 1;
        LinkNode *current = list->next;//Point to the initial node, index = 1;
        while(current && e != current->data)
        {
            current = current->next;
            i = 2; //Update index
        }
    }
    return current;
}

Topics: data structure