Linked List of data structures and algorithms

Source: Internet
Author: User

Linked List of data structures and algorithms

Classification of linked lists:

(1) single-chain table

Header insertion: you only need to maintain one header node, which is often used to simulate the stack;

Tail insertion: You need to maintain the head and end nodes, which are often used to simulate queues.

(2) Two-way linked list

Bidirectional traversal can be used to save Web page history;

(3) Circular linked list

It often appears in the interview questions to determine whether the linked list has loops.

Delete linked list

Method 1: maintain two pointers, current (indicating the current node) and previous (representing the previous node of the current node ). When current traverses the elements to be deleted, execute previous-> next = current-> next and delete current. When deleting a node, you must determine whether current is equal to the head node.

Method 2: maintain a second-level pointer, Node ** current, and a temporary variable entry. You only need to execute * current = entry-> next. to delete the entry, you do not need to determine whether it is a head node.

Memory Pool-based linked list

Disadvantages of the traditional linked list: the linked list will call the system function to allocate memory during the insertion process, and then link the memory to the linked list. The insert operation has three disadvantages:

(1) Frequent system calls may waste a lot of time;

(2) A lot of memory fragments are generated when the linked list node is allocated and released, which is not conducive to allocating the entire memory;

(3) Frequent cache defects may occur;

Solution:

Creates a memory pool. Each insert operation retrieves a node from the memory pool. Each deletion operation puts the node back into the memory pool. The advantage is that there are no memory fragments and no system calls are performed.

Reverse of linked list (regular interview)

(1) Train of Thought 1: reverse during previous and later traversal. Maintain three pointers pointing to the current node and the front and back nodes of the current node respectively.

(2) Train of Thought 2: insert the third node to the nth node and insert it to the first node (head node) one by one, finally, move the first node to the end of the new table.

(3) Train of Thought 3: recursively process the nodes behind the head, and then modify the point of the nodes behind the head and the head.

Print the linked list in reverse order

(1) recursion: inverted printing means that the last element is printed first and then forward. This process can be implemented using recursion. Recursively print all nodes behind the head, and then print the head nodes.

(2) stack simulation: recursive use of the system stack (activity records), we can use the stack in STL to simulate the above recursive process. First, traverse the linked list sequentially, put the elements into the stack, and then print the elements in the stack.

Determine whether the linked list has a ring

(1) how to determine whether a ring exists?

Solution: Set a pair of speed pointers and traverse from the head of the linked list. The slow pointer moves one step forward and the fast pointer moves two steps forward at a time. If there is a ring, the two pointers will encounter each other.

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

Solution: record the collision point of problem (1), and the speed pointer traverses the ring from this position. The distance between the slow pointer and the next collision is the length of the ring.

(3) how to find out the connection point of the ring?

Solution: the distance from the collision point to the connection point = the distance from the header pointer to the connection point. Traverse from two points again.

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

Solution: Question 2 + question 3.

Basic concepts of indirect addressing

A simple description of indirect addressing is the application of second-level pointers. The second-level pointer has three meanings: pointer to pointer, one-dimensional array, and two-dimensional array. Indirect addressing refers to the meaning of its one-dimensional array.

Indirect addressing is a combination of arrays and linked lists. It not only retains many advantages of arrays, but also obtains important characteristics of linked lists. First, you can access each element based on the index time in O (1. Secondly, an ordered table can be searched in the logarithm time using binary. Finally, you do not have to actually move the elements during operations such as insert and delete operations. Indirect addressing uses pointer arrays to track every element. There is no restriction on how to allocate elements themselves (discrete and continuous ).

Application of indirect addressing

(1) Memory Pool

A self-built block memory pool. Each Pointer Points to the first address of each block of memory. The memory pool can avoid Memory fragments and system calls.

(2) hash linked list

If the pointer points to an element that contains the next pointer, the indirect addressing becomes a hashed linked list.

Basic concepts of simulated pointers

A simple description of a simulated pointer is to use the subscript of an array as a pointer. The maximum purpose of simulating pointers is to solve the problem of querying sets.

(1) Implementation 1: first, construct a node array. The node contains two fields: data and link. The link field points to other nodes in the array. Similar to indirect addressing.

(2) Implementation 2: arrays only contain link fields and can be used to simulate trees.

Definition of equivalence classes

Definition: assume that there is a set U with n elements and a set r with R relations. If (a, B) belongs to R, Elements a and B are equivalent. An equivalence class is the largest set of equivalent elements. In other words, the set U is divided according to the relationship. The elements in the class are equivalent and can be seen as a clustering.

Offline equivalence class: We know n and R and determine all equivalence classes.

Online equivalence class: Initially, there are n elements, each of which belongs to an independent equivalence class. Then, the Find and Union operations are continuously executed to add new relationships to R. It is usually called the parallel query set problem.

Basic concepts of querying Sets

Query the set:

(1) Find: queries whether elements a and B belong to the same class;

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

Implementation of query set:

Use the analog pointer in method 2. The subscript of the array indicates the pointer. The Pointer Points to and queries the set to form a virtual forest. However, the query set only focuses on the final root node and the number or height of the elements of the tree, instead of the shape and pointing relationship of each tree.

Optimization of query set:

You can optimize the operations of the query set based on the weight or height rules.

Related Article

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.