The concept and structure of the stack (C language implements the stack)

Table of contents:

1. The concept of stack

Second, the implementation of the stack

 3. Code implementation and detailed explanation

        1. Initial introduction

2. Define the structure and the data type in the stack

3. Initialize the stack void STInit(ST* pst);

4. Destroy the stack void STDestroy(ST* pst);

5. Add data void STPush(ST* pst, STDataType x);

6. Delete data void STPop(ST* pst);

7. Pop data STDataType STTop(ST* pst);

8. Determine whether it is empty bool STEmpty(ST* pst);

9. Judging size int STSize(ST* pst);


Preface and shaping code:

        After learning data tables and linked lists, let's learn a new data structure: stack. It’s still the old routine. Let’s show you the molding code first, so that you can have a preliminary understanding. More importantly, it can facilitate the work of CV engineers

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>

typedef int STDataType;
typedef struct Stack
{
	STDataType* a;
	int top;
	int capacity;
}ST;

// 初始化栈
void STInit(ST* pst);

// 销毁栈
void STDestroy(ST* pst);

// 添加数据
void STPush(ST* pst, STDataType x);

// 删除数据
void STPop(ST* pst);

// 弹出数据
STDataType STTop(ST* pst);

// 判断是否为空
bool STEmpty(ST* pst);

// 判断大小
int STSize(ST* pst);
void STInit(ST* pst)
{
	assert(pst);
	pst->a = NULL;
	pst->top = 0;
	pst->capacity = 0;
}

// 销毁栈
void STDestroy(ST* pst)
{
	assert(pst);
	free(pst->a);
	pst->a = NULL;
	pst->top = 0;
	pst->capacity = 0;
}

// 添加数据
void STPush(ST* pst, STDataType x)
{
	if (pst->capacity == pst->top)
	{
		int newcapacity = (pst->capacity == 0 ? 4 : pst->capacity * 2);
		STDataType* tmp = (STDataType*)realloc(pst->a, newcapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		pst->a = tmp;
		pst->capacity = newcapacity;
	}
	pst->a[pst->top] = x;
	pst->top++;
}

// 删除数据
void STPop(ST* pst)
{
	assert(pst);
	assert(!(STEmpty(pst)));
	pst->top--;
}

// 弹出数据
STDataType STTop(ST* pst)
{
	assert(pst);
	assert(!(STEmpty(pst)));
	return pst->a[pst->top - 1];
}

// 判断是否为空
bool STEmpty(ST* pst)
{
	assert(pst);
	return pst->top == 0;
}

// 判断大小
int STSize(ST* pst)
{
	assert(pst);
	return pst->top;
}

1. The concept of stack

        Stack: A special linear list that only allows insertion and deletion of elements at a fixed end. The end where data insertion and deletion operations are performed 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 LIFO (Last In First Out).

        Push stack : The insertion operation of the stack is called push/push/push, and the incoming data is at the top of the stack .
        Popping : The deletion operation of the stack is called popping. The output data is also on the top of the stack .

Second, the implementation of the stack

        The implementation of the stack can generally be implemented using an array or a linked list . Relatively speaking, the structure of the array is better . Because the cost of inserting data at the end of the array is relatively small.

 

 3. Code implementation and detailed explanation

        1. Initial introduction

  1. Define the structure and the data type in the stack
  2. Initialize stack void STInit(ST* pst);
  3. Destroy the stack void STDestroy(ST* pst);

  4. Add data void STPush(ST* pst, STDataType x);

  5. delete data void STPop(ST* pst);

  6. Pop data STDataType STTop(ST* pst);

  7. Determine whether it is empty bool STEmpty(ST* pst);

  8. Judgment size int STSize(ST* pst);


2. Define the structure and the data type in the stack

code:

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

typedef int STDataType;  //定义栈内数据类型,方便后面修改
typedef struct Stack    //创建栈的基本结构
{
	STDataType* a;
	int top;
	int capacity;
}ST;    

        These lines of code are the foundation of the stack and the simplest code in this structure.


3. Initialize the stack void STInit(ST* pst);

code:

// 初始化栈
void STInit(ST* pst)
{
	assert(pst);   //判断是否为空指针
	pst->a = NULL; //栈为空的,所以先初始化为空指针
	pst->top = 0;  
	pst->capacity = 0;
}

        Initializing the stack, as the name implies, is to add some initial values ​​to the stack structure. Let the front foundation have the frame.

4. Destroy the stack void STDestroy(ST* pst);

code: 

// 销毁栈
void STDestroy(ST* pst)
{
	assert(pst);
	free(pst->a);  //释放指针
	pst->a = NULL; //置空指针a
	pst->top = 0;  //把栈顶的位置降低,释放数据
	pst->capacity = 0;//把栈的大小置为0
}

        After creation, it must be destroyed, otherwise it will cause a memory leak.

5. Add data void STPush(ST* pst, STDataType x);

code:

// 添加数据
void STPush(ST* pst, STDataType x)
{
	if (pst->capacity == pst->top)  //判断是否要扩容
	{
		int newcapacity = (pst->capacity == 0 ? 4 : pst->capacity * 2); //扩容步骤,当为空栈时扩基础的大小,不为空则扩成原来的二倍
		STDataType* tmp = (STDataType*)realloc(pst->a, newcapacity * sizeof(STDataType));//使用realloc扩出来一定的空间
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		pst->a = tmp;   //初始化这一块空间
		pst->capacity = newcapacity;
	}
	pst->a[pst->top] = x;  //把x的直插入到栈的顶部
	pst->top++;
}

        Adding data is also a very important step, which directly determines the success of the stack. When adding, you must first judge whether the space behind is enough. If it is enough, add it directly, and if it is not enough, expand it.


6. Delete data void STPop(ST* pst);

// 删除数据
void STPop(ST* pst)
{
	assert(pst);
	assert(!(STEmpty(pst)));  //判断是否为空
	pst->top--;
}

        When deleting data, you need to use the following empty judgment function. If it is empty, it can no longer be deleted.


7. Pop data STDataType STTop(ST* pst);

code:

// 弹出数据
STDataType STTop(ST* pst)
{
	assert(pst);
	assert(!(STEmpty(pst)));
	return pst->a[pst->top - 1];
}

        The pop-up data is: after calling this function, the value at the top of the stack can be returned


8. Determine whether it is empty bool STEmpty(ST* pst);

// 判断是否为空
bool STEmpty(ST* pst)
{
	assert(pst);
	return pst->top == 0;//判空
}

        Empty judgment is to see if there is any data in it, if there is any, it will return false, if there is no data, it will return true


9. Judging size int STSize(ST* pst);

code:

// 判断大小
int STSize(ST* pst)
{
	assert(pst);
	return pst->top;
}

        Just return to the top of the stack

Guess you like

Origin blog.csdn.net/m0_75215937/article/details/130744589#comments_27669496