Summary and analysis of linear structure of data structure (linked list and stack and queue)

  • The author has already had a considerable amount of time for the study of data structure. Only by reviewing the past and learning the new can we better grasp the learned knowledge. Therefore, we must summarize the linear structure in the data structure and keep in mind the methods and operations.

Part 1: Linked List
Classification: singly linked list, singly circular linked list, doubly linked list, two-way circular linked list.

Analysis of
singly linked lists : singly linked lists are the most widely used in daily life, and they are also an introduction to chain structure.
Each node has two parts: a data field and a pointer field. The pointer field holds the address of the next node in the linked list. Since the linked list is composed of discrete memory spaces, unlike arrays, which are continuous spaces, the pointer field It is a very critical part for finding subsequent nodes.
The implementation of the singly linked list and the menu test are implemented in detail in the author's blog, which will not be detailed here.

Analysis of a
one-way circular linked list: Compared with a one-way non-cyclic linked list, the pointer field of the end node does not point to the NULL pointer, but points to the head node, so no matter which one of the linked list is accessed Nodes can find the predecessor and successor of any node, and the predecessor of a certain node in a one-way acyclic linked list cannot be accessed, which is restricted by the backward directivity of its pointer, so the circular linked list is for some operations It is more convenient, especially when setting a pointer to the end node in some cases, which can simplify the implementation of the algorithm.
Example: Combine two unidirectional circular linked lists. If there are only pointers to the head node of the two linked lists, the time complexity of the algorithm must be linear time O(n), but when two pointers to the end node are set , The time complexity of the algorithm can be reduced to a constant time O(1).
The implementation of the one-way circular linked list and the menu test are implemented in detail in the author's blog, and will not be detailed here.

Analysis of
doubly linked list : For singly linked list, an extra pointer field is added to point to its predecessor node, and the memory usage is larger, but the benefit in exchange is that the time to find the predecessor node of a node is complicated. The degree is a constant time O(1). Compared with a one-way circular linked list, if the latter wants to find the precursor node of a node, it must go through a cycle to find the target node. The time complexity is O(n), It can be seen from it that in some cases the benefits of changing space for time are obvious, and some ideas of dynamic programming are embedded in it.

Analysis of the
doubly circular linked list : The doubly circular linked list changes the NULL pointers in the first node and the end node of the original doubly linked list, so that the predecessor pointer field of the first node points to the end node and the subsequent pointer field of the end node Pointing to the first node of the linked list greatly reduces the limitation of the original two-way non-circular linked list, so that this data structure can be implemented as the underlying structure of the queue, and the operation time complexity of the enqueue and dequeue of elements is a constant time O( 1) If it is implemented with a two-way non-cyclic linked list, the time complexity is linear time, which is very slow. Therefore, the use of a two-way circular linked list is more extensive than that of a non-cyclic linked list.

Part 2: Stack
Classification: array stack, linked list stack.

Analysis of the
array stack : The underlying implementation of the data structure of the array stack is based on the array. The characteristic of the stack data structure is "last in, first out" (LIFO). You can only get the content of the element at the top of the stack each time. Operations include: push, pop, isempty, and get the top element of the stack (top). Compared with the linked list stack, the array stack has a limited number of elements that can be pushed into the stack. Therefore, in practical applications, we do not often use this construction method to solve practical problems.

Analysis of the
linked list stack : The underlying implementation of the data structure of the linked list stack is based on the linked list. The most convenient structure is a one-way non-circular linked list. Using the "head insertion method" to add elements can reduce the time complexity of the stacking and popping operations. O(1), for solving practical problems, we usually use this implementation.

The implementation of the stack and the menu test are implemented in detail in the author's blog and will not be detailed here.

Examples of practical applications
of stacks : For the "Last In First Out" (LIFO) feature of the stack, it is very suitable for solving some complex nonlinear structure problems, such as the depth-first search of graphs and the traversal of the first-middle-post-order of the tree, and Some problems can use this data structure to simplify operations.

Part 3: Queue
Classification: circular queue, chain queue.

Analysis of
circular queue : The underlying implementation of this data structure of circular queue is based on arrays, which is characterized by "first in, first out" (FIFO). The basic operations are: enqueue, dequeue, and get the head element (front), isempty. The special case of "false overflow" occurs when the queue is implemented by an array, so in actual implementation, it is generally solved by using one less element space.

Analysis of
chained queue : The underlying realization of the data structure of chained queue is based on linked lists. If a two-way circular linked list is used, the time complexity of enqueue and dequeue operations can be reduced to O(1), and the queue in practical applications Most of them are implemented by chain to solve the problem of insufficient memory space.

Examples of practical applications
of queues : For the "first in, first out" (FIFO) characteristics of queues, it is also very suitable for solving some complex nonlinear structure problems, such as breadth-first search of graphs and hierarchical traversal of trees. There are also dabbling in practical applications.
The author has a program implementation in the blog, so I will not go into details here.

Author: Winds_X_Clover
Creation time: November 24, 2020

Guess you like

Origin blog.csdn.net/m0_46181359/article/details/110087783