Given a partial main.py and PlaneQueue class in PlaneQueue.py, write the push() and pop() methods for PlaneQueue. Then complete main.py to read in whether flights are arriving or have landed at an airport. An "arriving" flight is pushed onto the queue. A "landed" flight is popped from the front of the queue. Output the queue after each plane is pushed or popped. Entering -1 exits the program. Ex: If the input is: arriving AA213 arriving DAL23 arriving UA628 landed -1 the output is: (see image attached) main.py from PlaneQueue import PlaneQueue from PlaneNode import PlaneNode if __name__ == "__main__": plane_queue = PlaneQueue() # TODO: Read in arriving flight codes and whether a flight has landed. # Print the queue after every push() or pop() operation. If the user # entered "landed", print which flight has landed. Continue until -1 # is read. PlaneNode.py class PlaneNode: def __init__(self, flight_code='0'): self.flight_code = flight_code self.next = None def print_node_data(self): print(self.flight_code, end='') PlaneList.py class PlaneList: def __init__(self): self.head = None self.tail = None def append(self, new_node): if self.head == None: self.head = new_node self.tail = new_node else: self.tail.next = new_node self.tail = new_node def prepend(self, new_node): if self.head == None: self.head = new_node self.tail = new_node else: new_node.next = self.head self.head = new_node def insert_after(self, current_node, new_node): if self.head == None: self.head = new_node self.tail = new_node elif current_node is self.tail: self.tail.next = new_node self.tail = new_node else: new_node.next = current_node.next current_node.next = new_node def remove_after(self, current_node): # Special case, remove head if (current_node == None) and (self.head != None): succeeding_node = self.head.next self.head = succeeding_node if succeeding_node == None: # Remove last item self.tail = None elif current_node.next != None: succeeding_node = current_node.next.next current_node.next = succeeding_node if succeeding_node == None: # Remove tail self.tail = current_node def search(self, key): position = 1 cur_node = self.head while cur_node != None: if cur_node.data == key: cur_node.node_pos = position return cur_node cur_node = cur_node.next position += 1 def print_list(self): cur_node = self.head while cur_node != None: cur_node.print_node_data() print() cur_node = cur_node.next PlanbeQueue.py from PlaneList import PlaneList class PlaneQueue: def __init__(self): self.plane_list = PlaneList() self.length = 0 # TODO: Write push() and pop() methods. push() adds an item to the queue # and adds 1 to length. pop() removes and returns the first item in # the queue and subtracts 1 from length. def is_empty(self): return self.length == 0 def print_queue(self): print('Air-traffic control queue') if not self.is_empty(): print(' Next to land:', end=' ') cur_node = self.plane_list.head cur_node.print_node_data() print() if self.length > 1: print(' Arriving flights:') cur_node = cur_node.next while cur_node is not None: print(' ', end='') cur_node.print_node_data() print() cur_node = cur_node.next else: print('Queue is empty.') print()
Given a partial main.py and PlaneQueue class in PlaneQueue.py, write the push() and pop() methods for PlaneQueue. Then complete main.py to read in whether flights are arriving or have landed at an airport.
- An "arriving" flight is pushed onto the queue.
- A "landed" flight is popped from the front of the queue.
Output the queue after each plane is pushed or popped. Entering -1 exits the program.
Ex: If the input is:
arriving AA213
arriving DAL23
arriving UA628
landed
-1
the output is: (see image attached)
main.py
from PlaneQueue import PlaneQueue
from PlaneNode import PlaneNode
if __name__ == "__main__":
plane_queue = PlaneQueue()
# TODO: Read in arriving flight codes and whether a flight has landed.
# Print the queue after every push() or pop() operation. If the user
# entered "landed", print which flight has landed. Continue until -1
# is read.
PlaneNode.py
class PlaneNode:
def __init__(self, flight_code='0'):
self.flight_code = flight_code
self.next = None
def print_node_data(self):
print(self.flight_code, end='')
PlaneList.py
class PlaneList:
def __init__(self):
self.head = None
self.tail = None
def append(self, new_node):
if self.head == None:
self.head = new_node
self.tail = new_node
else:
self.tail.next = new_node
self.tail = new_node
def prepend(self, new_node):
if self.head == None:
self.head = new_node
self.tail = new_node
else:
new_node.next = self.head
self.head = new_node
def insert_after(self, current_node, new_node):
if self.head == None:
self.head = new_node
self.tail = new_node
elif current_node is self.tail:
self.tail.next = new_node
self.tail = new_node
else:
new_node.next = current_node.next
current_node.next = new_node
def remove_after(self, current_node):
# Special case, remove head
if (current_node == None) and (self.head != None):
succeeding_node = self.head.next
self.head = succeeding_node
if succeeding_node == None: # Remove last item
self.tail = None
elif current_node.next != None:
succeeding_node = current_node.next.next
current_node.next = succeeding_node
if succeeding_node == None: # Remove tail
self.tail = current_node
def search(self, key):
position = 1
cur_node = self.head
while cur_node != None:
if cur_node.data == key:
cur_node.node_pos = position
return cur_node
cur_node = cur_node.next
position += 1
def print_list(self):
cur_node = self.head
while cur_node != None:
cur_node.print_node_data()
print()
cur_node = cur_node.next
PlanbeQueue.py
from PlaneList import PlaneList
class PlaneQueue:
def __init__(self):
self.plane_list = PlaneList()
self.length = 0
# TODO: Write push() and pop() methods. push() adds an item to the queue
# and adds 1 to length. pop() removes and returns the first item in
# the queue and subtracts 1 from length.
def is_empty(self):
return self.length == 0
def print_queue(self):
print('Air-traffic control queue')
if not self.is_empty():
print(' Next to land:', end=' ')
cur_node = self.plane_list.head
cur_node.print_node_data()
print()
if self.length > 1:
print(' Arriving flights:')
cur_node = cur_node.next
while cur_node is not None:
print(' ', end='')
cur_node.print_node_data()
print()
cur_node = cur_node.next
else:
print('Queue is empty.')
print()
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 4 images