catalogue
Concept and structure of stack
Returns the element at the top of the stack
Count the number of elements in the stack
Determine whether the stack is empty
Concept and structure of stack
Stack: a special linear table that allows insertion and deletion of elements only in a fixed segment. The section for data insertion and deletion is called the top of the stack, and the other end is called the bottom of the stack. The data elements in the stack follow the principle of last in first out LIFO(Last In First Out).
Stack pressing: the stack insertion operation is called stack entering / stack pressing / stack entering, and the input data is at the top of the stack.
Stack out: stack deletion is called stack out. The output data is also at the top of the stack.
Its structure is a bit similar to the cartridge clip of the bullet. When we fill the cartridge clip, the bullet is pressed in from the top. When we shoot, the bullet is shot first and then pressed in.
Implementation of stack
Stack can generally be implemented by array or linked list. Relatively speaking, the structure of array is better. Because the cost of inserting data on the tail of the array is relatively small.
Let's use array to realize array stack.
Function interface declaration of stack
#pragma once #include<stdio.h> #include<assert.h> #include<stdlib.h> #include<stdbool.h> typedef int STDataType; typedef struct Stack { STDataType* a; int top;//Position of stack top int capacity;//Space capacity of stack }ST; //initialization void StackInit(ST* ps); //Destroy void StackDestroy(ST* ps); //Stack pressing void StackPush(ST* ps, STDataType x); //Out of stack void StackPop(ST* ps); //Returns the element at the top of the stack STDataType StackTop(ST* ps); //Count the number of elements in the stack int StackSize(ST* ps); //Determine whether the stack is empty bool StackEmpty(ST* ps);
Stack initialization
At first, the array has not been opened up, so set it to NULL and capacity to 0. The top here can be 0 or - 1. The author selects the method of top=0 to initialize
//initialization void StackInit(ST* ps) { assert(ps); ps->a = NULL; ps->top = 0;//ps->top = -1; ps->capacity = 0; }
Stack destruction
//Destroy void StackDestroy(ST* ps) { assert(ps); free(ps->a); ps->a = NULL; ps->capacity = 0; ps->top = 0; }
Stack pressing
When pressing the stack, we first need to judge whether the stack is full. If it is full, increase the capacity. If there is not enough space, we can directly press the stack.
//Stack pressing void StackPush(ST* ps, STDataType x) { assert(ps); //If there is not enough space, it needs to be expanded if (ps->top == ps->capacity) { int Newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2; STDataType* temp = (STDataType*)realloc(ps->a, sizeof(STDataType)*Newcapacity); if (temp == NULL) { printf("realloc fail\n"); exit(-1); } ps->a = temp; ps->capacity = Newcapacity; } //If there is enough space, press the stack directly ps->a[ps->top] = x; ps->top++; }
Out of stack
When leaving the stack, the stack must not be empty, otherwise an error will occur when entering the stack or printing later. Therefore, we need to add an assertion or call a function to judge the empty stack.
//Out of stack void StackPop(ST* ps) { assert(ps); assert(!StackEmpty(ps)); //assert(ps->top > 0); ps->top--; }
Returns the element at the top of the stack
Similarly, we also need to add assertions or call the function to judge the empty stack to ensure that the stack is not empty.
//Returns the element at the top of the stack STDataType StackTop(ST* ps) { assert(ps); assert(!StackEmpty(ps)); //assert(ps->top > 0); return ps->a[ps->top - 1]; }
Count the number of elements in the stack
Top is exactly the Size we need, so just return top
//Count the number of elements in the stack int StackSize(ST* ps) { assert(ps); return ps->top; }
Determine whether the stack is empty
We can implement this function in the following two ways. The author prefers the second because it has only one line and feels more fragrant.
//Determine whether the stack is empty bool StackEmpty(ST* ps) { assert(ps); if (ps->top == 0) //{ // return true; //} //else //{ // return false; //} return ps->top == 0; }
Stack test:
void TestStack2() { ST st; StackInit(&st); StackPush(&st, 1); StackPush(&st, 2); StackPush(&st, 3); StackPush(&st, 4); StackPush(&st, 5); printf("%d ", StackTop(&st)); StackPop(&st); StackPush(&st, 6); printf("%d ", StackTop(&st)); StackPop(&st); while (!StackEmpty(&st)) { printf("%d ", StackTop(&st)); StackPop(&st); } StackDestroy(&st); } int main() { //TestStack(); TestStack2(); return 0; }