[data structure] stack (implementation + original code)

catalogue

Concept and structure of stack

Implementation of stack

Initialization stack

Destroy

Push

Out of stack

Get stack top element

Gets the number of valid elements in the stack

Determine whether the stack is empty

test

Concept and structure of stack

Stack: a special linear table that allows insertion and deletion of elements only at a fixed end. One end 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 comply with the Last In First Out LIFO (Last In First Out) principle.

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.

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.

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>

typedef int STDataType;

// The following is the structure of fixed length static stack, which is generally not practical in practice, so we mainly implement the following stack that supports dynamic growth
#define N 10
typedef struct Stack
{
    STDataType _a[N];
    int _top; // Stack top
}Stack;


// Stack supporting dynamic growth
typedef struct Stack
{
	STDataType* a;
	int top;//Stack top
	int capacity;//Stack capacity
}Stack;

//initialization
void StackInit(Stack*ps);

//Destroy
void StackDestroy(Stack* ps);

//Push 
void StackPush(Stack* ps, STDataType x);

//Out of stack
void StackPop(Stack* ps);

//Get stack top element
STDataType StackTop(Stack* ps);

//Determine whether the stack is empty
bool StackEmpty(Stack* ps);

//Gets the number of valid elements in the stack
int StackSize(Stack* ps);

Initialization stack

The initialization and destruction of the stack are similar to that of the sequence table, which will not be explained in detail here, but the size (number of effective elements) in the sequence table is replaced by the top of the stack here.

void StackInit(Stack* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;//It can also be initialized to - 1, but it needs to be changed in the implementation of later functions
}

Destroy

void StackDestroy(Stack* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}

Push

Like the sequence table, when adding elements, you must first judge whether the capacity is sufficient. If the capacity is insufficient, you need to expand the capacity.

The stacking here is the same as the tail insertion of the sequence table.

void StackPush(Stack* ps, STDataType x)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(ps->a , sizeof(STDataType) * newcapacity);
		if (tmp == NULL)
		{
			perror("realloc");
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
	ps->a[ps->top] = x;
	ps->top++;
}

Out of stack

When the stack is out, the tail is deleted. To delete elements, you need to judge whether the stack is empty. An empty stack cannot be out of the stack.

Determine whether the stack is empty, which is implemented below.

void StackPop(Stack* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	ps->top--;
}

Get stack top element

The top element of the stack is the last element in the sequence table, which can be found directly according to the subscript.

Of course, the stack cannot be empty.

STDataType StackTop(Stack* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	return ps->a[ps->top - 1];
}

Gets the number of valid elements in the stack

Just return to top.

int StackSize(Stack* ps)
{
	assert(ps);
	return ps->top;
}


Determine whether the stack is empty

You can directly judge whether the stack is empty according to whether top is equal to 0.

bool StackEmpty(Stack* ps)
{
	assert(ps);
	return ps->top == 0;
}

test

The stack test cannot print each element of the stack in turn, but needs to enter the stack first, then find the top element of the stack and exit the stack in turn.

int main()
{
	Stack s;
	StackInit(&s);
	StackPush(&s, 1);
	StackPush(&s, 2);
	StackPush(&s, 3);
	StackPush(&s, 4);
	StackPush(&s, 5);

	while (!StackEmpty(&s))
	{
		printf("%d ", StackTop(&s));
		StackPop(&s);
	}
	StackDestroy(&s);


	return 0;
}

The results are as follows:

Tags: data structure stack

Posted on Sat, 30 Oct 2021 22:06:36 -0400 by twostars