Data structure - stack (9)

data structure

foreword

A stack is a special linear list that only allows insertion and deletion of elements at one end. The end of the data insertion and deletion operation is called the top of the stack, and the other end is called the bottom of the stack. The elements in the stack follow the principle of last in first out (LIFO, Last In First Out).

1. Introduction to the stack

1. The English of the stack is (stack)

2. The stack is an ordered list of FILO-First In Last Out.

3. Stack (stack) is a special linear table that restricts the insertion and deletion of elements in the linear table to the same end of the linear table. The end that allows insertion and deletion is the variable end, called the top of the stack (Top), and the other end is the fixed end, called the bottom of the stack (Bottom).

4. According to the definition of the stack, it can be seen that the first element to be placed in the stack is at the bottom of the stack, the last element is at the top of the stack, and the deletion element is just the opposite. The last element is deleted first, and the first element is last. delete.

Graphically illustrate the order of pop and push
insert image description here

Second, the implementation of the stack

There are two types of stacks: sequential stacks and linked stacks, so the implementation of the stack can be implemented using an array or a linked list.
But relatively speaking, the structure implementation of the array is better.

1. The structure of the stack

Since stack access is at the tail, a top is used to mark the tail.

// 支持动态增长的栈
typedef int STDataType;
typedef struct Stack
{
    
    
	STDataType *a;//存储数据的数组
	int top; //栈顶的位置
	int capacity; //栈能存储的最大容量
}stack;
//静态栈
typedef int STDataType;
#define N 10
typedef struct Stack
{
    
    
	STDataType a[N];
	int top; // 栈顶
}Stack;

2. Stack initialization

//栈的初始化
int StackInit(stack *st)
{
    
    
	assert(st);   
	//这里给栈的初始大小为4个整型变量的大小
	st->a = (STDataType*)malloc(sizeof(stack)* 4);
	st->top = -1;//给栈顶一个初始值也可设置为其他值
	st->capacity = 4;//给一个初始大小,也可设置为其他值
	return 1;
}

insert image description here

3. Push into the stack

//进栈 
int StackPush(stack *st, STDataType x)
{
    
    
	assert(st);
	
	if (st->top == st->capacity)//如果栈满则扩容
	{
    
    
		STDataType* tmp = (STDataType*)realloc(st->a, sizeof(stack)* st->capacity * 2);

		if (tmp == NULL)
		{
    
    
			exit(-1);//结束整个程序
		}
		st->capacity *= 2;
		st->a = tmp;
	}

	//在top位置插入数据后,top++表示栈顶向后移一个位置
	st->a[st->top++] = x;
	printf("%d进栈成功\n",x); 
}

insert image description here

4. Pop out

//出栈 
void StackPop(stack* st)
{
    
    
	assert(st);
	//assert(!StackEmpty(st));//判断栈非空,若空则不进行删除操作
	st->top--;//top--即可,下一次数据入栈会直接覆盖top位置的值
}

insert image description here
After popping the stack twice, there are only two stacks left

5. Judgment of the stack

//判断栈是否为空
void StackEmpty(stack* st)
{
    
    
	assert(st);
	if (st->top == -1)//若top为-1,说明栈中没有元素,为空
	{
    
    
		printf("栈为空\n"); 
	}
	else
	printf("栈不为空\n"); 
}

//得到栈的数据个数
void StackSize(stack *st)
{
    
    
	assert(st);
	printf("有%d个栈\n",(st->top)+1);  //从-1开始所以要初始化为0 
}

insert image description here

6. Take the top element of the stack

//取栈顶元素 
void StackTop(stack* st)
{
    
    
	assert(st);
//	assert(!StackEmpty(st));

	printf( "栈顶为%d\n",st->a[st->top - 1]);
}

7. Clearing the stack

