Data structure and algorithm - what is a stack, detailed stack storage structure

Like the sequential list and the linked list, the stack is also a linear storage structure used to store data with a logical relationship of "one-to-one", as shown in the figure below.

Schematic diagram of stack storage structure

From the above figure, 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 the figure above;
  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. Take the stack in the above figure 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 the figure below The element is element 1.

top and bottom of 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.

The above is only the tip of the iceberg in the stack application field, so I won't give too many examples here. In the study of subsequent chapters, we will use the stack structure extensively. Next, we learn how to implement sequential stacks and chain stacks, and how to push and pop elements in the stack.

2023 new version of data structure and algorithm Java video tutorial (Part 1), data structure and algorithm that senior java programmers must learn
2023 new version of data structure and algorithm Java video tutorial (part 2), data structure and algorithm that java senior programmer must learn

おすすめ

転載: blog.csdn.net/Itmastergo/article/details/131890711