forked from TECHOUS/DSKaKhel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimum_element_using_stack.cpp
More file actions
61 lines (54 loc) · 1.27 KB
/
minimum_element_using_stack.cpp
File metadata and controls
61 lines (54 loc) · 1.27 KB
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
#include <iostream>
/* PROGRAMME TO FIND THE MINIMUM ELEMENT USING A SINGLE STACK :') */
using namespace std;
int * STACK;
int top = 0;
int element;
int choice;
int size;
void push(int x)
{
STACK[top++]=x;
}
int pop()
{
return STACK[top--];
}
int main()
{
cout<<"Enter the size of the array"<<endl;
cin>>size;
cout<<"\nEnter the choice\n1.push()\n2.display minimum element"<<endl;
cin>>choice;
STACK = new int[size];
while(choice != -1)
{
switch(choice)
{
case 1:
cout<<"Enter the element"<<endl;
cin>>element;
// push(element);
if(top == 0)
{
push(element);
//cout<<"top is "<<top<<endl;
}
else if(element < STACK[0])
{
cout<<"\npopping larger value :-"<<STACK[0]<<"\npushing smaller value:-"<<element<<endl;
pop();
push(element);
}
break;
case 2:
cout<<"THE MINIMUM ELEMENT IS "<<endl;
for(int i = 0; i<top; i++)
{
cout<<STACK[i]<<endl;
}
}
cout<<"\nEnter the choice\n1.push()\n2.display minimum element"<<endl;
cin>>choice;
}
}