Algorithm & Data Structure - Static Linked List, Circular Linked List, Doubly Linked List of Linear List

        In addition to the most common single-linked list introduced above, this article briefly introduces other linear linked storage structure tables. There are more codes in this article.

Table of contents

static linked list

Insertion and deletion operations

Advantages and disadvantages

circular linked list

Doubly linked list

Linear table summary


static linked list

        If there is no pointer, the pointer cannot be used to point to the next element address, and the linked list cannot be implemented as we said before. So how can we store both the information of the data field (data) and the information of the pointer field (cur) ?

        We use an array to describe a linked list, and such a linked list is called a static linked list.

#define MAXSIZE 1000 /* 存储空间初始分配量 */

/* 线性表的静态链表存储结构 */
typedef struct 
{
    ElemType data;
    int cur;  /* 游标(Cursor) ,为0时表示无指向 */
} Component,StaticLinkList[MAXSIZE];

Insertion and deletion operations

/*  插入  */
Status ListInsert(StaticLinkList L, int i, ElemType e)   
{  
    int j, k, l;   
    k = MAXSIZE - 1;   /* 注意k首先是最后一个元素的下标 */
    if (i < 1 || i > ListLength(L) + 1)   
        return ERROR;   
    j = Malloc_SSL(L);   /* 获得空闲分量的下标 */
    if (j)   
    {   
		L[j].data = e;   /* 将数据赋值给此分量的data */
		for(l = 1; l <= i - 1; l++)   /* 找到第i个元素之前的位置 */
		   k = L[k].cur;           
		L[j].cur = L[k].cur;    /* 把第i个元素之前的cur赋值给新元素的cur */
		L[k].cur = j;           /* 把新元素的下标赋值给第i个元素之前元素的ur */
		return OK;   
    }   
    return ERROR;   
}




/*  删除  */
Status ListDelete(StaticLinkList L, int i)   
{ 
    int j, k;   
    if (i < 1 || i > ListLength(L))   
        return ERROR;   
    k = MAXSIZE - 1;   
    for (j = 1; j <= i - 1; j++)   
        k = L[k].cur;   
    j = L[k].cur;   
    L[k].cur = L[j].cur;   
    Free_SSL(L, j);   
    return OK;   
} 

Advantages and disadvantages

advantage

  • Modifying the cursor during insertion and deletion does not require moving elements, which improves the original complex insertion and deletion operations in the sequence structure

shortcoming 

  • It is difficult to determine the table length of continuous storage space;
  • Lost the random access feature.

circular linked list

The pointer field of the last node points to the head node, and the linked list is closed.

Join two circular linked lists:

p=rearA->next;   			    /* 保存A表的头结点 */
rearA->next=rearB->next->next;	/* 将本是指向B表的第一个结点(不是头结点)*/
                 				/* 赋值给reaA->next*/
q=rearB->next;
rearB->next=p;				   	/* 将原A表的头结点赋值给rearB->next */
free(q);					   	/* 释放q */

Doubly linked list

        Our pointers point down in order, which is irreversible in the singly linked list, that is, the next node can be found through the previous node, but the previous node cannot be found in the next node. In each node, set a pointer to the previous node , which becomes a doubly linked list.

 What if it is a circular double necklace list?

/*线性表的双向链表存储结构*/
typedef struct DulNode
{
		ElemType data;
		struct DuLNode *prior;    	/*直接前驱指针*/
		struct DuLNode *next;		/*直接后继指针*/
} DulNode, *DuLinkList;



p->next->prior = p = p->prior->next

s - >prior = p;   			/*把p赋值给s的前驱*/
s -> next = p -> next;		/*把p->next赋值给s的后继*/
p -> next -> prior = s;		/*把s赋值给p->next的前驱*/
p -> next = s;				/*把s赋值给p的后继*/


p->prior->next=p->next;   	/*把p->next赋值给p->prior的后继*/
p->next->prior=p->prior;	/*把p->prior赋值给p->next的前驱*/
free(p);					/*释放结点*/

Linear table summary

        A linear table is the most basic, simplest, and most commonly used data structure. A linear list is a kind of data structure, and a linear list is a finite sequence of n data elements with the same characteristics.

Welcome to like, collect, and comment in the comment area, and reprint to indicate the source.

-----------------------------

Above link:

Algorithm & Data Structure - Linear Table and Its Chained Storage Structure . https://blog.csdn.net/qq_52213943/article/details/125824927

Links below:

Algorithm & Data Structure - Basic Concepts Related to the Stack . All data can only be stored or retrieved at the top of the stack at the floating end, strictly following the principle of "first in, last out". The elements in the middle must be pushed in at the top of the stack and moved out one by one. can only be removed afterwards. ... https://blog.csdn.net/qq_52213943/article/details/126354699

Guess you like

Origin blog.csdn.net/qq_52213943/article/details/125879485