[Algorithm basis] Data structure | Singly linked list + double linked list code implementation + diagram + principle

insert image description here

foreword

Because Yao Yaozi is preparing for the Blue Bridge Cup and the ACM selection competition in the school, and is learning about algorithms recently. I learned with the help of the AcWing website. This article is a note on my learning content. Some of the explanations of the principles are friends who may see bullet screens or comments during my learning process. I think it is very good . It makes sense and I was enlightened, so I quoted it.
This article is about data structures, mainly using arrays to simulate various data structures, mainly to improve algorithm efficiency.
For some places that are relatively obscure and bald, I am used to drawing pictures to understand, so you will see my carefully drawn diagrams based on algorithm templates or topics. I hope it can help students who are learning algorithms together!
Because Yaoyaozi is still a rookie at the moment, there may be places where I don’t understand well, or where I can understand better. Please point out more!

Notice! The explanation method of each data structure below adopts code template + text description & explanation + diagram

1. Singly linked list (adjacency list)

Function: mostly used for adjacency list , storage graph and tree

code template

// head存储链表头,e[]存储节点的值,ne[]存储节点的next指针,idx表示当前用到了哪个节点,后一个
int head, e[N], ne[N], idx;
//NULL相当于-1,所以head = -1相当于head=NULL
// 初始化
void init()
{
    
    
    head = -1;
    idx = 0;
}

// 在链表头插入一个数a
void insert(int a)
{
    
       
    e[idx] = a, ne[idx] = head, head = idx ++ ;
}
//将x插入到下标是k的点之后
void insert(int k, int x)
{
    
    
	e[idx] = x;
	ne[idx] = ne[k]
	ne[k] = idx;
	idx ++;
}

// 将头结点删除,需要保证头结点存在
void remove()
{
    
    
    head = ne[head];
}
  • The storage node array e[N]and the next pointer array storing the node ne[N]are associated through subscripts
  • idx only records the position of the current operation, and the generally implemented linked list idx is out of order (the array subscripts of the front and rear nodes do not need to be continuous, and the next idx needs to be found through the current ne[i]. This is also the connection between the two .
  • At the beginning of the head =-1, this -1 is equivalent to the physical address NULL, indicating that the linked list is empty, that is, the head points to a head node, and the head interpolation method is used to cleverly make this empty node a tail node . The pointer field of the last node of the singly linked list realized by the association structure is NULLtherefore, the array realizes the last node of the singly linked list, assuming it is i, then ne[i]=-1;
  • What is simulated here is that there is no head node, and the head pointer directly points to the singly linked list of the head node
  • Although the linked list simulated by the array is not well understood by the structure/class simulation, the essence is the same. We can compare it to a node Node.
  • So in fact, when drawing a picture, there is no need to divide it so clearly. In fact, before I learned about arrays and linked lists, I always thought that arrays & linked lists belonged to physical structures . Only now did I find out that linked lists are actually a logical structure!
comparison point Struct/class mock Node Array analog Node
node itself pointer physical address, node By subscripting the array, it represents its own pointer
Numeric field Just define it in the structurenode.val val[node], through the array to store the value field
pointer field defined in the structure,node. next next[node], stored in an array

Graphical
insert image description here
insertion operation (head insertion method)
insert image description here

2. Double linked list

After learning about arrays to simulate single-linked lists, double-linked lists are actually easy to understand. In fact, there is one more pointer field.

  • Singly linked list: ne[i] stores the next pointer of the node whose pointer is i
  • double linked list
    • l[i], the predecessor of the node whose pointer is i (pointing to the previous node)
    • r[i], the backdriver of the node whose pointer is i (pointing to the next node)
//e[index],表示节点的值,l[index]表示节点的左指针,r[index]表示节点的右指针,idx表示当前用到哪个节点的”地址“

int e[N],l[N],r[N],idx;

//初始化
void init(){
    
    
  //0是左端点,1是右端点
  r[0] = 1,l[1] = 0;
  idx = 2;
}
//在节点a的右边插入一个数x
void insert(int a,int x){
    
    
  //1、让待插入节点占位
  e[idx] = x;
  //2、处理待插入点左右两侧
  l[idx] = a,r[idx] = r[a];//注意,这里必须是r[a],因为a的下一个节点不一定和a顺序存储
  //3、处理前一个节点和后一个节点
  l[r[a]] = idx,r[a] = idx++;
}

insert image description here

insert image description here
Java Island Adventures [Road from Little White to Boss]

LeetCode Daily Question – Attack on the Big Factory

Guess you like

Origin blog.csdn.net/Yaoyao2024/article/details/129621855