The order of the stack stores the realization of various functions

Stack

1: Related concepts of the stack

Stack (Stack), also known as the stack , which is an operation restricted in linear form . Limit the linear table that only inserts and deletes at the end of the table. This end is called the top of the stack , while the other end is called the bottom of the stack . Inserting a new element to the stack, also referred to push, push or stack , which is a new element into the element above the stack, making the new top of the stack; a stack, also referred to remove elements from the stack Or unstack , it is to delete the top element of the stack, so that the adjacent element becomes the new top element (as for the term stack: n (tidy) pile ) can reflect the characteristics of the stack, such as adding new The data can only be placed at the top of a pile (it can reflect the logical relationship of the stack)

Important terms: stack top, stack bottom, empty stack

For the storage form of the stack, you can imagine a solid storage method:

( Empty stack ) Store data in a three-dimensional space.                                        A5 is the top of the stack, and a1 is the bottom of the stack (the stacking order: a1->a2->a3->a4->a5)

Rules : The top of the stack is allowed to insert and delete data, and the bottom of the stack is not allowed to insert and delete

That is to say, pushing (inserting) means adding data, and popping (deleting) means deleting data. These operations can only be operated from the top of the stack, that is, the lowest address (the address increases from top to bottom)

Features of the top of the stack : As a data structure , the stack is a special linear table that can only be inserted and deleted at one end . It stores data according to the principle of first- in -last-out . The first data is pushed into the bottom of the stack, and the last data is on the top of the stack. When data needs to be read, data is popped from the top of the stack (the last data is read out first) as above Figure (Pull order: a5->a4->a3->a2->a1)

Before learning the stack, you should first understand the basic concepts of the stack and some rules and characteristics to deepen your understanding of the stack.

Connection with ordinary linear table:

The data has the characteristics of adjacent one after the other, but there is a big difference between insertion and deletion


2: The realization of each function of stack order storage

Brief description: declaration, initialization, push, pop, insert data set (push), delete data (pop), judge whether the stack is empty/full

2.1> Statement

Malloc is not used here, but SqStack is defined to define it. So after the program runs, the space will be automatically reclaimed (the destruction code is omitted here)

#include<stdio.h>
#define MaxSize 5//栈中最大存储数量
typedef struct
{
	int data[MaxSize];//定义一个静态数组存放栈的数据
	int top;//栈顶指针
}SqStack;//tepedef重命名


void main()
{
	SqStack S;//声明一个顺序栈(与此同时计算机已分配空间)
 }

Use tepedef to name the structure SqStack in the statement, Sq is the abbreviation of sequence (n: sequence) . So a sequential stack allocates address space at the same time

2.2> Initialization

Analysis: After the address space is declared and allocated, the stack is currently an empty stack, and the top pointer of the stack points to the top element of the stack (the top of the stack is also the bottom of the stack in the empty stack). At this time, the pointer of the top of the stack points to a representative address, which indicates that the stack is empty

void InitStack(SqStack&S)
{
	S.top = -1;//初始化栈顶指针
}

. And -> conditions: "dot" is a member variable in the calling structure; if it is a pointer to a structure, it can be replaced with ->shu (here top is a member variable)

2.3> Empty

void StackEmpty(SqStack S)
{
	if (S.top == -1)
		printf("当前栈为空!\n");
	else
		printf("当前栈非空!\n");

}

 

2.4>Add data

Two cases of input data: define n as the number of input data

1: If n<maxsize, the stack is not filled

2 : If n>maxsize, it means that the stack space is insufficient and input should be stopped when the stack is full

void CreatStack(SqStack&S,int n)//输入数据
{
	printf("向栈中添加%d个数据:\n",n);
	int value;//保存每个位置的数据
	if (n < MaxSize)
	{
		while (S.top != n-1)
		{
			S.top = S.top + 1;//此时栈顶指针指向了栈底
			printf("data[%d]=", S.top);
			scanf("%d", &value);
			S.data[S.top] = value;//将数据传到第一个底部
		}
		printf("栈添加%d个数据完成!\n", S.top+1);
	}
	else {
		while (S.top != MaxSize-1)
		{
			S.top = S.top + 1;//此时栈顶指针指向了栈底
			printf("data[%d]=", S.top);
			scanf("%d", &value);
			S.data[S.top] = value;//将数据传到第一个底部
			if (S.top == MaxSize - 1)//栈已满
				printf("栈已满无法入栈!\n");
		}
		printf("栈添加%d个数据完成!剩余%d个数据无法入栈\n", S.top+1,n-(S.top+1));
	}
	
}