void Stackclear(stack* st)
{
    
    
	assert(st);
	//assert(!StackEmpty(st));//判断栈非空,若空则不进行删除操作
	st->top=-1;//top--即可,下一次数据入栈会直接覆盖top位置的值
	printf("栈清空成功\n");
}

insert image description here

8. Destruction of the stack

//栈的销毁
void StackDestroy(stack* st)
{
    
    
		
	assert(st);
	free(st->a);
	st->a = NULL;
	st->capacity = 0;
	st->top = 0;
	free(st);
	printf("栈销毁成功\n"); 
}

Summarize

insert image description here

Attach the full code

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
// 支持动态增长的栈,本文以动态增长的栈为例
typedef int STDataType;
typedef struct Stack
{
    
    
	STDataType *a;//存储数据的数组
	int top; //栈顶的位置
	int capacity; //栈能存储的最大容量
}stack;

//栈的初始化
int StackInit(stack *st)
{
    
    
	assert(st);   
	//这里给栈的初始大小为4个整型变量的大小
	st->a = (STDataType*)malloc(sizeof(stack)* 4);
	st->top = -1;//给栈顶一个初始值也可设置为其他值
	st->capacity = 4;//给一个初始大小,也可设置为其他值
	return 1;
}

//进栈 
int StackPush(stack *st, STDataType x)
{
    
    
	assert(st);
	
	if (st->top == st->capacity)//如果栈满则扩容
	{
    
    
		STDataType* tmp = (STDataType*)realloc(st->a, sizeof(stack)* st->capacity * 2);

		if (tmp == NULL)
		{
    
    
			exit(-1);//结束整个程序
		}
		st->capacity *= 2;
		st->a = tmp;
	}
	//在top位置插入数据后,top++表示栈顶向后移一个位置
	st->a[st->top++] = x;
	printf("%d进栈成功\n",x); 
//	if(st->top>3)
//	printf("%d进栈失败\n",x); 
}
//判断栈是否为空
void StackEmpty(stack* st)
{
    
    
	assert(st);
	if (st->top == -1)//若top为-1,说明栈中没有元素,为空
	{
    
    
		printf("栈为空\n"); 
	}
	else
	printf("栈不为空\n"); 
}

//得到栈的数据个数
void StackSize(stack *st)
{
    
    
	assert(st);
	printf("有%d个栈\n",(st->top)+1);  //从-1开始所以要初始化为0 
}
//出栈 
void StackPop(stack* st)
{
    
    
	assert(st);
	//assert(!StackEmpty(st));//判断栈非空,若空则不进行删除操作
	st->top--;//top--即可,下一次数据入栈会直接覆盖top位置的值
}
//取栈顶元素 
void StackTop(stack* st)
{
    
    
	assert(st);
//	assert(!StackEmpty(st));

	printf( "栈顶为%d\n",st->a[st->top - 1]);
}
//栈的销毁
void StackDestroy(stack* st)
{
    
    	
	assert(st);
	free(st->a);
	st->a = NULL;
	st->capacity = 0;
	st->top = 0;
	free(st);
	printf("栈销毁成功\n"); 
}
void Stackclear(stack* st)
{
    
    
	assert(st);
	//assert(!StackEmpty(st));//判断栈非空,若空则不进行删除操作
	st->top=-1;//top--即可,下一次数据入栈会直接覆盖top位置的值
	printf("栈清空成功\n");
}
int main()
{
    
    
	stack st;
	int ret=StackInit(&st);
	if(ret==1)
	printf("栈初始化成功\n"); 
    StackPush(&st,1);
    StackPush(&st,3);
    StackPush(&st,5);
    StackPush(&st,7);
    StackEmpty(&st);
    StackSize(&st);
    StackPop(&st); 
    StackSize(&st);
    StackPop(&st); 
    StackSize(&st);
    StackTop(&st);
    Stackclear(&st);
    StackSize(&st);
 } 

Guess you like

Origin blog.csdn.net/qq_51963216/article/details/128840344