Algorithms & Data Structures - Stack related basic concepts

        From the perspective of data structure, the stack is also a linear table , and its particularity is that the basic operation of the stack is a subset of the linear table. He is a linear table with limited operations, so it can be called a limited data structure. But from the perspective of data types, the stack is an important abstract data type that is quite different from the linear table.

Table of contents

The definition of the stack import

Scenario introduction

stack definition

Basic operation of the stack

In and out of the stack 

case one

 case two

The role of the stack 


The definition of the stack import

Scenario introduction

        The two buttons that everyone is familiar with on the browser web page, forward and backward: 

        Or the undo button in Word:

        Everyone knows that the function of a button like this is to roll back a webpage or operation, like playing backwards step by step. Operations like this are the idea of ​​the stack.

stack definition

        A stack is a linear table limited to insert and delete operations only at the end of the table.

Top of the stack (Top): The end of the linear table that allows insertion and deletion.
Bottom of the stack (Bottom): Fixed, the other end that does not allow insertion and deletion.
Empty stack : An empty list without any elements.

Stack is also called Last In First Out (Last In First Out) linear list, referred to as LIFO structure.

Basic operation of the stack

  • Initialize InitStack(&S);
  • Judgment empty Empty(S); judge whether a stack is empty, if the stack is empty, return true, otherwise return false.
  • Push into the stack Push(&S, x); Push into the stack (stack insertion operation), if the stack S is not full, add x to make it the new top of the stack.
  • Pop(&S, &x); pop out (stack delete operation), if the stack S is not empty, pop the top element of the stack and return with x.
  • Read stack top element GetTop(S);
  • Traverse stack PrintStack(&S);
  • Destroy the stack DestroyStack(&S); The stack is destroyed and the storage space occupied by S is released ("&" means a reference call).

In and out of the stack 

        If the first-in-last-out of the stack is simply said in this way, it may cause everyone to fall into some kind of misunderstanding. Let's see:

 We have a stack with three elements a, b, and c. If a advances to the stack, must a be the last one to pop out of the stack?

case one

a advances to the stack, b then pushes to the stack, and c finally pushes to the stack.

 c is popped first, b is popped later, and a is popped last

 case two

a advanced stack

 Then a is popped from the stack

 b into the stack, c into the stack

。。。 

The role of the stack 

        The introduction of the stack simplifies the problem of programming and divides it into different attention levels, which narrows the scope of thinking and focuses on the core of the problem we want to solve. All data can only be stored or retrieved at the top of the stack at the floating end, strictly following the principle of "first in, last out". The elements in the middle must be pushed in at the top of the stack and moved out one by one . can only be removed afterwards.

Welcome to like, collect, and comment in the comment area, and reprint to indicate the source.

-----------------------------

Above link:

Algorithm & Data Structure - Static Linked List, Circular Linked List, Doubly Linked List of Linear List Table, there are more codes in this article. https://blog.csdn.net/qq_52213943/article/details/125879485

Links below:

Stay tuned: the order of the stack, the chain structure

Guess you like

Origin blog.csdn.net/qq_52213943/article/details/126354699