-
Notifications
You must be signed in to change notification settings - Fork 0
/
28.Stack_Top_Bottom.c
69 lines (57 loc) · 1.19 KB
/
28.Stack_Top_Bottom.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
#include<stdio.h>
#include<stdlib.h>
typedef struct{
int stackSize;
int top;
int *arr;
}stack;
int isEmpty(stack *ptr){
if(ptr->top == -1)
return 1;
return 0;
}
int isFull(stack *ptr){
if(ptr->top == ptr->stackSize - 1)
return 1;
return 0;
}
void push(stack *ptr, int data){
if(isFull(ptr))
printf("Stack Overflow! Can't push %d in stack\n", data);
else{
ptr->top++;
ptr->arr[ptr->top] = data;
}
}
int pop(stack *ptr){
if(isEmpty(ptr))
return -1;
int popelement = ptr->arr[ptr->top];
ptr->top--;
return popelement;
}
int stackTop(stack *ptr){
if(isEmpty(ptr))
return -1;
return (ptr->arr[ptr->top]);
}
int stackBottom(stack *ptr){
if(isEmpty(ptr))
return -1;
return (ptr->arr[0]);
}
int main(){
stack * s = (stack *)malloc(sizeof(stack));
s->stackSize = 8;
s->top = -1;
s->arr = (int *)malloc(s->stackSize * sizeof(int));
push(s, 12);
push(s, 2);
push(s, 53);
push(s, 34);
push(s, 98);
pop(s);
printf("Stack Top element is : %d\n", stackTop(s));
printf("Stack Bottom element is : %d\n", stackBottom(s));
return 0;
}