-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplementQueueUsingStacks.cs
More file actions
48 lines (38 loc) · 922 Bytes
/
Copy pathImplementQueueUsingStacks.cs
File metadata and controls
48 lines (38 loc) · 922 Bytes
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
namespace Solutions.Problems;
public class MyQueue
{
#region Constructors
#endregion
#region Fields
private Stack<int> stack1 = new();
private Stack<int> stack2 = new();
#endregion
#region Properties
#endregion
#region Events
#endregion
#region Methods
public void Push(int x)
{
stack1.Push(x);
}
public int Pop()
{
while (stack1.Count > 1)
stack2.Push(stack1.Pop());
int result = stack1.Pop();
(stack2, stack1) = (stack1, new Stack<int>(stack2));
return result;
}
public int Peek()
{
while (stack1.Count > 1)
stack2.Push(stack1.Pop());
int result = stack1.Peek();
stack2.Push(stack1.Pop());
(stack2, stack1) = (stack1, new Stack<int>(stack2));
return result;
}
public bool Empty() => stack1.Count == 0;
#endregion
}