-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStack_implimentation.c
105 lines (93 loc) · 1.79 KB
/
Stack_implimentation.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
95
96
97
98
99
100
101
102
103
104
105
#include <stdio.h>
#include <stdlib.h>
struct Stack
{
int size;
int top;
int* arr;
};
int isEmpty(struct Stack* ptr){
if (ptr->top==-1)
{
return 1;
}
else
{
return 0;
}
}
int isFull(struct Stack* ptr){
if (ptr->top==ptr->size)
{
return 1;
}
else
{
return 0;
}
}
void push(struct Stack * ptr,int element){
if (isFull(ptr))
{
printf("Stack OverFlow....\n");
}
else
{
ptr->top++;
ptr->arr[ptr->top] = element;
}
}
int pop(struct Stack* ptr){
int element = ptr->arr[ptr->top];
if (isEmpty(ptr))
{
printf("Stack Underflow....\n");
}
else
{
ptr->top--;
printf("%d element is popped\n",element);
}
return element;
}
int peek(struct Stack* ptr,int pos){
if (ptr->top-pos+1<0)
{
printf("Not a valid pos\n");
}
else
{
return ptr->arr[(ptr->top)+1-pos];
}
}
int stackTop(struct Stack* ptr){
return ptr->arr[ptr->top];
}
int stackBottom(struct Stack* ptr){
return ptr->arr[0];
}
int main()
{
struct Stack * s = (struct Stack*)malloc(sizeof(struct Stack));
s->size=5;
s->top=-1;
s->arr = (int*)malloc(s->size*(sizeof(int)));
printf("%d\n",isEmpty(s));
printf("%d\n",isFull(s));
push(s,19);
push(s,78);
push(s,278);
push(s,8);
push(s,728);
push(s,72);
pop(s);
// pop(s);
// pop(s);
// pop(s);
printf("%d\n",isEmpty(s));
printf("%d\n",isFull(s));
printf("The element is %d\n",peek(s,3));
printf("The Top element is %d\n",stackTop(s));
printf("The Bottom element is %d\n",stackBottom(s));
return 0;
}