Data Structures and Algorithms: list

banner


First, the difference between linked lists and arrays

Linked list arrays similar, but the list is a slightly more complex than the array data structures. Array need a contiguous memory space to store data, the memory requirements are relatively high, but the list is not required, it is through "hands" will not contiguous block of memory series. If you want to apply for a 100MB size arrays and linked lists, when the memory is not continuous, or not enough space for the size of the array will fail to apply, but the list will not.

the difference

There are many chain structure, common are: single linked list , doubly linked list , circular list .

Second, singly linked list

List pointer by discontinuous memory blocks used together in series, in which we have a block of memory called a "node", and in order to string together all the nodes, the nodes in the linked list is stored in addition to data, also with the address of the next node pointer records linked list of nodes, we recorded a pointer to this node address called "successor pointer next", the entire single list as shown below.

List

Where the first node and the last node is special, they are usually referred to as "head node" and "tail node." Base address of the first node to the list of records, obtained with it can traverse the entire list. End node does not point to any node, but point to the empty address null, represents the last node on the list.

Unlike arrays, linked lists of insertion and deletion operations do not require moving large amounts of data, it only needs to consider the neighboring node pointer change, corresponding to the time complexity is O (1). Similarly, access to the list of elements does not operate directly as an array corresponding to the memory address directly by using the first address and addressing formula subscripts, a list need a pointer from node traversed until it finds the corresponding node. Thus the list of random access time complexity is O (n).

Operating difference


Third, the circular list

Circular list is a special single list, which differ in the single linked list points to the null end node address null, and the tail circular linked list node points to the first node of the list, ending like a ring connected, so called cycle chain .

与单链表相比,当要处理的数据具有环型结构特点的问题时(如 约瑟夫问题 ),采用循环链表会简单地多,因为循环链表的优点是从链尾到链头比较方便。

Circular list


四、双向链表

双向链表 在实际的软件开发中更加常用,与单向链表相对应的,双向链表的结点除了有一个后继指针 next 指向后面的结点,还有一个前驱指针 pre 指向前一个结点,因此双向链表需要两个额外的空间来存储前驱结点和后继结点的地址,会占用更多的空间, 大专栏  数据结构与算法:链表但是它的双向遍历也给链表操作增加了更多的灵活性。

Doubly linked list

尽管双向链表会耗费更多的内存,但在实际的开发中,双向链表还是比单链表应用更加广泛,因为在某些情况下,双向链表的操作会比单链表更加高效。比如在实际开发中,删除一个数据无非两种情况:

  • 删除结点中「值等于某个给定值」的结点
  • 删除给定指针指向的结点

对于第一种情况,不管是单向链表还是双向链表,为了找到给定的值的结点都得从头开始遍历,它们的时间复杂度为 O(n) 。而对于第二种情况就不同,第二种情况已经找到了要删除的结点,假设为结点 q ,但是要删除 q 就必须要知道它的前驱结点 p ,所以还是得从头结点重新遍历直到 p->next = q ,时间复杂度为 O(n) 。而由于双向链表具有前驱指针,便不需要像单像链表那样遍历,只用 O(1) 时间复杂度就可以完成删除操作。这种情况对于插入操作也是同理

除此之外,对于一个有序链表,双向链表的按值查询效率也比单项链表要高。因为可以记录上次查找的记录 p ,每次查询时,根据查找的值与 p 的大小关系,可以选择向前遍历或向后遍历,不用重头开始遍历,平均下来质询要查找一半的数据。

This uses a design space for time. That is, when sufficient memory space when speed of execution in the pursuit of code selection can be of high complexity, relatively low time complexity algorithms or data structures . Conversely, if memory is in short supply, it must, in turn, change the design ideas of space with time.


Fifth, implement LRU caching algorithm based on list

The limited size of the cache, when the cache is full use, it should clean up the data, which is the cleanup, which the reservation is required to decide by the cache out of policy, common cache elimination strategies: FIFO strategy FIFO (First In First Out) , the least used tactics LFU (Lest Frequently Used) , least recently used strategy the LRU (Lest recently Logs in Used) .

Based on the list to achieve LRU caching algorithm:

Assuming a singly linked list ordered, linked list of nodes closer to the tail is accessed earlier, when a new data is accessed, the meter starts the traversal list.

  1. If this data has been previously linked list, we will traverse the resulting nodes removed and then inserted into the head of the list.
  2. If the data is not in the list, this time can be divided into two cases
    • Buffer is not full, then the data can be directly inserted into the junction of the head of the list
    • Cache is full, then the node will remove the tail of the list, then the new data is inserted into the list head node


Six palindrome string problem

If a string is a single chain store, how to determine whether it is a palindrome string?

answer:

Speed ​​using two pointers, each pointer quickly forward two nodes, each time the slow advance a node pointer, the pointer in order to find the list midpoint slow, and slow in the node pointer modify the process proceeds the next pointer, the front half list part in reverse order, the final list on both sides of the mid-point equality comparison.

Seven, write the correct list of code tips

(No)

Guess you like

Origin www.cnblogs.com/liuzhongrong/p/12365344.html