forked from gaurav03kr/hello-hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stacks.c
75 lines (67 loc) · 1.29 KB
/
Stacks.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
//To implement stack over an array
#include<stdio.h>
#define TRUE 1
#define FALSE 0
#define MAX 5
struct stack
{
int data[MAX];
int top;
} typedef Stack;
int isEmpty(Stack s)
{
if (s.top== -1 )
return TRUE;
else
return FALSE;
}
int isFull(Stack s)
{
if (s.top== MAX)
return TRUE;
else
return FALSE;
}
void push(Stack *sp, int element)
{
if(isFull(*sp))
{
printf("OVERFLOW");
}
else
{
//add the new element at the top of stack
sp->top+=1;
sp->data[sp->top]=element;
}
}
int pop(Stack *sp)
{
if(isEmpty(*sp))
{
printf("UNDERFLOW");
return -1;
}
//delete the new element at the top of the stack
int top_element=sp->data[sp->top]; //gives the top element to a temporary variable
sp->top-=1;
return top_element;
}
int main()
{
Stack s;
s.top=-1;
printf("\n isEmpty() returning : %d",isEmpty(s));
printf("\n Popped Element is : %d",pop(&s)); //UNDERFLOW
push(&s,100);
push(&s,200);
printf("\n Popped Element is : %d",pop(&s));
push(&s,300);
printf("\n Popped Element is : %d",pop(&s));
push(&s,400);
push(&s,500);
push(&s,600);
push(&s,700);
printf("\n isFull() returning : %d\n",isFull(s));
return 0;
}