Data structure—implementation of array stack

Preface: Dear friends, we have already learned about the leading bidirectional circular linked list. There are also some special linear tables in the data structure, such as stacks and queues, so today we will implement the array stack.

Insert image description here

Table of contents:

1.
The concept of stack
2.
The implementation of stack
3.
Code Test

The concept of stack:

The concept and structure of a stack
Stack: a special linear list that only allows insertion and deletion of elements at a fixed end. One 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: The insertion operation of the stack is called push/push/push. The input data is on the top of the stack. Pop: The deletion operation of the stack is called Get out of the stack. The outgoing data is also on the top of the stack.
Insert image description here
Top of the stack (Top): The end of the linear table that allows insertion and deletion.
Bottom: Fixed, the other end that does not allow insertion and deletion.
Empty stack: an empty list without any elements.

Stack implementation:

The stack can generally be implemented using an array or a linked list. Relatively speaking, the array structure is more optimal. Because the cost of inserting data at the end of the array is relatively small.
Insert image description here

interface:

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

// 入栈
void STPush(ST* pst, STDataType x);
// 出栈
void STPop(ST* pst);
// 获取栈顶元素
STDataType STTop(ST* pst);

// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool STEmpty(ST* pst);
// 获取栈中有效元素个数
int STSize(ST* pst);

Here we need three files, a header file, a file to implement our various interfaces, and a file to test our code.
Insert image description here

Header file (Stack.h):

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


typedef int STDataType;

typedef struct Stack
{
    
    
	int* 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);

Insert image description here
Our top is the top of the stack. If our top=0, we point to the top element of the stack. If our top=1, then our top points to the next position of the top element of the stack.

Function implementation (Stack.c)

#include"Stack.h"

void STInit(ST* pst)
{
    
    
	assert(pst);

	pst->a = NULL;
	pst->capacity = 0;
	pst->top = 0;
}

void STDestroy(ST* pst)
{
    
    

}

// 栈顶插入删除
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);
int STSize(ST* pst);

Test code (test.c)

int main()
{
    
    
		ST s;
	STInit(&s);
	STPush(&s, 1);
	STPush(&s, 2);
	STPush(&s, 3);
	STPush(&s, 4);
	STPush(&s, 5);
		while (!STEmpty(&s))
	{
    
    
		printf("%d ", STTop(&s));
		STPop(&s);
	}
	printf("\n");
	return 0;
}

We push five data onto the stack here. When our stack is not empty, we access the top element of the stack and pop the top element off the stack. When our stack is empty, we exit the loop.
Insert image description here

int main()
{
    
    
	
	ST s;
	STInit(&s);
	STPush(&s, 1);
	STPush(&s, 2);
	STPush(&s, 3);
	printf("%d ", STTop(&s));
	STPop(&s);
	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;
}

Here we can push and pop the stack at the same time. We push 1, 2, and 3 first, and then pop the stack. Our stack is last in, first out, which means that the elements we push into the stack later will be popped out first. When we pop one off the stack, it is 3, when we pop it off the stack again, it is 2, and when we finally push it onto the stack, it is 4 and 5.
Insert image description here

Guess you like

Origin blog.csdn.net/Lehjy/article/details/134381362