In: Computer Science
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
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.
}
}
PlaneQueue.javapublic 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.\n");
}
System.out.println();
}
}
PlaneList.java
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
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);
}
}
Hi, I have answered this question before. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
Note: Only the modified files PlaneQueue.java and AirTrafficControl.java are attached, other files are not modified.
// PlaneQueue.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 void push(PlaneNode node) {
// appending to planeList, incrementing size
planeList.append(node);
length++;
}
public PlaneNode pop() {
// returning null if empty
if (isEmpty()) {
return null;
}
// storing head node
PlaneNode toBeRemoved = planeList.headNode;
// removing head node, decrementing length
planeList.removeAfter(null);
length--;
// removing the next link of node and returning it
toBeRemoved.nextNode = null;
return toBeRemoved;
}
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.\n");
}
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.
arrivingOrLanded = "";
// looping until user enter -1
while (!arrivingOrLanded.equals("-1")) {
// reading command
arrivingOrLanded = scnr.next();
// identifying command
if (arrivingOrLanded.equalsIgnoreCase("arriving")) {
// reading flight code, creating a PlaneNode and pushing to
// queue
flightCode = scnr.next();
curNode = new PlaneNode(flightCode);
planeQueue.push(curNode);
// printing queue
planeQueue.printPlaneQueue();
} else if (arrivingOrLanded.equalsIgnoreCase("landed")) {
// if plane queue is not empty, popping and displaying flight
// code of popped node
if (!planeQueue.isEmpty()) {
foundNode = planeQueue.pop();
System.out.println(foundNode.flightCode + " has landed");
}
//printing queue
planeQueue.printPlaneQueue();
}
}
}
}
/*INPUT*/
arriving AA213
arriving DAL23
arriving UA628
landed
-1
/*OUTPUT*/
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