什么是链表? (What is linked list?)
A linked list is a data structure used for storing data. A linked list has the following properties:
链表是用于存储数据的数据结构。 链表具有以下属性:
Successive elements are connected by pointers.
连续元素通过指针连接。
The last element points to NULL.
最后一个元素指向NULL。
Can grow or shrink in size during program execution.
可以在程序执行期间增大或缩小大小。
Can be made as long as needed. (Until system memory exhausts).
可以根据需要制作。 (直到系统内存耗尽)。
Doesn’t waste memory space but takes some extra memory for pointers. It allocates memory as it grows.
不会浪费内存空间,但会为指针占用一些额外的内存。 它随着增长分配内存。
链接列表作为ADT (Link list as an ADT)
Link list is an ADT and has the following operations:
链接列表是ADT,具有以下操作:
Main link list ADT operation:
主链接列表ADT操作:
Insert: Insert element to the list
插入:将元素插入列表
Delete: deletes element from the list
删除:从列表中删除元素
Auxiliary link list ADT operations:
辅助链接列表ADT操作:
Delete list: delete and remove the whole list
删除列表:删除并删除整个列表
Count: returns the number of elements in the list
Count:返回列表中的元素数
Find nth node: Returns the nth node from the beginning of the list
查找第 n 个节点:从列表的开头返回第n个节点
为什么链接列表? (Why Link list?)
There are so many data structure all serves the same purpose like link list. So it must come in mind that why link list, not the others.
链接列表有这么多的数据结构都起着相同的作用。 因此必须记住,为什么要列出链接,而不是其他链接。
Before discussing link list it’s necessary to differentiate link list and array. Both are used for same purpose, but we need to differentiate their usage, their operations. That means where using array is suitable and where linked list.
在讨论链接列表之前,有必要区分链接列表和数组。 两者的用途相同,但我们需要区分其用法和操作。 这意味着在哪里使用数组是合适的,在哪里是链表。
数组和链接列表之间的比较 (Comparison between array and linklist)
Advantage of Arrays:
阵列的优势:
Simple and easy to use
简单易用
Faster access to elements (O (1))
更快地访问元素(O(1))
Disadvantage of Arrays:
数组的缺点:
Wastage of memory as indices may be empty.
内存浪费,因为索引可能为空。
Fixed size - Size of array is static.
固定大小-数组大小为静态。
To insert an element at a given position, operation is complex. We may need to shift the existing elements to create vacancy to insert the new element at desired position.
要在给定位置插入元素,操作很复杂。 我们可能需要转移现有元素以创建空位,以将新元素插入所需位置。
However, problem arising for static array can be overcome through dynamic array.
但是,通过动态数组可以解决静态数组出现的问题。
Advantage of link lists:
链接列表的优点:
Can be expanded at constant time, which is expensive for an array
可以在恒定时间进行扩展,这对于阵列来说是昂贵的
No wastage of memory
不会浪费内存
Insertion at desired position is relatively easier that inserting in an array
插入所需位置比插入数组相对容易
Disadvantage of link lists:
链接列表的缺点:
The main disadvantage is that the access time for any single element in a link list is not constant like an array.
主要缺点是链接列表中任何单个元素的访问时间都不像数组那样恒定。
Link list somehow needs additional code to be implemented.
链接列表某种程度上需要实现其他代码。
Comparison between link list, array & dynamic array
链接列表,数组和动态数组之间的比较
Parameter | Link list | Array | Dynamic array |
---|---|---|---|
Indexing | O(N) | O(1) | O(1) |
Insertion/deletion at beginning | O(1) | O(N)(array not full) | O(N) |
Insertion at the end | O(N) | O(1)(array not full) | O(1)... array not full O(N)... array full |
Deletion at the end | O(N) | O(1) | O(N) |
Insertion in the middle | O(N) | O(N)(array not full) | O(N) |
Deletion in the middle | O(N) | O(N)(array not full) | O(N) |
Wasted space | For pointers | For empty indices | Depends on allocation |
参数 | 连结清单 | 数组 | 动态数组 |
---|---|---|---|
索引编制 | 上) | O(1) | O(1) |
开始插入/删除 | O(1) | O(N)(数组未满) | 上) |
最后插入 | 上) | O(1)(数组未满) | O(1)...数组未满 O(N)...数组已满 |
最后删除 | 上) | O(1) | 上) |
插入中间 | 上) | O(N)(数组未满) | 上) |
中间删除 | 上) | O(N)(数组未满) | 上) |
浪费空间 | 对于指针 | 对于空索引 | 取决于分配 |
翻译自: https://www.includehelp.com/data-structure-tutorial/introduction-to-linked-list.aspx