Skip to content

Commit fd1d112

Browse files
authored
Merge pull request #3671 from NotADucc/0225
create 0225-implement-stack-using-queues.cs
2 parents f5bf87b + d60f8c1 commit fd1d112

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
public class MyStack
2+
{
3+
Queue<int> queue;
4+
5+
public MyStack()
6+
{
7+
queue = new Queue<int>();
8+
}
9+
10+
public void Push(int x)
11+
{
12+
queue.Enqueue(x);
13+
for (int i = 0; i < queue.Count - 1; i++)
14+
{
15+
queue.Enqueue(queue.Dequeue());
16+
}
17+
}
18+
19+
public int Pop()
20+
{
21+
return queue.Dequeue();
22+
}
23+
24+
public int Top()
25+
{
26+
return queue.Peek();
27+
}
28+
29+
public bool Empty()
30+
{
31+
return queue.Count == 0;
32+
}
33+
}

0 commit comments

Comments
 (0)