Realization of stack dynamic allocation related functions

Dynamic stack allocation

Static and dynamic have a high similarity, all the logic is the same, here is not the description

1>Define structure and declaration stack

When allocating space for the stack, the default continuous address space in the computer is not used. Using the malloc function to open up the address memory space is the same as the basic operation of statically allocating the address space. The variables defined in the structure are different, and only the top and bottom pointers of the stack are defined in the dynamic allocation

#include<stdio.h>
#include<stdlib.h>
#define MaxSize 5

typedef struct SqStack
{
	int *top;//栈顶指针
	int *base;//栈底指针
}SqStack;//typedef重命名为SqStack

void main()
{
	SqStack S;//声明一个栈
}

2>Initialize the stack

Use malloc function to allocate memory space, and point the base address to the first address of the space . After initialization, the top of the stack and the bottom of the stack are the same, so the top of the stack is initialized to the bottom of the stack

void InitStack(SqStack &S)
{
	S.base = (int*)malloc(sizeof(int)*MaxSize);//为栈分配内存空间,不是连续的地址空间
	if (!S.base)//为空输出提示
		printf("抱歉内存分配失败!");
	else
		printf("内存分配完成,初始化成功!\n");
	S.top = S.base;//初始栈栈顶等价于栈底,讲栈顶初始化为栈底
}

In this way, the stack initialization is completed, and the base address also points to the first address of the opened space (that is, the bottom of the stack)

3>Add data

Input data If the number of input data is n:

n>maxsize, the input cannot be completed, all is input

n<maxsize can complete all input

Two inputs are used here to better understand the availability of memory space during the stack input process:

A better and concise way can be (s.top-s.base==maxsize) to judge whether the space is full*(s.top)++=e; first push the data into the stack and then move the pointer, which can save a lot of code . The above method is easy to understand

void CreatStack(SqStack &S,int n)
{
	int data;//保存数据
	printf("向栈中添加%d个数据:\n", n);
	if (n <= MaxSize)
	{
		for (int i = 0; i < n ; i++)
		{
			printf("data[%d]=", i);
			scanf("%d", &data);
			*S.top++ = data;//先赋值后移动位置
		}
	}
	else
	{
		for (int i = 0; i < MaxSize ; i++)
		{
			printf("data[%d]=", i);
			scanf("%d", &data);
			*S.top++= data;//先赋值后自增位置
		}
		printf("剩余%d元素无法入栈!\n",n-MaxSize);
	}
}

The amount of input data is less than the allocated memory space

The amount of input data is greater than the allocated memory space

4>Output data

In the input process, when the last data is completed, it is first assigned to the address space and then the pointer moves. So after the input data is over, the pointer is actually above the top of the stack ( when the input data can be modified to the top of the stack, there is no modification here), and the pointer is moved forward before the output is data

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

The input data space is less than the open space:

The input data space is larger than the open space

5>Insert data to the top of the stack

Inserting data to the top of the stack needs to determine whether the stack space is sufficient, and it can be inserted only when there is remaining space. Since the pointer is above the node after the last data is input in the input data, the pointer is first assigned and then moved when inserting the top of the stack

void PushStack(SqStack &S, int e)//将元素入栈
{
	if (S.top - S.base ==MaxSize)
		printf("当前栈已满!\n");
	*S.top++ = e;//赋值后移动节点到下一处
}

Insert data to the top of the stack 

 

6>Delete the top element of the stack

The same reason is that in the input data, the pointer after the last data input and above the node, so when deleting the top of the stack, the pointer is moved first and then assigned

void  PopStack(SqStack &S)//元素出栈
{
	int e=*(S.top--);//保存栈顶数据
	printf("删除栈顶元素%d\n",e);
}

Insert first and delete immediately

 

Regular input deletes the top element of the stack

7>Get the top element of the stack

Since the position of the data pointer is at the end of the data input, the pointer is moved first and then the data is obtained like deleting the top of the stack.

void  GetElemStack(SqStack S)//得到栈顶元素
{
	S.top--;
	int e = *S.top;
	printf("栈顶元素为%d\n", e);

}

8> Overall code

#include<stdio.h>
#include<stdlib.h>
#define MaxSize 5

typedef struct SqStack
{
	int *top;//栈顶指针
	int *base;//栈底指针
}SqStack;//typedef重命名为SqStack

void InitStack(SqStack &S)
{
	S.base = (int*)malloc(sizeof(int)*MaxSize);//为栈分配内存空间,不是连续的地址空间
	if (!S.base)//为空输出提示
		printf("抱歉内存分配失败!");
	else
		printf("内存分配完成,初始化成功!\n");
	S.top = S.base;//初始栈栈顶等价于栈底,讲栈顶初始化为栈底
}

void EmptyStack(SqStack S)
{
	if (S.top==S.base)
		printf("当前为空栈!\n");
	else
		printf("当前栈不为空栈!\n");
}

void CreatStack(SqStack &S,int n)
{
	int data;//保存数据
	printf("向栈中添加%d个数据:\n", n);
	if (n <= MaxSize)
	{
		for (int i = 0; i < n ; i++)
		{
			printf("data[%d]=", i);
			scanf("%d", &data);
			*S.top++ = data;//先赋值后移动位置
		}
	}
	else
	{
		for (int i = 0; i < MaxSize ; i++)
		{
			printf("data[%d]=", i);
			scanf("%d", &data);
			*S.top++= data;//先赋值后自增位置
		}
		printf("剩余%d元素无法入栈!\n",n-MaxSize);
	}
}

void PushStack(SqStack &S, int e)//将元素入栈
{
	if (S.top - S.base ==MaxSize)
		printf("当前栈已满!\n");
	*S.top++ = e;//赋值后移动节点到下一处
}

void  PopStack(SqStack &S)//元素出栈
{
	int e=*(S.top--);//保存栈顶数据
	printf("删除栈顶元素%d\n",e);
}

void  GetElemStack(SqStack S)//得到栈顶元素
{
	S.top--;
	int e = *S.top;
	printf("栈顶元素为%d\n", e);

}

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

void main()
{
	SqStack S;//声明一个栈
	InitStack(S);//初始化
	EmptyStack(S);//判断是否空栈
	CreatStack(S,4);//添加数据
	PushStack(S,99);//将元素插入到栈顶
	PopStack(S);//将栈顶数据删除
	GetElemStack(S);//得到栈顶元素
	ShowStack(S);//输出栈数据
}

 

 

Guess you like

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