[Data structure—implementation of stack (array stack)]

Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right.

Article directory


Preface

世上有两种耀眼的光芒,一种是正在升起的太阳,一种是正在努力学习编程的你!一个爱学编程的人。各位看官,我衷心的希望这篇博客能对你们有所帮助,同时也希望各位看官能对我的文章给与点评,希望我们能够携手共同促进进步,在编程的道路上越走越远!


提示:以下是本篇文章正文内容,下面案例可供参考

1. Stack

1.1 Concept and structure of stack

Array:Array is generally suitable for tail insertion and tail deletion, so the top of the stack is generally at the tail.
Doubly linked list implementation: Tail insertion and tail deletion are the same as head insertion and head deletion, and they all conform to LIFO principle, so the top of the stack can be the head or the tail.
Singly linked list implementation: Suitable for head insertion and head deletion, the top of the stack can only be the head.

Stack rules:Things that go in last must come out first (last in, first out) associative magazine

top: Two design options
1. If top points to the top element of the stack, assuming that the stack is empty, top == 0, then when an element is inserted into the empty stack, due to top points to the top element of the stack, top is still 0. At this time, it is impossible to distinguish whether it is an empty stack or a stack with one element. Then set top == -1 when the stack is empty, and when inserting an element into the stack , top == 0;
2. If top points to the next position of the top element of the stack, when the stack is empty, top == 0 is given. When an element is inserted into the stack, top points to The next position of the top element of the stack, so top == 1 (it can also be regarded as top being the number of data in the stack)

2. Implementation of stack

2.1 Implementation of header file—Stack.h

#pragma once
#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 STDestory(ST* pst);

//压栈
void STPush(ST* pst, STDataType x);
//出栈
void STPop(ST* pst);

//获取栈顶元素
STDataType STTop(ST* pst);

//判空
bool STEmpty(ST* pst);

//统计栈内元素个数
int STSize(ST* pst);

2.2 Implementation of source files—Stack.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "Stack.h"

//初始化
void STInit(ST* pst)
{
	assert(pst);
	pst->a = NULL;
	//表示top指向栈顶元素的下一个位置
	pst->top = 0;

	//表示top指向栈顶元素
	//pst->top = -1;

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

//压栈
void STPush(ST* pst, STDataType x)
{
	assert(pst);
	//判断数组栈空间是否足够
	if (pst->top == pst->capacity)
	{
		int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(pst->a, sizeof(STDataType) * newcapacity);
		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(pst->top > 0);
	pst->top--;
}

//获取栈顶元素
STDataType STTop(ST* pst)
{
	assert(pst);
	assert(pst->top > 0);
	return pst->a[pst->top - 1];
}

//判空
bool STEmpty(ST* pst)
{
	assert(pst);
	//判断数组栈为空
	//1、如果top是指向栈顶元素的下一个位置,那当top == 0时,栈为空
	//2、如果top时指向栈顶元素,那当top == -1时,栈为空
	/*if (pst->top == 0)
	{
		return true;
	}
	else
	{
		return false;
	}*/
	return pst->top == 0;
}

//统计栈内元素个数
int STSize(ST* pst)
{
	assert(pst);
	//1、如果top指向栈顶元素的话,栈内元素的个数为top+1;
	//2、如果top指向栈顶元素的下一个位置的话,栈内元素的个数为top;
	return pst->top;
}

2.3 Test of source file—test.c

#include "Stack.h"

int main()
{
	ST s;
	STInit(&s);
	STPush(&s, 1);
	STPush(&s, 2);
	STPush(&s, 3);
	printf("%d ", STTop(&s));
	STPop(&s);
	STPush(&s, 4);
	STPush(&s, 5);

	//入栈顺序(一种)  --  出栈顺序(多种)
	//是一对多的关系
	while (!STEmpty(&s))
	{
		printf("%d ", STTop(&s));
		STPop(&s);
	}
	printf("\n");
	return 0;
}

3. Display of actual test data of the stack

1. The relationship between popping and pushing from the stack is:one-to-many relationship.
2. The rules for putting elements on and off the stack:
Last in, first out.

3.1 Normal stack display:

3.2 Display of pushing into the stack and popping out at the same time:

Summarize

Okay, this blog ends here. If you have a better point of view, please leave a message in time. I will watch it carefully and learn from it.
If you don’t accumulate steps, you can’t reach a thousand miles; if you don’t accumulate small streams, you can’t become a river or sea.

Guess you like

Origin blog.csdn.net/2301_79585944/article/details/134973474