In the beginning, top is -1 under the bottom of the stack , and top should always point to the top of the stack when inputting data. So you should move top to the bottom of the stack before you start inputting data

If the stack is full during input, you should stop inputting data.

2.5> output elements in the stack 

The stack satisfies the principle of first -in-last-out to store data, so the stacking order of the stack table is from the top of the stack

The output also corresponds to two situations: one is the stack with remaining space and the other is the full stack output

void ShowStack(SqStack S)//打印
{
	printf("打印栈数据:\n");
	while(S.top!=-1)
	{
		printf("data[%d]=%d\n",S.top,S.data[S.top]);
		S.top--;//从栈顶向下移动
	}
}

 

2.6> Insert (press in)

Insert different order lists and linked lists, stack insertion and deletion can only be performed on the top of the stack

push (v: push (person or thing); move (body part); push (road); squeeze away; push;)

Insertion should be performed first to determine whether the stack has free space, if it is necessary to move the top node up to the new stack top node, and then perform the data assignment

void PushStack(SqStack &S, int e)//插入
{
	if (S.top == MaxSize - 1)
		printf("抱歉栈已满无法插入!\n");
	S.top = S.top + 1;//将top的位置向上移动到新的栈顶位置
	S.data[S.top] = e;
}

2.7>Delete (popup)

First save the deleted stack top data, and then move top down to the new stack top

The deletion here is only a logical deletion, and the top (old) data on the stack still exists in the computer.

void PopStack(SqStack &S)//删除栈顶元素
{
	int  e;
	e = S.data[S.top];//将弹出数据保存
	S.top--;//top向下移动到达新的栈顶
	printf("删除栈顶数据%d\n",e);
}

3: the whole body Code

#include<stdio.h>
#define MaxSize 6//栈中最大存储数量
typedef struct
{
	int data[MaxSize];//定义一个静态数组存放栈的数据
	int top;//栈顶指针
}SqStack;//tepedef重命名

void InitStack(SqStack&S)//初始化
{
	S.top = -1;//初始化栈顶指针
}

void StackEmpty(SqStack S)//判断空
{
	if (S.top == -1)
		printf("当前栈为空!\n");
	else
		printf("当前栈非空!\n");

}

void CreatStack(SqStack&S,int n)//输入数据
{
	printf("向栈中添加%d个数据:\n",n);
	int value;//保存每个位置的数据
	if (n < MaxSize)
	{
		while (S.top != n-1)
		{
			S.top = S.top + 1;//此时栈顶指针指向了栈底
			printf("data[%d]=", S.top);
			scanf("%d", &value);
			S.data[S.top] = value;//将数据传到第一个底部
		}
		printf("栈添加%d个数据完成!\n", S.top+1);
	}
	else {
		while (S.top != MaxSize-1)
		{
			S.top = S.top + 1;//此时栈顶指针指向了栈底
			printf("data[%d]=", S.top);
			scanf("%d", &value);
			S.data[S.top] = value;//将数据传到第一个底部
			if (S.top == MaxSize - 1)//栈已满
				printf("栈已满无法入栈!\n");
		}
		printf("栈添加%d个数据完成!剩余%d个数据无法入栈\n", S.top+1,n-(S.top+1));
	}
	
}

void PushStack(SqStack &S, int e)//插入
{
	if (S.top == MaxSize - 1)
		printf("抱歉栈已满无法插入!\n");
	S.top = S.top + 1;//将top的位置向上移动到新的栈顶位置
	S.data[S.top] = e;
}

void PopStack(SqStack &S)//删除栈顶元素
{
	int  e;
	e = S.data[S.top];//将弹出数据保存
	S.top--;//top向下移动到达新的栈顶
	printf("删除栈顶数据%d\n",e);
}


void ShowStack(SqStack S)//打印
{
	printf("打印栈数据:\n");
	while(S.top!=-1)
	{
		printf("data[%d]=%d\n",S.top,S.data[S.top]);
		S.top--;//从栈顶向下移动
	}
}

void main()
{
	SqStack S;//声明一个顺序栈(与此同时计算机已分配空间)
	InitStack(S);//初始化栈
	StackEmpty(S);//判空
	CreatStack(S,5);//向栈中添加数据
	PushStack(S, 99);//插入元素到栈顶
	ShowStack(S);//打印栈中数据
	PopStack(S);//删除栈顶数据

 }

 

Guess you like

Origin blog.csdn.net/qq_46861651/article/details/112979730