What is a stack? C language realizes the algorithm of pop and push operation on the stack
stack
A stack (stack) is a data structure that has the characteristics of Last-In-First-Out (Last-In-First-Out), which means that the last element added to the stack is the first to be taken out. The stack has two basic operations: push (push) a new element to the top of the stack, and pop (pop) the top element of the stack.
Implement pop and push operations on the stack
In C language, a stack data structure is implemented using an array. The following is the algorithm for pop and push operations on the stack:
Push element:
void push(int stack[], int *top, int val, int size) {
if (*top == size - 1) { // 检查栈是否已满
printf("Stack Overflow\n");
return;
}
*top += 1; // 栈指针加一
stack[*top] = val; // 将新元素压入栈顶
}
popup element:
int pop(int stack[], int *top) {
if (*top == -1) { // 检查栈是否已空
printf("Stack Underflow\n");
return -1;
}
int val = stack[*top]; // 获取栈顶元素
*top -= 1; // 栈指针减一
return val; // 返回栈顶元素
}
These algorithms assume a stack with a maximum capacity of size
, and use a pointer top
to keep track of the index of the top element of the stack. Note that these algorithms do not perform any initialization or clearing of the data in the stack, so the stack needs to be initialized before use.
explain
Let's explain these C language codes in detail.
First push
the function:
void push(int stack[], int *top, int val, int size) {
if (*top == size - 1) { // 检查栈是否已满
printf("Stack Overflow\n");
return;
}
*top += 1; // 栈指针加一
stack[*top] = val; // 将新元素压入栈顶
}
- What this function does is push an integer value
val
onto the stack. The parameterstack
is an array of integers, storing the elements of the stack,*top
and a pointer to an integer, used to record the index of the element at the top of the stack.size
is the maximum capacity of the stack. - The first line of code in the function,
if (*top == size - 1)
, checks to see if the stack is full. If the stack is full, we print an error message and return without executing the following code. If the stack is not full, execute the following code to push a new element onto the top of the stack. *top += 1;
This line of code moves the pointer to the top of the stack up one position. Since the index of the C language array starts from 0, the index of the next element is, but we want to push the top of the stack*top + 1
before this index , so we must first add 1, and then push the top of the stack, just Like this line of code does: .val
*top
val
stack[*top] = val;
Here is pop
the function:
int pop(int stack[], int *top) {
if (*top == -1) { // 检查栈是否已空
printf("Stack Underflow\n");
return -1;
}
int val = stack[*top]; // 获取栈顶元素
*top -= 1; // 栈指针减一
return val; // 返回栈顶元素
}
- The function of this function is to pop an integer value from the stack, which is a pop operation. The parameters
stack
are*top
the same as the previous push function, which are an array of integers and a pointer to integers, which are used to record the index of the element at the top of the stack. if (*top == -1)
This line of code is used to check whether the stack is empty. If the stack is empty, we print an error message and return. Otherwise, we pop the top element off the stack and move the pointer down one position. That's whatint val = stack[*top];
the and*top -= 1;
code do.- Finally,
return val;
return the top element of the popped stack.
In short, these two functions jointly implement a stack data structure, which can realize push and pop operations. When using these functions, please initialize the stack array first, and make sure that the maximum capacity of the stack will not be exceeded.