-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamicStackArray.c
86 lines (77 loc) · 1.66 KB
/
DynamicStackArray.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
#include<stdio.h>
#include<malloc.h>
struct DynamicStack
{
int top;
int capacity;
int *array;
};
struct DynamicStack * createStack()
{
struct DynamicStack *stack = (struct DynamicStack *)malloc(sizeof(struct DynamicStack));
if(!stack)
{
printf("Overflow in memory\n");
return NULL;
}
stack->capacity = 1; //starting with capacity one.
stack->top = -1;
stack->array = (int *)malloc(stack->capacity * sizeof(int));
if(!(stack->array))
{
printf("Overflow in memeory\n");
return NULL;
}
return stack;
}
void reallocateStack(struct DynamicStack **stack)
{
(*stack)->capacity = ((*stack)->capacity)*2;
realloc((*stack)->array, (*stack)->capacity);
}
int isStackEmpty(struct DynamicStack *stack)
{
return (stack->top == -1);
}
int isStackFull(struct DynamicStack *stack)
{
return (stack->top == stack->capacity - 1);
}
void push(struct DynamicStack **stack, int value)
{
if(isStackFull(*stack))
reallocateStack(stack);
(*stack)->top = (*stack)->top + 1;
(*stack)->array[(*stack)->top] = value;
}
int pop(struct DynamicStack **stack)
{
int resultingValue;
if(isStackEmpty(*stack))
{
printf("Underflow\n");
return 0;
}
resultingValue = (*stack)->array[(*stack)->top];
(*stack)->top = (*stack)->top - 1;
return resultingValue;
}
void traverse(struct DynamicStack *stack)
{
for(int i = stack->top; i>-1; i--)
{
printf("%d-->", stack->array[i]);
}
printf("\n");
}
void main(int argc, char const *argv[])
{
printf("Compiling successfully\n");
struct DynamicStack *stack = createStack();
push(&stack, 2);
push(&stack, 3);
push(&stack, 4);
push(&stack, 7);
push(&stack, 11);
traverse(stack);
}