Data structure-stack (C language)

github code download

github code:
https://github.com/Kyrie-leon/Data_Structures/tree/main/stack_queue

1. The concept and structure of the stack

Stack : A special linear form, in which only one end fixed for insert and delete elements operations.One end of the data insertion and deletion operations is called the top of the stack, and the other end is called the bottom of the stack. Data elements in the stack obeyLIFO (Last In First Out)in principle.
Push : The operation of inserting the stack is called push/push/push,Incoming data is at the top of the stack.
Pop : The delete operation of the stack is called pop.The data is also on the top of the stack
Insert picture description here
Insert picture description here

Second, the implementation of the stack

The implementation of the stack can generally be implemented using an array or a linked list , and the structure of the array is relatively better. Because the array is inThe cost of inserting data on the tail is relatively small,As shown below.

Insert picture description here
Insert picture description here

However, if the following structure is used to implement the stack, the tail insertion needs to move the data , causing a lot of computational overhead.

Insert picture description here

2.1 Stack storage definition

The static storage stack is implemented
using a static one-dimensional array a[N], but doing so will cause too much space waste or insufficient space

#define N 10
typedef int STDataType;
typedef struct Stack Stack;
struct Stack
{
    
    
	STDataType a[N];
	int _top;	//栈顶
};

Supports dynamic growth of the stack
Use a pointer _a to dynamically open up memory to achieve stack storage

typedef int STDataType;
typedef struct Stack Stack;
//支持动态增长的栈
struct Stack
{
    
    
	STDataType * _a;
	int _top;			//栈顶
	int _capacity;		//容量
};

2.2 Initialization of the stack

Initialization: By default, four STDataType-sized spaces are opened for the stack

  • capacity: the capacity of the stack , default 4
  • _top: Indicates the top of the stack . We agree that _top=0 means the stack is empty. After that, _top will increase by 1 for every element pushed onto the stack.That is, the following table of the array pointed to by _top is always 1 larger than the actual stack top element index

Insert picture description here

Insert picture description here

//栈的初始化
void StackInit(Stack * ps)
{
    
    
	assert(ps);
	ps->_a = (STDataType *)malloc(sizeof(Stack) * 4);	//默认数组大小为4
	ps->_top = 0;		//栈为空,则栈顶为0
	ps->_capacity = 4;	//默认栈的容量为4
}

2.3 Push into the stack

It has been agreed that the following table of the array pointed to by _top is always 1 larger than the actual stack top element subscript. You
only need to store the data in the _top subscript array, and then add 1 to _top.
Insert picture description here
Insert picture description here

//入栈
void StackPush(Stack * ps, STDataType data)
{
    
    
	assert(ps);
	//判断栈是否满了,满了则增容
	if (ps->_top == ps->_capacity)
	{
    
    
		ps->_capacity *= 2;	//每次扩容2倍
		STDataType * tmp = (STDataType *)realloc(ps->_a, sizeof(Stack)*ps->_capacity);
		//判断内存是否申请成功
		if (NULL == tmp)
		{
    
    
			printf("扩容失败\n");
			exit(-1);
		}
		ps->_a = tmp;
	}
	//入栈
	ps->_a[ps->_top] = data;	
	ps->_top++;
}

2.4 Pop

The following table of the array pointed to by _top is always 1 greater than the subscript of the actual stack top element
Therefore, you only need to reduce _top by 1.

Insert picture description here

//出栈
void StackPop(Stack * ps)
{
    
    
	assert(ps);
	assert(ps->_top>0);
	ps->_top--;
}

2.5 Get the top element of the stack

The following table of the array pointed to by _top is always 1 greater than the subscript of the actual stack top element
So the subscript of the top element of the stack is _top-1

//获取栈顶元素
STDataType StackTop(Stack *ps)
{
    
    
	assert(ps);
	assert(ps->_top>0);

	return ps->_a[ps->_top-1];	
}

2.6 Get the number of valid elements in the stack

Because the array element index starts from 0, so _top represents the number of valid elements in the stack


//获取栈中有效元素个数
int StackSize(Stack * ps)
{
    
    
	assert(ps);
	return ps->_top;
}

2.7 Check if the stack is empty

The value of _top can be used to determine whether the stack is empty.
If _top is 0, it means it is empty.
If _top is 1, it means it is not empty. If _top is 1, it means it is not empty. If it is negative, it returns 0.

