-
Notifications
You must be signed in to change notification settings - Fork 4
/
Assignment on Queue of Numbers & Characters - Level 2 .py
91 lines (77 loc) · 2.3 KB
/
Assignment on Queue of Numbers & Characters - Level 2 .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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#lex_auth_0127438930044108801627
class Queue:
def __init__(self,max_size):
self.__max_size=max_size
self.__elements=[None]*self.__max_size
self.__rear=-1
self.__front=0
def is_full(self):
if(self.__rear==self.__max_size-1):
return True
return False
def is_empty(self):
if(self.__front>self.__rear):
return True
return False
def enqueue(self,data):
if(self.is_full()):
print("Queue is full!!!")
else:
self.__rear+=1
self.__elements[self.__rear]=data
def dequeue(self):
if(self.is_empty()):
print("Queue is empty!!!")
else:
data=self.__elements[self.__front]
self.__front+=1
return data
def display(self):
for index in range(self.__front, self.__rear+1):
print(self.__elements[index])
def get_max_size(self):
return self.__max_size
#You can use the below __str__() to print the elements of the DS object while debugging
def __str__(self):
msg=[]
index=self.__front
while(index<=self.__rear):
msg.append((str)(self.__elements[index]))
index+=1
msg=" ".join(msg)
msg="Queue data(Front to Rear): "+msg
return msg
def merge_queue(queue1,queue2):
#write your logic here
merged_queue=Queue(queue1.get_max_size()+queue2.get_max_size())
i=0
while(not queue1.is_empty() and not queue2.is_empty()):
if(i%2==0):
data=queue1.dequeue()
merged_queue.enqueue(data)
else:
data=queue2.dequeue()
merged_queue.enqueue(data)
i+=1
while(not queue1.is_empty()):
data=queue1.dequeue()
merged_queue.enqueue(data)
while(not queue2.is_empty()):
data=queue2.dequeue()
merged_queue.enqueue(data)
return merged_queue
#Enqueue different values to both the queues and test your program
queue1=Queue(3)
queue2=Queue(6)
queue1.enqueue(3)
queue1.enqueue(6)
queue1.enqueue(8)
queue2.enqueue('b')
queue2.enqueue('y')
queue2.enqueue('u')
queue2.enqueue('t')
queue2.enqueue('r')
queue2.enqueue('o')
merged_queue=merge_queue(queue1, queue2)
print("The elements in the merged queue are:")
merged_queue.display()