-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueues-a-table-of-two-stacks.py
54 lines (42 loc) · 1.94 KB
/
queues-a-table-of-two-stacks.py
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
#https://www.hackerrank.com/challenges/ctci-queue-using-two-stacks/problem
# A queue is an abstract data type that maintains the order in which elements were added to it,
# allowing the oldest elements to be removed from the front and new elements to be added to the rear.
# This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue
# (i.e., the one that has been waiting the longest) is always the first one to be removed.
# A basic queue has the following operations:
# Enqueue: add a new element to the end of the queue.
# Dequeue: remove the element from the front of the queue and return it.
# In this challenge, you must first implement a queue using two stacks.
# Then process q queries, where each query is one of the following 3 types:
# 1. x: Enqueue element into the end of the queue.
# 2. Dequeue the element at the front of the queue.
# 3. Print the element at the front of the queue.
# Input Format
# The first line contains a single integer, q , denoting the number of queries.
# Each line i of the q subsequent lines contains a single query in the form described in the problem statement above.
# All three queries start with an integer denoting the query type, but only query 1 is followed by
# an additional space-separated value, x, denoting the value to be enqueued.
# Output Format
# For each query of type 3, print the value of the element at the front of the queue on a new line.
class MyQueue(object):
def __init__(self):
self.data = []
def peek(self):
return self.data[0]
def pop(self):
value = self.data[0]
del self.data[0]
return value
def put(self, value):
self.data.append(value)
queue = MyQueue()
t = int(input())
for line in range(t):
values = map(int, input().split())
values = list(values)
if values[0] == 1:
queue.put(values[1])
elif values[0] == 2:
queue.pop()
else:
print(queue.peek())