本文实例为大家分享了C语言数组栈实现模板的具体代码,供大家参考,具体内容如下
SeqStack.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#pragma once
#define MAX_SIZE 1024
typedef struct SEQSTACK
{
void * data[MAX_SIZE];
int size;
}SeqStack;
SeqStack* Init_SeqStack(); // 初始化栈
void Push_SeqStack(SeqStack* stack, void * data); // 入栈
void * Top_SeqStack(SeqStack* stack); // 返回栈顶元素
void Pop_SeqStack(SeqStack* stack); // 出栈
int IsEmpty(SeqStack* stack); // 判断是否为空
int Size_SeqStack(SeqStack* stack); // 返回栈中元素的个数
void Clear_SeqStack(SeqStack* stack); // 清空栈
void FreeSpace_SeqStack(SeqStack* stack); // 销毁栈
|
SeqStack.cpp 函数实现
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
#include "SeqStack.h"
#include <stdio.h>
#include <stdlib.h>
SeqStack* Init_SeqStack()
{
SeqStack* stack = (SeqStack*) malloc ( sizeof (SeqStack));
for ( int i = 0; i < MAX_SIZE; i++)
{
stack->data[i] = NULL;
}
stack->size = 0;
return stack;
}
void Push_SeqStack(SeqStack* stack, void * data)
{
if (stack == NULL)
return ;
if (stack->size == MAX_SIZE) // 栈为满
return ;
if (data == NULL)
return ;
stack->data[stack->size] = data;
stack->size++;
}
void * Top_SeqStack(SeqStack* stack) // 返回栈顶元素,不弹出
{
if (stack == NULL)
return NULL;
if (stack->size == 0)
return NULL;
return stack->data[stack->size - 1];
}
void Pop_SeqStack(SeqStack* stack) // 出栈
{
if (stack == NULL)
return ;
if (stack->size == 0)
return ;
stack->data[stack->size - 1] = NULL;
stack->size--;
}
int IsEmpty(SeqStack* stack) // 判断是否为空
{
if (stack == NULL)
return -1;
if (stack->size == 0)
return 1;
return 0;
}
int Size_SeqStack(SeqStack* stack)
{
if (stack == NULL)
return -1;
return stack->size;
}
void Clear_SeqStack(SeqStack* stack)
{
if (stack == NULL)
return ;
for ( int i = 0; i < stack->size; i++)
{
stack->data[i] = NULL;
}
stack->size = 0;
}
void FreeSpace_SeqStack(SeqStack* stack)
{
if (stack == NULL)
return ;
free (stack);
}
|
数组栈测试程序
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
|
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "SeqStack.h"
typedef struct PERSON
{
char name[64];
int age;
}Person;
int main()
{
SeqStack* stack = Init_SeqStack(); // 创建栈
Person p1, p2, p3, p4, p5;
strcpy (p1.name, "aaa" );
strcpy (p2.name, "bbb" );
strcpy (p3.name, "ccc" );
strcpy (p4.name, "ddd" );
strcpy (p5.name, "eee" );
p1.age = 10;
p2.age = 20;
p3.age = 30;
p4.age = 40;
p5.age = 50;
Push_SeqStack(stack, &p1); // 入栈
Push_SeqStack(stack, &p2);
Push_SeqStack(stack, &p3);
Push_SeqStack(stack, &p4);
Push_SeqStack(stack, &p5);
while (Size_SeqStack(stack) > 0)
{
Person* person = (Person*)Top_SeqStack(stack);
printf ( "Name:%s Age:%d\n" , person->name, person->age);
Pop_SeqStack(stack);
}
// 释放内存
FreeSpace_SeqStack(stack);
system ( "pause" );
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/wwxy1995/article/details/84992971