-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathstack_using_queue.cpp
More file actions
122 lines (109 loc) · 2.16 KB
/
stack_using_queue.cpp
File metadata and controls
122 lines (109 loc) · 2.16 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
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
#include<iostream>
#include<queue>
using namespace std;
class stack
{
public:
queue<int> q1;
queue<int> q2;
void push(int val)
{
q1.push(val);
}
void pop()
{
if(q1.empty())
{
cout<<"No elements to pop\n";return;
}
int count=0;
while(!q1.empty())
{
int peak = q1.front();
q2.push(peak);
q1.pop();
count++;
}
if(!q2.empty())
q2.pop();
while(!q2.empty() && count--)
{
int peak = q2.front();
q1.push(peak);
q2.pop();
}
}
int top()
{
if(q1.empty())
{
cout<<"No elements on top\n";return -1;
}
while(!q1.empty())
{
int peak = q1.front();
q2.push(peak);
q1.pop();
}
int top = q2.front();
while(!q2.empty())
{
int peak = q2.front();
q1.push(peak);
q2.pop();
}
return top;
}
bool empty()
{
if(q1.empty())
return 1;
return 0;
}
};
int main()
{
stack s;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.pop();
s.pop();
// s.pop();
// s.pop();
cout<<s.empty()<<'\n';
cout<<s.top()<<'\n';
}
class Solution {
public:
vector<int> sortEvenOdd(vector<int>& nums) {
int n=nums.size();
vector<int>o;
vector<int>e;
for(int i=0;i<n;i+=2)
{
e.push_back(nums[i]);
}
for(int j=1;j<n;j+=2)
{
o.push_back(nums[j]);
}
sort(e.begin(),e.end());
sort(o.begin(),o.end(),greater<>());
vector<int>res(n,0);
int k=0;
for(int i=0;i<n;i+=2)
{
res[i]=e[k];
k++;
}
k=0;
for(int j=1;j<n;j+=2)
{
res[j]=o[k];
k++;
}
return res;
}
};