-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
95 lines (84 loc) · 1.49 KB
/
stack.c
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
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
#include "vector.h"
#include "ADTDefs.h"
# define _PRINT_ printf("Testing %s\n", __FUNCTION__) ;
# define _LINE_ printf("Line num: %d\n", __LINE__) ;
struct Stack
{
Vector * vector ;
};
Stack* StackCreate(size_t _initSize, size_t _blockSize)
{
Stack* stackTemp;
if ((_initSize < 0) ||(_blockSize < 1))
{
return NULL;
}
stackTemp = malloc(sizeof(Stack)) ;
if (! stackTemp)
{
return NULL;
}
stackTemp->vector = VectorCreate(_initSize, _blockSize) ;
if (! stackTemp->vector)
{
free(stackTemp);
return NULL;
}
return stackTemp ;
}
void StackDestroy(Stack * _stack)
{
if (_stack)
{
VectorDestroy( _stack->vector);
free(_stack);
}
return ;
}
ADTErr StackPush(Stack* _stack, int _item)
{
if (! _stack)
{
return ERR_NOT_INITIALIZE ;
}
return VectorAdd(_stack->vector,_item) ;
}
ADTErr StackPop(Stack* _stack, int* _item)
{
if (! _stack)
{
return ERR_NOT_INITIALIZE ;
}
return VectorDelete(_stack->vector, _item) ;
}
ADTErr StackTop(Stack* _stack, int* _item)
{
int _num_of ;
if (! _stack)
{
return ERR_NOT_INITIALIZE ;
}
VectorItemsNum(_stack->vector, &_num_of);
if (_num_of == 0)
{
return ERR_OVERFLOW;
}
return VectorGet(_stack->vector, _num_of - 1, _item) ;
}
int StackIsEmpty(Stack* _stack)
{
int _num_of = -1;
if ((_stack == NULL)||(_stack->vector == NULL))
{
return _num_of ;
}
VectorItemsNum(_stack->vector, &_num_of);
if (_num_of==-1)
{
return _num_of ;
}
return (_num_of==0) ;
}