Day4: stack of data structures

Posted by DarrenL on Fri, 24 Dec 2021 05:44:57 +0100

cataloguecatalogue
Sequence tableSingle linked list (without additional header node)
Double linked list (with additional header node)Stack (sequential table implementation)

preface

Hello, last time we learned about double linked list, today we'll learn some properties and precautions of stack.

1. Definition of stack

  1. Stack is a linear table that is first in and last out and can only be inserted and deleted at the end. The end that allows insertion and deletion is called the top of the stack, while the other end that does not allow insertion and deletion is called the bottom.
  2. The stack is generally implemented with a sequence table, because only the insertion and deletion at the top of the stack are considered, and the acquisition of the top of the stack element is also obtained, and other locations do not need to be considered.

2. Basic implementation of stack

Header file

#pragma once / / prevent header files from being included repeatedly
#include<stdio.h>
#include<assert. h> / / assert check header file
#include<stdlib. h> / / dynamic memory function header file
#include<stdbool. h> / / bool header file
// 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
//typedef int STDataType;
//#define N 10
//typedef struct Stack
//{
//	STDataType a[N];
//	int top; //  Stack top
//}Stack;
// Stack supporting dynamic growth
typedef int STDataType;
typedef struct Stack
{
	STDataType* a; // Pointer form, which can be expanded
	int top; // Stack top
	int capacity; // capacity
}Stack;

1. Initialization

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

2. Stack

  1. The stack can only be loaded at the top of the stack. The implementation here uses an array, so it is loaded at the end of the array.

void StackPush(Stack* ps, STDataType data)
{
	assert(ps);
	if (ps->top == ps->capacity) // If the sequence table is full, expand the capacity
	{
		STDataType newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity; // Set new capacity for expansion
		STDataType* ps1 = (STDataType*)realloc(ps->a, newcapacity * sizeof(STDataType)); // Expand the pointer PS - > a
		if (ps1 == NULL) // Check whether the memory allocation is successful
		{
			perror("realloc"); // Error alarm information
			exit(-1); // sign out
		}
		ps->a = ps1; // Successfully applied for memory, assigned to PS - > A
		ps->capacity = newcapacity; // Capacity equals new capacity
	}
	ps->a[ps->top] = data; // Assign a new value to the top of the stack
	ps->top++; // Effective number + 1
}

3. Out of stack

  1. Out of stack is also to remove the elements at the top of the stack. It is out of stack. Here, you can still delete the elements at the end of the array. Therefore, it is relatively simple to implement. Directly check whether the stack is empty and the effective number is - 1.

void StackPop(Stack* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));// Check whether the stack is empty
	--ps->top;// Number of valid - 1
}

4. Get stack top element

  1. Get the stack top element, check whether the stack is empty, and then return the stack top element (tail element).

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

5. Number of valid checkpoints

  1. Just return to top.
int StackSize(Stack* ps)
{
	assert(ps);
	return ps->top;
}

6. Check whether the stack is empty

  1. Check whether the stack is empty. If it is empty, it returns a non-zero result. If it is not empty, it returns 0.
int StackEmpty(Stack* ps)
{
	assert(ps);
	return ps->top == 0;
}

7. Destruction stack

  1. Release PS - > A to prevent memory leakage.
void StackDestroy(Stack* ps)
{
	assert(ps);
	if (ps->a) // Check whether it is empty. Release if it is not empty
	{
		free(ps->a); // Prevent memory leaks
	}
	ps->a = NULL; // Set null to prevent it from becoming a wild pointer
	ps->top = 0; 
	ps->capacity = 0;
}

Interview questions

After learning the stack, you can do a bracket matching question to test whether you have learned it. Link: Valid parentheses.

extend

After learning this, everyone must know that stack is a relatively simple linear table, but it is the most commonly used and important data structure with a wide range of uses.

For example:

  1. Syntax recognition and expression calculation in assembly processor are implemented based on stack.
  2. The stack also often uses parameter passing and function value return in function calls.

After coming down, you can understand the role of stack by yourself. I won't say more here.
Source code link: gitee.

ps: it's not easy to make, remember the third consecutive!!!

Topics: Algorithm data structure linked list stack