Data Structure 1 Linear Structure

 

Data structure refers to the combination of data elements and the relationship between elements and construction methods. The relationship between elements is the logical structure of data, and the storage form of element relationship becomes the storage structure. The data structure is divided into two categories: linear structure and non-linear structure according to the difference of logical relationship. Among them, the linear structure is the most basic structure, and the elements are arranged in order. The common ones are linear tables, stacks, queues, arrays, and strings.

 

1. Linear table

1. The linear table is the simplest and most commonly used linear structure. A linear representation of a finite sequence of n (n>=0) elements, a non-empty linear list is characterized by:

There is only one "first" element;

There is only one "last" element;

Except for the first element, each element in the sequence has exactly one immediate predecessor;

Except for the last element, each element in the sequence has exactly one immediate successor;

 

2. Regarding the storage structure of the linear table, there are two ways of sequential storage and chain storage. Sequential storage is to use a group of storage units with consecutive addresses to sequentially store data elements in a linear table, so that two logically adjacent elements are also adjacent in physical position. Chained storage uses nodes to store data elements. The nodes are divided into data fields and pointer fields. The data field is used to store the value of the element, and the pointer field is used to store the address of the immediate predecessor or immediate rear of the element. A linked list is formed through the pointer field. If there is only one pointer field in the node, it is called a singly linked list.

The sequential storage structure does not need to occupy additional space to represent the logical relationship between elements, and can access the elements in the list randomly, but the operations of inserting and deleting data are troublesome, and many elements need to be moved in sequence; The node address is not required to be continuous, so the logical relationship between the elements must be stored at the same time. The insertion and deletion of the linked list are more convenient, and only the point of the pointer field needs to be modified.

 

3. According to how the pointer field in the node is set, there are several other linked list structures:

Doubly linked list: Each stage contains two pointers, respectively pointing to the direct predecessor and the direct rear drive, so that the linked list can be traversed from two directions from any node in the list.

Circular linked list, on the basis of singly linked list or doubly linked list, makes the pointer at the end of the list point to the first element, so that the entire linked list can be traversed from any node.

 

2. Stacks and Queues

The logical structure of stacks and queues is the same as that of linear lists, but there are restrictions on their operations.

 

1. Stack

The stack can only store and retrieve data by accessing one end of it, that is, the operation of the stack is carried out according to the principle of first in, last out (Last In first out, LIFO). This end is called the top of the stack, and the other end is bottom of the stack. A stack with no elements is called an empty stack.

The storage structure of the stack is also divided into sequence and chain. Sequential storage is to use a group of storage units with consecutive addresses to sequentially store the data elements from the top of the stack to the bottom of the stack, and use the pointer top to indicate the position of the top element of the stack. The stack also becomes a sequential stack. Using this storage method requires pre-defining the storage space of the stack. The space of the stack is limited, so before elements are pushed into the stack, it is necessary to determine whether the stack is full, otherwise overflow will occur.

The chained storage stack (chain stack) has no overflow problem, and the chain stack does not need to set the top pointer.

Stacks play an important role in the evaluation of expressions, the implementation of computer languages, and the process of turning recursive processes into non-recursive ones.

2. Queue

A queue is a first in first out (FIFO) linear list, but elements can only be inserted at the rear of the queue and then deleted at the front of the queue.

Similar to the sequential stack, the sequential queue adopts the sequential storage structure, and needs to set the queue head pointer and the queue tail pointer.

In the chain queue, for the convenience of operation, a head node will be added, and the head pointer will point to the head node, so if the values ​​of the head pointer and the tail pointer are the same, it can be determined that the queue is empty.

Queues are often used to deal with occasions that require queuing, such as print queues, message queues, and so on.

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325466038&siteId=291194637