-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImplementQUEUEusingStack.cs
57 lines (45 loc) · 1.1 KB
/
ImplementQUEUEusingStack.cs
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
using System;
using System.Collections;
namespace QueueUsingStack
{
public class Queue
{
private readonly Stack _s1 = new Stack();
private readonly Stack _s2 = new Stack();
public void EnQueue(int x)
{
while (_s1.Count > 0)
{
_s2.Push(_s1.Pop());
}
_s1.Push(x);
while (_s2.Count > 0)
{
_s1.Push(_s2.Pop());
}
}
public int DeQueue()
{
if (_s1.Count == 0)
{
Console.WriteLine("Q is empty");
}
var x = (int) _s1.Peek();
_s1.Pop();
return x;
}
}
internal static class Program
{
public static void Main(string[] args)
{
var q1 = new System.Collections.Queue();
q1.Enqueue(1);
q1.Enqueue(2);
q1.Enqueue(3);
Console.WriteLine(q1.Dequeue() + "");
Console.WriteLine(q1.Dequeue() + "");
Console.WriteLine(q1.Dequeue());
}
}
}