Detailed stack storage structure

Table of contents

Detailed stack storage structure

push and pop

The specific implementation of the stack

stack application

What is a queue (queue storage structure)


Detailed stack storage structure

Like the sequential list and the linked list , the stack is also a linear storage structure used to store data whose logical relationship is "one-to-one" , as shown in Figure 1.


 

Schematic diagram of stack storage structure


Figure 1 Schematic diagram of stack storage structure


From Figure 1, we can see that the stack storage structure is different from the linear storage structure learned before, because the stack has special requirements for the process of data "storage" and "retrieval" :

  1. The stack can only access data from one end of the table , and the other end is closed , as shown in Figure 1;
  2. In the stack, whether it is storing data or fetching data, the principle of "first in, last out" must be followed, that is, the most advanced stack element is the last to be popped out of the stack. Taking the stack in Figure 1 as an example, it can be judged from the storage state of the data in the figure that element 1 is the most advanced stack. Therefore, when element 1 needs to be taken out from the stack, according to the principle of "first in, last out", element 3 and element 2 need to be taken out of the stack in advance, and then element 1 can be successfully taken out.


Therefore, we can give a definition to the stack, that is, the stack is a linear storage structure that can only access data from one end of the table and follows the "first in, last out" principle.

Typically, the open end of the stack is called the top of the stack; correspondingly, the closed end is called the bottom of the stack. Therefore, the top element of the stack refers to the element closest to the top of the stack. Taking Figure 2 as an example, the top element of the stack is element 4; similarly, the bottom element of the stack refers to the element at the bottom of the stack. The bottom of the stack in Figure 2 The element is element 1.


 

top and bottom of stack


Figure 2 The top and bottom of the stack

push and pop

Based on the characteristics of the stack structure, in practical applications, only the following two operations are usually performed on the stack:

  • Adding elements to the stack, this process is called "pushing" (pushing or pushing);
  • Extract the specified element from the stack, this process is called "popping the stack" (or popping the stack);

The specific implementation of the stack

The stack is a "special" linear storage structure, so the specific implementation of the stack has the following two methods:

  1. Sequential stack: Using the sequential storage structure can simulate the characteristics of stack storage data, so as to realize the stack storage structure;
  2. Chain stack: use the chain storage structure to realize the stack structure;

The difference between the two implementations is limited to the relative position of the data elements stored in the actual physical space. The bottom layer of the sequence stack is an array, and the bottom layer of the chain stack is a linked list. The specific implementation of sequential stack and chain stack will be explained in detail in subsequent chapters.

stack application

Based on the characteristics of the "first in, last out" principle for data access based on the stack structure, it can be used to implement many functions.

For example, we often use our browsers to find information on various websites . Suppose you browse page A first, then close page A and jump to page B, then close page B and jump to page C. At this time, if we want to return to page A, we have two options:

  • Search again to find page A;
  • Use your browser's "fall back" function. The browser will fall back to page B first, and then fall back to page A.


The implementation of the browser's "fallback" function uses the stack storage structure at the bottom. When you close page A, the browser will push page A to the stack; similarly, when you close page B, the browser will also push B to the stack. Therefore, when you perform a rollback operation, you will first see page B, and then page A. This is the effect of the data in the stack being popped out of the stack one by one. Not only that, the stack storage structure can also help us detect parentheses matching problems

in the code . Brackets (small brackets, square brackets, and curly brackets) are used in most programming languages. The wrong use of brackets (usually missing right brackets) will cause program compilation errors, and many development tools have the function of detecting whether the code has editing errors , which includes the problem of matching brackets in the detection code. The underlying implementation of this function uses a stack structure. At the same time, the stack structure can also realize the decimal conversion function of the value. For example, writing a program to automatically convert from a decimal number to a binary number can be achieved by using a stack storage structure.

What is a queue (queue storage structure)

A queue, like a stack , is also a linear storage structure that has strict requirements on data "storage" and "retrieval" .

Different from the stack structure, both ends of the queue are "open", requiring data to only enter from one end and exit from the other end , as shown in Figure 1:


 

queue storage structure


Figure 1 Queue storage structure

Usually, the end of the data entry is called the "team tail", and the end of the data output is called the "queue head". The process of data elements entering the queue is called "queue entry", and the process of leaving the queue is called "queue exit".

Not only that, the entry and exit of data in the queue must follow the principle of "first in, first out" , that is, the data elements of the most advanced queue must also be out of the queue first. Taking the queue in Figure 1 as an example, from the storage status of data in the queue, it can be seen that element 1 is the most advanced, followed by element 2, and finally element 3. At this time, if element 3 is dequeued, according to the "first in, first out" characteristic of the queue, element 1 must be dequeued first, element 2 will be dequeued, and element 3 will be out of the queue at last.

Don't confuse the stack with the queue. The stack structure is closed at one end, which is characterized by "first-in, last-out"; while both ends of the queue are open, and the characteristic is "first-in, first-out".

Therefore, data enters from one end of the table and exits from the other end, and a linear storage structure that follows the "first in, first out" principle is a queue.

There are two ways to implement the queue storage structure:

  1. Sequence queue: a queue structure implemented on the basis of a sequence table ;
  2. Chain queue: a queue structure implemented on the basis of a linked list ;


The difference between the two is only the difference between the sequential list and the linked list, that is, in the actual physical space,

  1. The queue stored in the dataset is a sequential queue
  2. The queue for decentralized storage is a chain queue


In real life, the application of queues can be seen everywhere, such as queuing up to buy XXX, the registration system of hospitals , etc., all adopt the structure of queues.

Take queuing to buy tickets as an example, all people line up in a line, those who come first will go to the front, and those who come later can only wait in line from the end of the line, and everyone in the line must wait until everyone in front of them has bought all of them. After the ticket is successfully issued and the team is discharged from the head of the team, it is your turn to buy the ticket. Isn't this a typical queue structure?
 

 

Guess you like

Origin blog.csdn.net/qq_38998213/article/details/132308154
Recommended