//检测栈是否为空,如果为空返回非0,不为空返回0
int StackEmpty(Stack* ps)
{
    
    
	assert(ps);
	return !(ps->_top);
}

2.8 Destroy the stack


//销毁栈
void StackDestory(Stack * ps)
{
    
    
	assert(ps);
	free(ps->_a);
	ps->_a = NULL;
	ps->_capacity = ps->_top = 0;
}

Three, stack test

#include "stack.h"

void TestStack()
{
    
    
	Stack ps;
	StackInit(&ps);
	StackPush(&ps, 1);
	StackPush(&ps, 2);
	StackPush(&ps, 3);
	StackPush(&ps, 4);
	StackPush(&ps, 5);
	while (!StackEmpty(&ps))
	{
    
    

		printf("%d, %d\n", StackTop(&ps),StackSize(&ps));
		StackPop(&ps);
	}

	
}

int main()
{
    
    
	TestStack();
	system("pause");
	return 0;
}

Insert picture description here

Fourth, the code list

4.1 stack.h

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

#define N 10
typedef int STDataType;
typedef struct Stack Stack;
//struct Stack
//{
    
    
//	STDataType a[N];
//	int _top;	//栈顶
//};

//支持动态增长的栈
struct Stack
{
    
    
	STDataType * _a;
	int _top;			//栈顶
	int _capacity;		//容量
};

//初始化栈
void StackInit(Stack * ps);

//入栈
void StackPush(Stack * ps, STDataType data);

//出栈
void StackPop(Stack * ps);

//获取栈顶元素
STDataType StackTop(Stack *ps);

//获取栈顶中有效元素个数
int StackSize(Stack * ps);

//检测栈是否为空,如果为空返回非0,不为空返回0
int StackEmpty(Stack* ps);

//销毁栈
void StackDestory(Stack * ps);

4.2 stack.c

#include "stack.h"

//栈的初始化
void StackInit(Stack * ps)
{
    
    
	assert(ps);
	ps->_a = (STDataType *)malloc(sizeof(Stack) * 4);	//默认数组大小为4
	ps->_top = 0;		//栈为空,则栈顶为0
	ps->_capacity = 4;	//默认栈的容量为4
}

//入栈
void StackPush(Stack * ps, STDataType data)
{
    
    
	assert(ps);
	//判断栈是否满了,满了则增容
	if (ps->_top == ps->_capacity)
	{
    
    
		ps->_capacity *= 2;	//每次扩容2倍
		STDataType * tmp = (STDataType *)realloc(ps->_a, sizeof(Stack)*ps->_capacity);
		//判断内存是否申请成功
		if (NULL == tmp)
		{
    
    
			printf("扩容失败\n");
			exit(-1);
		}
		ps->_a = tmp;
	}
	//入栈
	ps->_a[ps->_top] = data;	
	ps->_top++;
}

//出栈
void StackPop(Stack * ps)
{
    
    
	assert(ps);
	assert(ps->_top>0);
	ps->_top--;
}

//获取栈顶元素
STDataType StackTop(Stack *ps)
{
    
    
	assert(ps);
	assert(ps->_top>0);

	return ps->_a[ps->_top-1];	
}

//获取栈中有效元素个数
int StackSize(Stack * ps)
{
    
    
	assert(ps);
	return ps->_top;
}

//检测栈是否为空,如果为空返回非0,不为空返回0
int StackEmpty(Stack* ps)
{
    
    
	assert(ps);
	return !(ps->_top);
}

//销毁栈
void StackDestory(Stack * ps)
{
    
    
	assert(ps);
	free(ps->_a);
	ps->_a = NULL;
	ps->_capacity = ps->_top = 0;
}

4.3 test.c

#include "stack.h"

void TestStack()
{
    
    
	Stack ps;
	StackInit(&ps);
	StackPush(&ps, 1);
	StackPush(&ps, 2);
	StackPush(&ps, 3);
	StackPush(&ps, 4);
	StackPush(&ps, 5);
	while (!StackEmpty(&ps))
	{
    
    

		printf("%d, %d\n", StackTop(&ps),StackSize(&ps));
		StackPop(&ps);
	}

	
}

int main()
{
    
    
	TestStack();
	system("pause");
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_40076022/article/details/112282137