顺序栈的C语言实现

本文使用c语言实现了顺序栈。栈的主要功能是Push和Pop,在二叉树的遍历中也会使用到。

1.代码部分

顺序栈结构体的定义:

1
2
3
4
5
typedef struct {
int* base;
int* top;
int stacksize;
}SqStack;

top指针指向的是栈顶元素的上一位。

入栈过程

函数部分定义了初始化、求长度、清空、摧毁、入栈和出栈这几个函数,都比较简单,不做过多介绍。值得注意的是,真正决定它是顺序栈而不是链栈的是’sta->top++’这一句,这一句是在插入之后top指针的地址进行移动,在物理结构上完成顺序表。其中地址的移动位置之差为4个字节即一个整型变量的大小。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"head.h"


void Init_sq(SqStack* sta) // Functio Init spares memory for pointer base and top.
{
sta->base = (int*)malloc(10 * sizeof(int));
if (!sta->base) { printf("error!"); }
sta->top = sta->base;
sta->stacksize = maxsize;
}


void StackEmpty(SqStack* sta)
{
if (sta->base == sta->top) { printf("Empty.\n"); }
else { printf("Not empty.\n"); }
}


void StackLength(SqStack* sta)
{
printf("The length of the stack is %d.\n", sta->top - sta->base);
}


void ClearStack(SqStack* sta)
{
if (sta->base) { sta->top = sta->base; }
}


void DestoryStack(SqStack* sta) { if (sta) { free(sta); } }


void Push(SqStack* sta)
{
int i;
if (sta->top - sta->base == sta->stacksize) { printf("ERROR! Stack is full.\n"); }
printf("Input the number you want to push in:\n");
scanf_s("%d", &i);
setvbuf(stdin, NULL, _IOFBF, 512);
*sta->top = i;
/*printf("Position: %p\n", sta->top);
printf("%d\n", *sta->top);
Input: 2
Output: address, 2*/
sta->top++; // The auto increment operation on the pointer will change the address of the pointer accordingly (address + 4 bytes(one int) in length).
/*printf("%d\n", *sta->top);
printf("Position: %p\n", sta->top);
Output: address+4, a rondom number*/
// The 50 line shows this is a linear stack.
}


void Pop(SqStack* sta)
{
int e;
if (sta->base == sta->top) { printf("ERROR!"); }
--sta->top;
e = *sta->top;
printf("The number poped is %d\n", e);
}

主函数部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"head.h"


int main()
{
SqStack *sqs = (SqStack*)malloc(sizeof(SqStack));
Init_sq(sqs);
Push(sqs); //这些部分内容可自行修改
Push(sqs);
Push(sqs);
StackLength(sqs);
Pop(sqs);
Pop(sqs);
StackLength(sqs);
return 0;
}

2.反思与改进

  1. 决定它是顺序栈的关键在于top指针的移动方式

  2. 一个结构体所占内存的大小不是结构内部变量的简单加和,因为cpu从内存中读取内存的机制不是一个数据一个数据地读取。结构体所占总内存为其成员变量中所占空间最大数据类型的整数倍。比如一个结构体定义了一个char a、一个int b、一个double c,分别占据1个、4个、8个字节,计算机将a存在0号位,却不能在1-4号位存b,因为这样子5个总单元长度不是int所占用4个字节的整数倍,所以b要存在4-7号位,同理c直接存放在8-15位即可。

    存储方式

  3. 出栈时无需清空栈里地出栈元素,只需要将栈顶指针下移即可。

  4. 在main函数中遇到了如下问题:

    报错

产生这个问题的原因是内存泄漏。未修改之前我在主函数中push了三次导致内存泄漏,将base和top指针所占内存扩大十倍之后问题消失,详细原因还在研究。

查看评论