-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqstack.c
More file actions
101 lines (82 loc) · 1.65 KB
/
sqstack.c
File metadata and controls
101 lines (82 loc) · 1.65 KB
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
91
92
93
94
95
96
97
98
99
100
101
#include <stdio.h>
#include <stdlib.h>
#include "sqstack.h"
struct t_sqstack {
StackElemType *data;
int top;
int size;
};
struct t_sqstack *stack_init(int size)
{
struct t_sqstack *pStack;
if((pStack = malloc(sizeof(struct t_sqstack))) == NULL)
{
printf("stack malloc err 1\n");
return NULL;
}
// printf("pStack = %p\n", pStack);
pStack->size = size;
if((pStack->data = malloc(sizeof(StackElemType) * pStack->size)) == NULL)
{
free(pStack);
printf("stack malloc err 2\n");
return NULL;
}
// printf("pStack->data = %p\n", pStack->data);
pStack->top = -1;
return pStack;
}
Status stack_push(struct t_sqstack *pStack, StackElemType e)
{
if(pStack->top == pStack->size - 1)
return -1;
pStack->top++;
pStack->data[pStack->top] = e;
return 0;
}
Status stack_pop(struct t_sqstack *pStack, StackElemType *e)
{
if(pStack->top == -1)
return -1;
if(e != NULL)
*e = pStack->data[pStack->top];
pStack->top--;
return 0;
}
Status stack_get_top(struct t_sqstack *pStack, StackElemType *e)
{
if(pStack->top == -1)
return -1;
if(e != NULL)
*e = pStack->data[pStack->top];
return 0;
}
Status stack_empty(struct t_sqstack *pStack)
{
return (pStack->top == -1);
}
void stack_clear(struct t_sqstack *pStack)
{
pStack->top = -1;
}
int stack_lenght(struct t_sqstack *pStack)
{
return (pStack->top + 1);
}
void stack_destroy(struct t_sqstack *pStack)
{
// printf("(*pStack)->data = %p\n", (*pStack)->data);
// printf("*pStack = %p\n", *pStack);
free(pStack->data);
free(pStack);
// *pStack = NULL;
}
void stack_traverse(struct t_sqstack *pStack, void (*vi)(StackElemType))
{
int i = 0;
while(i <= pStack->top)
{
vi(pStack->data[i++]);
}
}
/* */