-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStacksToQueue.cs
More file actions
38 lines (36 loc) · 911 Bytes
/
StacksToQueue.cs
File metadata and controls
38 lines (36 loc) · 911 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _120324
{
public class StacksToQueue<T>
{
private Stack <T> inbox = new Stack <T> ();
private Stack <T> outbox = new Stack <T> ();
public StacksToQueue(){ }
public void AddToQueue(T elem)
{
inbox.Push(elem);
}
public void Dequeue()
{
if (inbox.Count == 0&& outbox.Count==0) {
throw new Exception("The queue is empty");
}
if (outbox.Count == 0)
{
while (inbox.Count > 0) {
T element = inbox.Pop();
outbox.Push(element);
}
outbox.Pop();
}
else
{
outbox.Pop ();
}
}
}
}