The concept of stack and its structure
Stack: A special kind of linear list that only allows insertion and deletion of elements at a fixed end. One end that performs data insertion and deletion operations is called the top of the stack, and the other end is called the bottom of the stack.
Elements follow the LIFO principle.
Pushing the stack: The insertion of the stack is called push/push/push, and the pushed data is at the top of the stack.
Pop: The deletion operation of the stack is called pop. The output data is on the top of the stack.
Implementation of stack
The stack can generally be implemented with an array or a linked list, and the array structure is relatively better.
The implementation of the stack is generally implemented using dynamic memory management.
准备环节:
//以顺序表的形式
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
int top;//栈顶
int capacity;//容量
}Stack;
1. Initialization
Basic idea: 1) Assertions
2) The pointer is empty
3) The top of the stack, the capacity is set to 0
Code:
//初始化
void StackInit(Stack* ps)
{
ps->a = NULL;
ps->capacity = ps->top = 0;
}
2. Push the stack
Basic idea: 1) Assertions
2) Determine whether the sequence table needs to be expanded
3) Insert data
Drawing:
Tail insertion similar to sequence table
Code:
//入栈
void StackPush(Stack* ps, STDataType x)
{
//断言
assert(ps);
//判断是否需要扩容
if (ps->top == ps->capacity)
{
int newcapacity = ps->capacity == 0 ? ps->capacity = 4 : ps->capacity * 2;
STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newcapacity);
//判断扩容是否成功
if (tmp == NULL)
{
printf("realloc fail\n");
exit(-1);
}
ps->a = tmp;
ps->capacity = newcapacity;
}
//插入数据
//数组的下标从0开始,top指向的就是栈顶
ps->a[ps->top] = x;
ps->top++;
}
3. Pop the stack
Basic idea: similar to sequential list tail deletion
1) Affirmation
2) Put top--, just step back one place
Drawing:
Code:
//出栈
void StackPop(Stack* ps)
{
assert(ps);
//断言栈是否为空
assert(ps->top > 0);
ps->top--;
}
4. Get the top element of the stack
Basic idea: 1) Assertions
2) Assert whether the stack is empty
3) Return ps->a[ps->top-1]
Drawing:
It can be seen that top points to the next bit of the top element of the stack.
Code:
//获取栈顶元素
STDataType StackTop(Stack* ps)
{
assert(ps);
//断言栈是否为空
assert(ps->top);
return ps->a[ps->top-1];
}
5. Check if the stack is empty
Returns true if empty Returns false if not empty
Basic idea: 1) Assert the passed pointer
2) If judgment sentence or return the result of ps->top == 0
Code:
//检测栈是否为空
bool StackEmpty(Stack* ps)
{
assert(ps);
非空
//if (ps->top > 0)
//{
// return false;
//}
为空
//else
// return true;
//更优化的写法
return ps->top == 0;
}
6. Record the number of data in the stack
Basic idea: 1) Assertions
2) The value of top refers to the number of data.
Drawing:
At this time, the number of stack data is 3, and the value of top is 3.
Code:
int StackSize(Stack* ps)
{
assert(ps);
//返回个数,top指的是栈顶数据的下一位。
return ps->top;
}
7. Destroy the stack
Basic idea: 1) Assertions
2) free pointer variable
3) The capacity and the top of the stack are set to 0
Code:
//销毁
void StackDestroy(Stack* ps)
{
free(ps->a);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
Print stack data against known interfaces
#include"Stack.h"
void test()
{
Stack st;
StackInit(&st);
StackPush(&st, 1);
StackPush(&st, 2);
StackPush(&st, 3);
StackPush(&st, 4);
//printf("%d ", StackTop(&st));
while (!StackEmpty(&st))
{
printf("%d ", StackTop(&st));
StackPop(&st);
}
printf("\n");
StackDestroy(&st);
}
int main()
{
test();
return 0;
}
head File:
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>
//以顺序表的形式
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
int top;//栈顶
int capacity;//容量
}Stack;
//初始化
void StackInit(Stack* ps);
//销毁
void StackDestroy(Stack* ps);
//入栈
void StackPush(Stack* ps,STDataType x);
//出栈
void StackPop(Stack* ps);
//获取栈顶元素
STDataType StackTop(Stack* ps);
//检测栈是否为空
bool StackEmpty(Stack* ps);
//获取栈有多少个数据
int StackSize(Stack* ps);