forked from iharsh234/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Dynamic_Stack.c
67 lines (58 loc) · 1.24 KB
/
Dynamic_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
#include <stdio.h>
#include <stdlib.h>
#include<limits.h>
typedef struct Stack
{
int top;
unsigned capacity;
int* array;
}Stack;
Stack* createStack(unsigned capacity)
{
struct Stack* stack = (Stack*) malloc(sizeof(Stack));
stack->capacity = capacity;
stack->top = -1;
stack->array = (int*) malloc(stack->capacity * sizeof(int));
return stack;
}
int Full(Stack* stack)
{ return stack->top == stack->capacity - 1; }
int Empty(Stack* stack)
{ return stack->top == -1; }
void push(Stack* stack, int item)
{
if (Full(stack))
return;
stack->array[++stack->top] = item;
printf("%d pushed to stack\n", item);
}
int pop(Stack* stack)
{
if (Empty(stack))
return INT_MIN;
return stack->array[stack->top--];
}
int peek(Stack* stack)
{
if (Empty(stack))
return INT_MIN;
return stack->array[stack->top];
}
int main()
{
Stack* stack = createStack(100);
push(stack, 14);
push(stack, 25);
push(stack, 38);
push(stack, 48);
printf("%d popped from stack\n", pop(stack));
printf("Top item is %d\n", peek(stack));
return 0;
}
//output
/*14 pushed to stack
25 pushed to stack
38 pushed to stack
48 pushed to stack
48 popped from stack
Top item is 38*/