Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

10.5 LAB: Air-traffic control (queue using a linked list) Given a partial main() and PlaneQueue class, write the push() and pop() methods for PlaneQueue. Then

10.5 LAB: Air-traffic control (queue using a linked list)

Given a partial main() and PlaneQueue class, write the push() and pop() methods for PlaneQueue. Then complete the main() 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:

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

Plane.Queue.java

public class PlaneQueue { private PlaneList planeList; // Queue implemented using linked list int length; public PlaneQueue() { planeList = new PlaneList(); length = 0; } // TODO: Write push() and pop() methods. push() adds an item to // the queue and increases the length by 1. pop() removes and // returns the first item in the queue and decreases the length by 1. public boolean isEmpty() { if (length == 0) { return true; } return false; } public int getLength() { return length; } public void printPlaneQueue() { PlaneNode curNode; curNode = planeList.headNode; System.out.println("Air-traffic control queue"); if (!isEmpty()) { System.out.print(" Next to land: "); curNode.printNodeData(); System.out.println(); if (length > 1) { System.out.println(" Arriving flights: "); curNode = curNode.nextNode; while (curNode != null) { System.out.print(" "); curNode.printNodeData(); System.out.println(); curNode = curNode.nextNode; } } } else { System.out.println("Queue is empty. "); } System.out.println(); } }

AirTrafficControl.java

import java.util.Scanner;

public class AirTrafficControl { public static void main (String[] args) { Scanner scnr = new Scanner(System.in); PlaneQueue planeQueue = new PlaneQueue(); PlaneNode curNode; PlaneNode foundNode; String arrivingOrLanded; String flightCode; // TODO: Complete main to 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. } }

PlaneList.java //read-only file

public class PlaneList { // Linked list nodes public PlaneNode headNode; public PlaneNode tailNode; public PlaneList() { // Front of nodes list headNode = null; tailNode = null; } // append public void append(PlaneNode newNode) { if (headNode == null) { // List empty headNode = newNode; tailNode = newNode; } else { tailNode.nextNode = newNode; tailNode = newNode; } } // prepend public void prepend(PlaneNode newNode) { if (headNode == null) { // list empty headNode = newNode; tailNode = newNode; } else { newNode.nextNode = headNode; headNode = newNode; } } // insertAfter public void insertAfter(PlaneNode curNode, PlaneNode newNode) { if (headNode == null) { // List empty headNode = newNode; tailNode = newNode; } else if (curNode == tailNode) { // Insert after tail tailNode.nextNode = newNode; tailNode = newNode; } else { newNode.nextNode = curNode.nextNode; curNode.nextNode = newNode; } } // removeAfter public void removeAfter(PlaneNode curNode) { PlaneNode sucNode; // Special case, remove head if (curNode == null && headNode != null) { sucNode = headNode.nextNode; headNode = sucNode; if (sucNode == null) { // Removed last item tailNode = null; } } else if (curNode.nextNode != null) { sucNode = curNode.nextNode.nextNode; curNode.nextNode = sucNode; if (sucNode == null) { // Removed tail tailNode = curNode; } } } // search public PlaneNode search(String key) { PlaneNode curNode; int position = 1; curNode = headNode; while (curNode != null) { curNode.nodePos = position; if (curNode.flightCode.equals(key)) { return curNode; } curNode = curNode.nextNode; ++position; } return null; } public void printPlaneList() { PlaneNode curNode; curNode = headNode; while (curNode != null) { curNode.printNodeData(); System.out.println(); curNode = curNode.nextNode; } } }

PlaneNode.java // read-only file

public class PlaneNode { public String flightCode; public PlaneNode nextNode; // Reference to the next node public int nodePos;

public PlaneNode() { flightCode = "0"; nextNode = null; }

// Constructor public PlaneNode(String initFlightCode) { this.flightCode = initFlightCode; this.nextNode = null; }

// Constructor public PlaneNode(String initFlightCode, PlaneNode newNextNode) { this.flightCode = initFlightCode; this.nextNode = newNextNode; } public void printNodeData() { System.out.print(this.flightCode); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions