-
Notifications
You must be signed in to change notification settings - Fork 195
/
get_Minimum_with_O(1)_constant_space.c
118 lines (94 loc) · 1.63 KB
/
get_Minimum_with_O(1)_constant_space.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
106
107
108
109
110
111
112
113
114
115
116
117
118
#include<stdio.h>
#include<conio.h>
int push(int element , int *top , int *stack)
{
*top = *top + 1;
stack[*top] = element;
}
int pop(int *stack , int *top)
{
int element;
if(*top > -1)
{
element = stack[*top];
*top = *top - 1;
return element;
}
else
{
printf("\n== STACK EMPTY == \n");
return -99999; // means nothing is popped
}
}
int main()
{
int stack[100];
int choice , x , top=-1 ,i , y , me , ce ;
printf("Enter the operation : \n 1. Push \n 2.Pop \n 3.check minimum \n 4.STOP \n");
scanf("%d",&choice);
while(choice != 5)
{
if (choice == 1)
{
printf("\nEnter the number to be pushed");
scanf("%d",&x);
if (top > -1)
{
if ( x < me)
{
ce = 2 * x - me;
push(ce , &top , stack);
me = x;
}
else
{
push(x , &top , stack);
}
}
else
{
ce = x;
push(ce , &top , stack);
me = x;
}
}
else if (choice == 2)
{
if (top > -1)
{
y = pop(stack , &top);
if (y != -99999)
{
printf("\n Top Element popped");
if( y < me )
{
me = 2 * me - y;
}
}
}
}
else if( choice == 3)
{
if (top > -1)
printf("\nMinimum element in the stack = %d \n\n" , me );
else
printf("\n ==== Stack Empty ======");
}
else if (choice == 4)
{
if (top > -1)
{
printf("\n === Main Stack === \n ");
for (i=top;i>=0;i--)
{
printf("\n%d",stack[i]);
}
}
else
printf(" \n ==== STACK EMPTY === \n ");
}
printf("\n\nEnter the operation : \n 1. Push \n 2.Pop \n 3.check minimum \n 4.See Full Stack \n 5.STOP \n");
scanf("%d",&choice);
}
return 0;
}