218k views
4 votes
Given a partial main.py and PlaneQueue class in PlaneQueue.py, write the push() and pop() instance 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.

Click the orange triangle next to "Current file:" at the top of the editing window to view or edit the other files.

Note: Do not edit any existing code in the files. Type your code in the TODO sections of the files only. Modifying any existing code may result in failing the auto-graded tests.

Important Coding Guidelines:

Use comments, and whitespaces around operators and assignments.
Use line breaks and indent your code.
Use naming conventions for variables, functions, methods, and more. This makes it easier to understand the code.
Write simple code and do not over complicate the logic. Code exhibits simplicity when it’s well organized, logically minimal, and easily readable.
Ex: If the input is:

arriving AA213
arriving DAL23
arriving UA628
landed
-1
the output is:

Air-traffic control queue
Next to land: AA213

Air-traffic control queue
Next to land: AA213
Arriving flights:
DAL23

Air-traffic control queue
Next to land: AA213
Arriving flights:
DAL23
UA628

AA213 has landed.
Air-traffic control queue
Next to land: DAL23
Arriving flights:
UA628
CODE:
from PlaneQueue import PlaneQueue
from PlaneNode import PlaneNode

if __name__ == "__main__":
plane_queue = PlaneQueue()

User Iainmcgin
by
8.0k points

1 Answer

3 votes

class PlaneQueue:

def __init__(self):

self.head = None

self.tail = None

def push(self, plane_node):

"""Pushes a plane node onto the queue.

Args:

plane_node: The plane node to push.

Returns:

None.

"""

# If the queue is empty, set the new node as the head and tail.

if self.head is None:

self.head = plane_node

self.tail = plane_node

return

# Set the new node as the tail and the previous tail's next node.

self.tail.next = plane_node

self.tail = plane_node

def pop(self):

"""Pops a plane node from the front of the queue.

Returns:

The plane node that was popped.

"""

# If the queue is empty, return None.

if self.head is None:

return None

# Get the plane node at the front of the queue.

plane_node = self.head

# Set the head to the next node.

self.head = self.head.next

# If the head is now None, set the tail to None as well.

if self.head is None:

self.tail = None

return plane_node

User Sahil Shekhawat
by
8.4k points