何时实际使用链表

我们都在学校或训练营中学习了不同的复杂数据结构。 链接列表,哈希图,二叉树和搜索树,堆栈,队列,单调队列等。我们还都学习了如何编写每一个,如何遍历节点,添加节点和删除节点。但如果我们实际上不知道什么时候去了解所有这些东西,那么有什么好处呢?使用这些数据结构..

Linked Lists

简要回顾一下,让我们回顾一下链接列表。 链接列表是一系列“节点”,其中包含值以及指向该系列中下一个节点的指针。 在链接列表中,您可以访问列表的“头”,并且通过逐个遍历列表可以找到所有后续节点。 双向链表具有相同的属性,除了还保留了对“ tail”的引用,并且节点也具有对先前节点的引用,并且可以反向遍历该列表。 通常将链接列表作为相似的数据结构与数组进行比较,尽管数组是“原始”数据结构,但它们与链接列表共享相似之处。

Similarities

例如,它们都需要遍历才能访问结构中的所有元素,并且它们都可以用于存储相似类型的线性数据。

Differences

要真正注意到差异,您需要使用较旧的编译语言(例如C ++,Java或C#)进行编程,其中数组的长度是固定的。

这是因为每个数组的内存是在编译时分配的,而链表是在运行时分配的。 这对链接列表很有用,因为可以在运行时动态调整它们的大小,而更改数组的大小会涉及创建长度更长的新数组并使用更多的内存,或者您必须事先知道所需长度的上限,这会浪费 内存空间。 在JavaScript,Python和Ruby中,这不是问题,可以使用.push()和.pop()等操作来动态调整数组的大小 但是,数组的内存分配比链接列表更有效,因为每个索引的数据都直接存储在每个索引的分配中,而链接列表必须存储一个值以及指向下一个(或上一个和下一个的指针) 在双链表中)节点。 Arrays can also reference a particular element using an index, so to get the 5th element:
    String[] array = new String[10]; //initializes new array of strings with length 10
    String fifth = array[4]; //access the fifth element in the array (constant time) 

and so on, while linked lists require access to the head, and then a loop to traverse the elements:

LinkedList<String> linkList = new LinkedList<String>();
 //initializes a new linkList with type string. (no length specified)
 linkList.search(4);

//then inside the linkList class:
public <T> search(int input){ 
  head = current;
  int counter = 1;
  while(current.next != null){
    if(counter == input){
      return current.value
    }
    else{
      counter++;
      current = current.next;
    } 

Here we are looking for the 4th element of linkList, so we have to iterate through the first three values to get the fourth. Since space time complexity is worst case scenario, the lookup of a value in a linked list is O(n) because it is dependent on the length of the linked list as well as the index you are searching for. An array lookup on the other hand is a constant time-complexity ( O(1) ) because it is a direct lookup to the memory location of an element at a specific index.

链表(尤其是双链表)对于在末端添加和删除节点具有更好的时空复杂性,因为插入(或删除)目标元素只需更改周围元素的指针即可。 要插入中间,复杂度仍然是线性的(上)),因为您必须遍历索引并更新指针。 与数组相比,这里的优势在于空间的复杂性,您必须遍历索引,插入值,然后找到与其余元素有关的方法来重新放置它们。 这可以通过几种不同的方式完成,但是无论如何都需要更多的内存。
Use Cases

因此,查看数组和链接列表之间的关键区别,我们可以看到每种数组的优缺点,并开始就何时使用每种数组得出结论。 链接列表利用它们的关键特征,使内容保持快速有序,真正发光。 现实世界中最常见的应用包括其他复杂的数据结构。 哈希表,图形,堆栈,队列和出队都在内部使用链接列表。

A linked list can be used as a stack by repeatedly adding and removing elements from the 'head' of the list.
  // create stack linked list 
  StackUsingLinkedlist stack = new StackUsingLinkedlist(); 
  // insert Stack value at head of linked list
     stack.push(task1); 
     stack.push(task2); 
     stack.push(task3); 
     stack.push(task4);

  while(!stack.isEmpty()){
    //execute the task at the top of the stack (head of linked list)
    execute(stack.pop());
  }
通过将双链表添加到链表的“头部”并从“尾巴”中删除,可以将其用作队列。 链接列表也可以是哈希表上的存储桶,以防止交叉。 (如果在该哈希位置已经有东西,请将其添加到列表的末尾)。 Other real world applications can include the back button on a browser, an undo button in photoshop, or the cycle of applications running on an operating system.

Conclusion

There is a time and a place to use linked lists and most commonly it's when you want quickly add and remove elements from a container. Usually this occurs in stacks and queues with lower space time complexity over arrays or when you want to keep ordered data with more flexibility than arrays.

Stay tuned next week for part 2 of practical applications: When to actually use stacks.

References:
https://www.geeksforgeeks.org/linked-list-vs-array/
https://www.quora.com/In-which-languages-are-arrays-automatically-resizable-and-in-which-are-they-a-fixed-length

from: https://dev.to//spencerlindemuth/when-to-actually-use-linked-lists-dcf

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值