Linked list of data structures and algorithms

Source: Internet
Author: User

List of categories:

(1) Single linked list

Head interpolation method: only need to maintain a head node can be used to simulate the stack;

The tail interpolation method: need to maintain the head node and the tail node, commonly used to simulate the queue.

(2) Doubly linked list

Bidirectional traversal, can be used to save the history of Web pages, etc.;

(3) Circular link list

Often appear in the interview questions, to determine whether the linked list has a ring.


Deletion of linked list

Mode one: maintains two pointers, current (representing the present node) and previous (representing the previous node of the current node). When current iterates over the element to be deleted, it executes previous->next = Current->next, and deletes it. When deleting, you need to determine if current is equal to the head node.

Mode two: Maintain a level two pointer, Node * * Current and a temporary variable entry. Delete only need to execute *current = entry next. and delete the entry, no need to determine whether the head node.


A linked list based on a memory pool

Disadvantages of the traditional list: The linked list calls the system function to allocate memory during the insertion process, and then links the block memory to the linked list. There are three disadvantages to inserting an operation:

(1) Frequent system calls, which will waste a lot of time;

(2) A lot of memory fragments are generated during the distribution of the linked list nodes, which is not conducive to allocating the whole block of memory.

(3) may result in frequent cache deletions;

Solution:

Constructs a pool of memory, each of which gets a node from the memory pool, and each time it is dropped, it is put back into the memory pool. The advantage of this is that there is no memory fragmentation and no system calls.


The reversal of the list (interview often test)

(1) Idea one: reverse in the process of traversing backward from the previous. Maintains three pointers, pointing to the current node and the front and back nodes of the current node, respectively.

(2) Train of thought two: the third node to the nth node, sequentially inserted into the first node (head node), and finally moved the first node to the footer of the new table.

(3) Idea three: recursively handle the node behind the head, and then modify the point of the head and head behind the node.


Reverse Print linked list

(1) Recursion: Reverse printing means the last element, and then print forward, you can use recursion to implement the process. Recursively prints all the nodes behind the head, and then prints the head node.

(2) Stack simulation: Recursive use of the system stack (activity record), we can use the STL stack to simulate the above recursive process. First, the list is traversed sequentially, the elements are placed on the stack, and then the stack prints the elements sequentially.


Determine if a linked list has a ring

(1) How to determine if there is a ring?

Solution: Set a pair of fast and slow pointers, and then walk forward from the head of the list. The slow pointer moves forward one step at a time, and the quick pointer advances two steps at a time, and if there is a ring then two pointers will meet.


(2) How to know the length of the ring?

Solution: Record the problem (1) of the collision point, the fast and slow pointer from the position to traverse the loop again. The distance that the next collision slow pointer passes is the length of the ring.

(3) How to find out where the ring connection points are?

Solution: The distance from the collision point to the connection point = the distance from the head pointer to the connection point, which is traversed again from two points.


(4) What is the length of the linked list?

Solution: Question 3.


Basic concepts of indirect addressing

A simple description of indirect addressing is the application of level two pointers. A secondary pointer has three meanings: a pointer to a pointer, a one-dimensional array, and a two-dimensional array. Indirection in this particular refers to the meaning of one-dimensional array.

Indirect addressing is a combination of arrays and linked lists. It preserves the many advantages of the array, and obtains the important features of the linked list. First, each element can be accessed according to the index at O (1). Second, you can use two points to search for an ordered table in logarithmic time. Finally, the elements do not have to be physically moved during the insert and delete operations. Indirection uses a pointer array to track each element, with no restrictions on how the element itself is assigned (discrete and continuous).


Application of indirect addressing

(1) Memory pool

Self-building blocks of memory pool, each pointing to the first address of each piece of memory. Memory pools can avoid memory fragmentation and system calls.

(2) Hash list

If the element pointed to by the pointer contains a next pointer, the indirection becomes a hash list.


Basic concepts of analog pointers

The simple description of the analog pointer is to use the subscript of the array as the pointer. The maximum use of analog pointers is to solve and check the set problem.

(1) Implementation of a: first constructs a node array, the node contains two domains: Data and link. The link field points to the other nodes in the array. and indirect addressing have similarities.

(2) Implementation two: The array contains only the link domain, which can be used to simulate the tree.


Definition of equivalence class

Definition: Suppose there is a set of n elements u, and another set R with R relationships. if (b) belongs to r, then elements a and B are equivalent. Equivalence classes refer to the largest set of mutually equivalent elements. In other words, the set U is divided according to the relationship, the elements within the class are equivalent and can be regarded as a kind of clustering.

Offline equivalence classes: Known N and R, determine all equivalence classes.

Online equivalence class: There are n elements at the beginning, and each element belongs to a separate equivalence class. The Find and union operations are then kept executing to add new relationships to R. It is often referred to as a set problem.


And the basic concept of the collection

And check the operation of the set:

(1) Find: Whether the query elements a and B belong to the same class;

(2) Union: The class where elements a and B are merged.


and check the realization of the set:

Using the analog pointer of mode two. The index of the array represents the pointer, and the pointer to the set makes up a virtual forest. However, it does not pay attention to the shape of each tree and the relationship of each other, only the final root node and the number of elements or height of the tree.


And the optimization of the check set:

The operation of the set can be optimized according to the weight rule or the height rule.






Linked list of data structures and algorithms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.