当前位置:网站首页>Analysis of the difference between array and linked list
Analysis of the difference between array and linked list
2022-07-02 15:46:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
1. What is a linked list
A linked list is a storage structure in which the reference of the previous element points to the next element , The linked list connects elements with elements through pointers ;
Linked list is a kind of linear list , The so-called linear list includes sequential linear list and linked list , Sequential linear table is realized by array , There is a sequential arrangement in memory , By changing the size of the array . The linked list is not implemented in order , Use a pointer to achieve , Discontinuous in memory . That is to say , Linked list is to connect a series of discontinuous memory , Make rational use of that fragmented memory , Solve the problem of space .
therefore , Linked list allows you to insert and delete nodes anywhere on the list , But random access is not allowed . There are many different types of lists : One way linked list 、 Two way linked list and circular linked list .
2. One way linked list
The one-way linked list contains two fields , One is the information domain , One is the pointer field . That is, the nodes of the one-way linked list are divided into two parts , One part is to save or display information about the node , The second part stores the address of the next node , The last node points to a null value .
3. Double linked list
It's clear from the picture above that , Each node has 2 A link , One refers to the previous node ( When this link is the first link , Point to null value or empty list ), The other one points to the latter node ( When this link is the last link , Point to null value or empty list ). It means that the two-way linked list has 2 A pointer to the , One refers to the pointer to the previous node , The other is a pointer to the next node .
4. Circular linked list
Circular linked list is that the first node and the end node are connected . The first node in the circular linked list is the last node , vice versa .
5. The difference between arrays and linked lists ?
Different : The linked list is linked Storage structure ; Arrays are sequential Storage structure .
The linked list connects elements with elements through pointers , An array stores all the elements in order .
The insertion and deletion elements of the linked list are relatively simple compared with the array , No need to move elements , And it is easy to realize length expansion , But finding an element is more difficult ;
It's easier to find an element in an array , But insertion and deletion are complex , Due to the maximum length, it needs to be specified at the beginning of programming , So when the maximum length is reached , The extended length is not as convenient as the linked list . identical : Both structures can realize the sequential storage of data , The constructed model is Linear structure .
6. The application of linked list 、 Code practice
Joseph's question :
It is said that in the park 1 In the Jewish War of the th century , Jewish Joseph is a famous historian in the first century AD . After the Romans took jotapat ,39 A Jew hid in a cave with Joseph and his friends ,39 A Jew decided that he would rather die than be captured by the enemy , So I decided to commit suicide through the ages ,41 Individuals in a circle , From the first 1 Individual starts counting , Every check-in day 3 You have to kill yourself , Then the next person will count off again , Until everyone killed himself . But Joseph and his friends didn't want to follow the agreement , Joseph asked his friend to pretend to obey , He arranged his friend and himself in the _ And the first _ A place , So I escaped the game of death , You know what's arranged in the first few ?
For the above problems , Use one-way circular linked list to solve :
// Node class
function Node(elemnt) {
this.item = elemnt;
this.next = null;
}
// The circular list needs to modify the constructor , And the judgment conditions during traversal
// The constructor is as follows ; Hope to traverse from back to front , I don't want to establish a two-way linked list , Just use the circular linked list .
function Llist() {
this.head = new Node("1");
this.head.next = this.head;
this.remove = remove;
this.insert = insert;
this.find = find;
this.display = display;
//..........
}
function find(number) {
var curr = this.head;
while (curr.item != number) {
curr = curr.next;
}
return curr;
}
function insert(element, newElement) {
var preNode = this.find(element);
var current = new Node(newElement);
current.next = preNode.next;
preNode.next = current;
}
function remove() {
var current = this.head;
console.log("remove");
// Skip two , Kill one
while(current.next.next != null && current.item!=current.next.next.item){
var temp = current.next.next;
current.next.next = temp.next;
current = temp.next;
temp.next = null;
}
return current;
}
function display(flag,current) {
var crr = this.head;
if(flag){
while(crr.next.item!="1"){
console.log(crr.item);
crr=crr.next;
}
}else{ // Finally, there are only two direct outputs
console.log(current.item);
console.log(current.next.item);
}
}
var Clist = new Llist();
// Enter sort
for (var i = 1; i < 41; i++){
Clist.insert(i.toString(),(i + 1).toString());
}
// Output all first
Clist.display(1,null);
// adopt remove Return one of the nodes after the last kill
Clist.display(0,Clist.remove()); //16,31The way of organizing code should be learned ;
7. Self understanding
1) Arrays are easy to query and modify , However, it is inconvenient to add and delete
2) Linked lists are suitable for adding and deleting , But it is not suitable for query , Using appropriate data structures and algorithms according to business conditions is a problem that must be considered when there is a large amount of data and high concurrency
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/147716.html Link to the original text :https://javaforall.cn
边栏推荐
- Thoroughly understand browser strong cache and negotiation cache
- 爱可可AI前沿推介(7.2)
- Ant group's large-scale map computing system tugraph passed the national evaluation
- 将点云坐标转换成世界坐标的demo
- 2303. 计算应缴税款总额
- 【LeetCode】417-太平洋大西洋水流问题
- 2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)
- List of sergeant schools
- 夏季高考文化成绩一分一段表
- PostgresSQL 流复制 主备切换 主库无读写宕机场景
猜你喜欢

Soul torture, what is AQS???

Finally, I understand the event loop, synchronous / asynchronous, micro task / macro task, and operation mechanism in JS (with test questions attached)
![[development environment] install the Chinese language pack for the 2013 version of visual studio community (install test agents 2013 | install visual studio 2013 simplified Chinese)](/img/cf/38e4035c3b318814672f21c8a42618.jpg)
[development environment] install the Chinese language pack for the 2013 version of visual studio community (install test agents 2013 | install visual studio 2013 simplified Chinese)

《大学“电路分析基础”课程实验合集.实验六》丨典型信号的观察与测量

Redux - detailed explanation

Comparison between rstan Bayesian regression model and standard linear regression model of R language MCMC

Experiment collection of University "Fundamentals of circuit analysis". Experiment 6 - observation and measurement of typical signals

树-二叉搜索树

怎样从微信返回的json字符串中截取某个key的值?

PHP static members
随机推荐
Moveit 避障路径规划 demo
[leetcode] 977 - carré du tableau ordonné
PTA ladder game exercise set l2-001 inter city emergency rescue
Pyobject to char* (string)
[leetcode] 577 reverse word III in string
03. Preliminary use of golang
[leetcode] 1140 stone game II
Bing. Com website
使用FFmpeg命令行进行UDP、RTP推流(H264、TS),ffplay接收
MD5 encryption
6090. Minimax games
Fiddler实现手机抓包——入门
【LeetCode】189-轮转数组
The outline dimension function application of small motherboard
Comparison between rstan Bayesian regression model and standard linear regression model of R language MCMC
Basic knowledge of cryptography
SQL修改语句
Leetcode skimming -- count the number of numbers with different numbers 357 medium
02. After containerization, you must face golang
Leetcode question brushing - parity linked list 328 medium