-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
108 lines (93 loc) · 1.98 KB
/
Copy pathstack.c
File metadata and controls
108 lines (93 loc) · 1.98 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
102
103
104
105
106
107
108
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
struct stack
{
int size;
int capacity;
int* data;
};
typedef struct stack Stack;
STACK stack_init_default(void)
{
Stack* pStack;
pStack = (Stack*)malloc(sizeof(Stack));
if(pStack!=NULL)
{
pStack->size=0;
pStack->capacity=7;
pStack->data = (int*)malloc(sizeof(int) * pStack->capacity);
if(pStack->data == NULL)
{
free(pStack);
return NULL;
}
}
return (STACK) pStack;
}
void stack_destroy(STACK* phStack)
{
Stack* pStack = (Stack*)* phStack;
free(pStack->data);
free(pStack);
*phStack = NULL;
//printf("Destroyification complete\n");
}
Status stack_push(STACK hStack, int number)
{
int* temp;
int i;
Stack* pStack = (Stack*) hStack;
//if there is no room, make room
if(pStack->size >= pStack->capacity)
{
temp=(int*)malloc(sizeof(temp) * pStack->capacity * 2);
if(temp==NULL)
{
return FAILURE;
}
for(i=0; i<pStack->size; i++)
{
temp[i]=pStack->data[i];
}
pStack->capacity *= 2;
free(pStack->data);
pStack->data=temp;
}
//now assume there is room so place the item in
pStack->data[pStack->size] = number;
pStack->size++;
return SUCCESS;
}
Status stack_pop(STACK hStack)
{
Stack* pStack = (Stack*) hStack;
if(pStack->size <= 0)
{
return FAILURE;
}
pStack->size--;
return SUCCESS;
}
Boolean stack_empty(STACK hStack)
{
Stack* pStack = (Stack*) hStack;
return (Boolean) pStack->size <=0;
}
int stack_top(STACK hStack, Status* pStatus)
{
Stack* pStack = (Stack*) hStack;
if(stack_empty(hStack))
{
if(pStatus != NULL)
{
*pStatus= FAILURE;
}
return -31337;
}
if(pStatus != NULL)
{
*pStatus= SUCCESS;
}
return pStack->data[pStack->size-1];
}