-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 8 assignment.txt
129 lines (108 loc) · 1.79 KB
/
Day 8 assignment.txt
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
119
120
121
122
123
124
125
126
127
128
129
Question 1]
#include<iostream.h>
void main()
{
int n;
long res=1;
int arr[n];
cout<<"Enter how many elements: ";
cin>>n;
for(int i=0;i<n;i++)
cin>>arr[i];
for(int i=0;i<n;i++)
res=res*arr[i];
cout<<res;
}
Question 2]
#include <iostream>
#include <stdlib.h>
using namespace std;
class Stack {
private:
static const int max = 100;
int arr[max];
int top;
public:
Stack() { top = -1; }
bool isEmpty();
bool isFull();
int pop();
void push(int x);
};
bool Stack::isEmpty()
{
if (top == -1)
return true;
return false;
}
bool Stack::isFull()
{
if (top == max - 1)
return true;
return false;
}
int Stack::pop()
{
if (isEmpty()) {
cout << "Stack Underflow";
abort();
}
int x = arr[top];
top--;
return x;
}
void Stack::push(int x)
{
if (isFull()) {
cout << "Stack Overflow";
abort();
}
top++;
arr[top] = x;
}
class SpecialStack : public Stack {
Stack min;
public:
int pop();
void push(int x);
int getMin();
};
void SpecialStack::push(int x)
{
if (isEmpty() == true) {
Stack::push(x);
min.push(x);
}
else {
Stack::push(x);
int y = min.pop();
min.push(y);
if (x < y)
min.push(x);
else
min.push(y);
}
}
int SpecialStack::pop()
{
int x = Stack::pop();
min.pop();
return x;
}
int SpecialStack::getMin()
{
int x = min.pop();
min.push(x);
return x;
}
int main()
{
SpecialStack s;
s.push(10);
s.push(20);
s.push(30);
cout << s.getMin() << endl;
s.push(5);
cout << s.getMin();
return 0;
}