Summary of other forms of linked list

Posted by eco on Fri, 03 Jan 2020 06:51:25 +0100

1, Two way list
1, definition
There are two pointer fields in the nodes of the two-way linked list, one is direct successor, the other is direct precursor, which can be described as follows

//Library function header file contains
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>

//Definition of function status code
#define TRUE        1
#define FALSE       0
#define OK          1
#define ERROR       0
#define INFEASIBLE -1
#define OVERFLOW   -2

typedef int  Status;
typedef int  ElemType; //Assume that all elements in the linear table are integers

typedef struct DuLNode
{
    ElemType data; // Data domain
    struct DuLNode *prior; // Pointer field to precursor
    struct DuLNode *next;// Pointer field to follow
}DuLNode,*DuLinkList;

2, Circular list
1, definition
Circular list is another form of chain storage structure. Its characteristic is that the pointer field of the last node in the table points to the head node, and the whole linked list forms a ring. Therefore, other nodes in the table can be found from any node in the table.

2. The operation of the circular list is basically the same as that of the linear list. The difference is that the circular condition in the algorithm is not whether p or p - > next is null, but whether they are equal to the head pointer.

3, Two way circular list
1,

2. In bi-directional linked list, some operations such as ListLength, GetElem and LocateElem only need to involve pointers in one direction, so their algorithm description is the same as that of linear linked list, but they are very different when inserting and deleting. In bi-directional linked list, the pointers in two directions need to be modified at the same time.

3. Double linked list inserting data

Status ListInsert_DuL(DuLinkList& L,int i,ElemType e)//Insert e element at position i
{
    DuLinkList s;
    DuLinkList p=L;
    int j=0;
    while(p->next!=L&&j<i-1)
    {
        p=p->next;
        j++;
    }
    if(p->next==L||j>i-1) return ERROR;
    s=(DuLinkList)malloc(sizeof(Node));
    scanf("%d",&s->data);
    s->next=p->next;p->next=s;
    s->next->prior=s;//New plus
    s->prior=p;//New plus
    return OK;
}

Be careful:
(1) What's different from a one-way list is that

s->next = p->next;    p->next = s;
s->next->prior = s;    s->prior = p;

4. Double linked list delete data

Status ListDelete_DuL(DuLinkList& L,int i,ElemType e)//Delete element at position i
{
    DuLinkList s,p=L;
    int j=0;
    while(p->next!=L&&j<i-1)
    {
        p=p->next;
        j++;
    }
    if(p->next==L||j>i-1) return ERROR;
    s=p->next;
    e=p->next->data;
    p->next=p->next->next;
    p->next->prior = p;//New plus
    free(s);
    return OK;
}