Home  >  Article  >  What is the initial state of the stack

What is the initial state of the stack

尚
Original
2019-10-23 10:35:497771browse

What is the initial state of the stack

The initial state of the stack is simply: The initial state of the stack itself when we have not yet pushed or popped elements into the stack (That is to say: when elements are not being moved in and out of the stack, the top pointer and bottom pointer in the stack point to a state such as this)

Stack (stack), also known as stack, is A linear table with limited operations. A linear table that restricts insertion and deletion operations only to the end of the table. This end is called the top of the stack, and the other end is called the bottom.

Inserting a new element into a stack is also called pushing, pushing or pushing. It is to put the new element on top of the top element of the stack to make it the new top element of the stack; delete it from a stack Element is also called popping or popping off the stack. It deletes the top element of the stack and makes its adjacent elements become the new top elements of the stack.

1. PUSH algorithm

①If TOP≥n, overflow information will be given and error handling will be performed (before pushing into the stack, first check whether the stack is full, if it is full, it will overflow; if not, do ②);

②Set TOP=TOP 1 (the stack pointer increases by 1, pointing to the push address);

③S(TOP)=X, end (X is the newly pushed element);

2. Pop off the stack (POP) algorithm

① If TOP ≤ 0, underflow information will be given and error handling will be performed (check whether the stack is empty before popping off the stack, if it is empty, it will underflow; if it is not empty, do ② );

②X=S(TOP), (the element after popping off the stack is assigned to X):

③TOP=TOP-1, end (the stack pointer decreases by 1 and points to the top of the stack).

The above is the detailed content of What is the initial state of the stack. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn