1. The concept of stack
A stack is a commonly used data structure that follows the principle of Last-In-First- Out (LIFO). The operation of the stack is only performed at one end of the stack, which is called the top of the stack , and the other end is called the bottom of the stack . The basic operations of the stack include push (push) and pop (pop), which are used to add elements to the top of the stack and delete elements from the top of the stack, respectively.
Second, the implementation of the stack
We can implement a stack using an array in C language. Here is a basic stack definition:
#define MAX_SIZE 100
typedef struct {
int data[MAX_SIZE];
int top;
} Stack;
Here, we use an array data
to store the elements of the stack, and an integer top
to indicate the position of the top of the stack. When the stack is empty, top
the value of -1.
1. Initialize the stack
Before using the stack, we need to initialize it first, which can be top
achieved by setting it to -1:
//初始化栈
void init(Stack *stack) {
stack->top = -1;
printf("初始化栈完成......\n");
}
2. Determine whether the stack is empty
We can top
determine whether the stack is empty by judging whether the value is -1:
int isEmpty(Stack *stack) {
return stack->top == -1;
}
3. Determine whether the stack is full
To prevent stack overflow, we need to push
check if the stack is full before performing an operation. When top
the value of is equal to MAX_SIZE-1
, the stack is full:
int isFull(Stack *stack) {
return stack->top == MAX_SIZE - 1;
}
4. Execute push operation
The stack push operation is used to add elements to the top of the stack. First, it needs to determine whether the stack is full. If the stack is not full, add the element to top
the pointed position and top
move it up by one position:
//入栈操作
void push(Stack *stack, int element) {
if (!isFull(stack)) {
stack->data[++stack->top] = element;
printf("完成一次入栈,入栈元素:%d\n",element);
} else {
printf("栈满,不能入栈。\n");
}
}
5. Perform pop-up operation
The stack pop operation is used to delete the element at the top of the stack and return the value of the element. First, it is necessary to determine whether the stack is empty. If the stack is not empty, the top
element at the returned position will top
be moved down by one position:
//出栈操作
int pop(Stack *stack) {
if (!isEmpty(stack)) {
printf("完成一次出栈,");
return stack->data[stack->top--];
} else {
printf("栈空,不能出栈\n");
return -1;
}
}
3. Example code
The following is a simple sample code using the stack, showing how to push and pop the stack:
#include <stdio.h>
//栈容
#define MAX_SIZE 100
typedef struct {
int data[MAX_SIZE];
int top;
} Stack;
//初始化栈
void init(Stack *stack) {
stack->top = -1;
printf("初始化栈完成......\n");
}
//判断栈是否为空
int isEmpty(Stack *stack) {
return stack->top == -1;
}
//判断是否栈满
int isFull(Stack *stack) {
return stack->top == MAX_SIZE - 1;
}
//入栈操作
void push(Stack *stack, int element) {
if (!isFull(stack)) {
stack->data[++stack->top] = element;
printf("完成一次入栈,入栈元素:%d\n",element);
} else {
printf("栈满,不能入栈。\n");
}
}
//出栈操作
int pop(Stack *stack) {
if (!isEmpty(stack)) {
printf("完成一次出栈,");
return stack->data[stack->top--];
} else {
printf("栈空,不能出栈\n");
return -1;
}
}
//遍历栈内元素
void printStack(Stack *stack){
printf("栈内元素为:");
int index=stack->top;
while(index>=0){
printf("%d\t",stack->data[index--]);
}
printf("\n");
}
int main() {
Stack stack;
init(&stack);
push(&stack, 1);
printStack(&stack);
push(&stack, 2);
printStack(&stack);
push(&stack, 3);
printStack(&stack);
printf("出栈元素: %d\n", pop(&stack));
printf("出栈元素: %d\n", pop(&stack));
printf("出栈元素: %d\n", pop(&stack));
return 0;
}
The above code will output the following result:
Popped element: 3
Popped element: 2
Popped element: 1
This is a detailed tutorial on implementing the basic operations of the stack in C language. Hope this helps you!