What is a data structure?
A data structure is a collection of data elements with specific relationships. The relationship between elements is called the logical structure of data, and the storage of data elements and the relationship between elements is called storage structure or physical structure. In general, carefully selected data structures can bring higher operation or storage efficiency.
Classification of data structures
The logical structure of data structure is mainly divided into linear structure and nonlinear structure.
The storage structure is mainly divided into sequential storage, chain storage, index storage and hash storage
Sequential storage: a group of storage units with continuous addresses are used for sequential storage (e.g. array).
Chained storage: the nodes linked by pointers are used for storage. The addresses of nodes do not need to be continuous (e.g. linked list).
Index storage: establish an index table, and determine the node storage address through the index number of the index table.
Hash storage: hash storage, also known as hash storage, is a search technology that tries to establish a corresponding relationship between the storage location of data elements and key codes. The basic idea of hash storage is that the storage address of a node is determined by the key value of the node. In addition to searching, it can also be used to store.
Linear table
Definition of linear table
Linear structure is a basic data structure, which is mainly used to describe the data relationship with a single precursor and successor in the objective world. The characteristic of linear structure is that there is a linear relationship between data elements, that is, the elements are “arranged one by one”.
Linear table is the simplest, most basic and most commonly used linear structure. Sequential storage and chain storage are usually used. The main basic operations are insertion, deletion and search.
A linear table is a finite sequence of n (n > = 0) data elements with the same characteristics. The characteristics of non empty linear table are as follows.
(1) There is only one element called “first”.
(2) There is only one element called “last”.
(3) Except for the first element, each element in the sequence has only one direct precursor.
(4) Except for the last element, each element in the sequence has only one direct successor.
Storage structure of linear table
The storage structure of linear table is divided into sequential storage and chain storage.
Sequential storage of linear table refers to the sequential storage of data elements in linear table with a group of storage units with continuous addresses, so that two logically adjacent elements are also adjacent in physical location, as shown in the figure:
The advantage of using sequential storage structure in linear table is that the elements in the table can be accessed randomly, but the disadvantage is that the elements need to be moved for insertion and deletion operations.
The chained storage of linear table uses nodes to store data elements. The basic node structure is as follows:
Among them, the data field is used to store the value of the data element, and the pointer field stores the direct precursor and direct successor information of the current element. The information in the pointer field is called a pointer (or chain).
The address of the node storing each data element is not required to be continuous, so the logical relationship between the elements must be stored while storing the data elements.
In addition, node space is applied only when needed, without prior allocation.
A linked list is formed between nodes through pointer fields. If there is only one pointer field in the node, it is called linear linked list (or single linked list), as shown in the figure:
In the storage structure of the linked list, you only need a pointer (called the head pointer, such as the head in the above figure) to point to the first node to access any element in the table in sequence.
Insertion and deletion in the chain storage structure are essentially the modification of related pointers.
When a linear list uses a linked list as the storage structure, it cannot randomly access the data elements (it needs to traverse the data elements), but the insertion and deletion operations do not need to move the elements.
There are several other linked list structures according to the setting mode of the pointer field in the node.
Two way linked list. Each node contains two pointers, indicating the direct predecessor and direct successor of the current node element respectively. Its characteristic is that it can traverse the linked list from any element node in the list from two directions.
Circular linked list. On the basis of single Necklace list (or two-way linked list), make the pointer of the end node point to the first node in the list to form a circular linked list. Its characteristic is that it can traverse the whole linked list from any node in the list.
Static linked list. The chain storage structure of linear table is described with the help of array. The subscript table of the array element is used to represent the pointer of the node where the element is located.
Stack
Definition of stack
Stack is a linear data structure that can only realize data storage and retrieval by accessing one end of it. In other words, the stack operates according to the “last in, first out” rule. Therefore, stack is also called last in first out (LIFO) linear table.
One end of the stack for inserting and deleting operations is called the top of the stack, and correspondingly, the other end is called the bottom of the stack. A stack without data elements is called an empty stack.
Basic operation of stack
① Initstack (s): create an empty stack.
② Stack empty (s): returns the “true” value when stack s is empty, otherwise returns the “false” value.
③ Push (s, x): add element X to the top of the stack and update the top pointer.
④ Pop (s): delete the stack top element from the stack and update the stack top pointer. If you need to get the value of the top stack element, you can define pop (s) as a function that returns the value of the top stack element.
⑤ Read stack top element (s): returns the value of the stack top element without modifying the stack top pointer.
Storage structure of stack
Sequential storage (sequential stack): the sequential storage of the stack refers to the sequential storage of data elements from the top of the stack to the bottom of the stack with a group of storage units with continuous addresses, and the pointer top is attached to indicate the position of the elements at the top of the stack. In this storage mode, the storage space of the stack needs to be defined (or applied) in advance, and the capacity of the stack space is limited. Therefore, in the sequential stack, when an element is loaded into the stack, it is necessary to judge whether the stack is full (there is no free unit in the stack space). If the stack is full, the element will overflow into the stack.
Chain storage (chain stack)): the chained storage of stack refers to the use of linked list to store the data elements in the stack, which solves the problem of possible overflow. Because the insertion and deletion of elements in the stack are only carried out at the top end of the stack, there is no need to set the head node. The head pointer of the linked list is the top pointer of the stack.
Application of stack
Definition of linear table
Storage structure of linear table
Stack
Definition of stack
Stack is a linear data structure that can only realize data storage and retrieval by accessing one end of it. In other words, the stack operates according to the “last in, first out” rule. Therefore, stack is also called last in first out (LIFO) linear table.
One end of the stack for inserting and deleting operations is called the top of the stack, and correspondingly, the other end is called the bottom of the stack. A stack without data elements is called an empty stack.
Basic operation of stack
① Initstack (s): create an empty stack.
② Stack empty (s): returns the “true” value when stack s is empty, otherwise returns the “false” value.
③ Push (s, x): add element X to the top of the stack and update the top pointer.
④ Pop (s): delete the stack top element from the stack and update the stack top pointer. If you need to get the value of the top stack element, you can define pop (s) as a function that returns the value of the top stack element.
⑤ Read stack top element (s): returns the value of the stack top element without modifying the stack top pointer.
Storage structure of stack
Sequential storage (sequential stack): the sequential storage of the stack refers to the sequential storage of data elements from the top of the stack to the bottom of the stack with a group of storage units with continuous addresses, and the pointer top is attached to indicate the position of the elements at the top of the stack. In this storage mode, the storage space of the stack needs to be defined (or applied) in advance, and the capacity of the stack space is limited. Therefore, in the sequential stack, when an element is loaded into the stack, it is necessary to judge whether the stack is full (there is no free unit in the stack space). If the stack is full, the element will overflow into the stack.
Chain storage (chain stack)): the chained storage of stack refers to the use of linked list to store the data elements in the stack, which solves the problem of possible overflow. Because the insertion and deletion of elements in the stack are only carried out at the top end of the stack, there is no need to set the head node. The head pointer of the linked list is the top pointer of the stack.
Application of stack
The typical applications of stack include expression evaluation and bracket matching. Stack plays an important role in the implementation of computer language and the transformation of recursive process into non recursive process