Question

In: Computer Science

10.5 LAB: Air-traffic control (queue using a linked list) Given a partial main() and PlaneQueue class,...

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);
}
}

Solutions

Expert Solution

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


Related Solutions

9.7 LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class,...
9.7 LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete the IntList class (a linked list of IntNodes) by writing the insertInDescendingOrder() method to insert new IntNodes into the IntList in descending order. Ex. If the input is: 3 4 2 5 1 6 7 9 8 -1 the output is: 9 8 7 6 5 4 3 2 1 Sortedlist.java import java.util.Scanner; public class SortedList { public static void main (String[] args)...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
Objectives: Define the new class type: Queue using a singly linked list. Define the new class...
Objectives: Define the new class type: Queue using a singly linked list. Define the new class type: Jukebox which creates three objects of type Queue class. Practice enqueue-ing and dequeue-ing elements from the top of your singly linked list Queue class. Test the implementation of the class: MyTunes. The class files are here: https://drive.google.com/file/d/1yCCQeZCS-uLoL_CK0Et9dX-KCaokXQxR/view?usp=sharing class MyTunes Creates an object of type MyTunes class that partially simulate the digital jukebox TouchTunes, using a queue which holds playlist. Tests the implementation of...
Python: Solve following problems using Linked List Data Structure 2. Create a Queue class. In the...
Python: Solve following problems using Linked List Data Structure 2. Create a Queue class. In the queue class create enqueue, dequeue, first, empty, len and resize methods. The class should support circular queue and have the ability to resize the queue.
9.8 LAB: Finding the first and last occurrence of a value (doubly-linked list) Given main() and...
9.8 LAB: Finding the first and last occurrence of a value (doubly-linked list) Given main() and a PeopleNode class, complete the PeopleList class by writing findFirst() and findLast() methods. The findFirst() method should find the first occurrence of an age value in the linked list and return the corresponding node. Similarly, the findLast() method should find the last occurrence of the age value in the linked list and return the corresponding node. For both methods, if the age value is...
In short, you’re going to implement a linked-list class for storing integers, using a provided main...
In short, you’re going to implement a linked-list class for storing integers, using a provided main program to help you interact and test your work. You’ll want to build the linked-list class function by function, working in “Develop” mode to test out each function you write. main.cpp is a read only file linkedlist.h is the file to work on. main.cpp #include #include #include "linkedlist.h" using namespace std; int main() { linkedlist LL; string cmd; int value, key; // // user...
Write a template class that implements an extended queue (use singly Linked List) in c++ please...
Write a template class that implements an extended queue (use singly Linked List) in c++ please create 3 classes please create 3 classes please create 3 classes please create 3 classes please create 3 classes Ex: ExtendedQueue int_queue; ExtendedQueue double_queue; ExtendedQueue char_queue; –Write a program to test this template class. you have to use inheritance so you will create 3 classes : so you will create 3 classes : so you will create 3 classes : so you will create...
In C++ In this lab we will be creating a stack class and a queue class,...
In C++ In this lab we will be creating a stack class and a queue class, both with a hybrid method combining linked list and arrays in addition to the Stack methods(push, pop, peek, isEmpty, size, print) and Queue methods (enqueue, deque, peek, isEmpty, size, print). DO NOT USE ANY LIBRARY, implement each method from scratch. Both the Stack and Queue classes should be generic classes. Don't forget to comment your code.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT