Question

I am trying to make a linked list queue and I am trying to use the...

I am trying to make a linked list queue and I am trying to use the display method I made just to see if its working but when I run it nothing is displayed please help. Also the newPlane boolean was made just so I can randomly decide if the plane is going to join the queue or not.

public class PlaneSimulation {

    public static void main(String[] args) {
        int landTime = 2;
        int takeoffTime = 3;
        int avgArrivalInterval = 3;
        int avgDepartInterval = 3;
        int totalTime = 65;
//regular queue takeOffQueue.insert()
        LinkedList.Queue takeOffQueue = new LinkedList.Queue();
        for (int i = 0; i < totalTime; i++) {
            if (i % 3 == 0) {
                if (newPlane())
                    takeOffQueue.insert();
            }
        }
        takeOffQueue.display();
    }

    public static boolean newPlane() {
        if (Math.random() < 1 / 2)
            return true;
        else {
            return false;
        }

    }

    static class Node {
        int data;
        Node next;

        public Node(int key) {
            data = key;
            next = null;
        }

        public void displayNode() {
            System.out.println("item" + data);
        }
    }

    static class LinkedList {
        Node first;

        public LinkedList() {
            first = null;
        }

        public void insert(int z) {
            Node newNode = new Node(z);

            if (first == null) {
                first = newNode;
                return;
            }
            Node tempNode = first;
            while (tempNode.next != null) {
                tempNode = tempNode.next;
            }
            tempNode.next = newNode;
        }

        public Node remove() {
            Node temp = first;
            first = first.next;
            return temp;
        }

        public void display() {
            Node tempNode = first;
            while (tempNode != null) {
                tempNode.displayNode();
                tempNode = tempNode.next;
            }
            System.out.println();
        }

        static class Queue {
            LinkedList list = new LinkedList();
            private int z;

            public void insert() {
                list.insert(z);
            }

            public void remove() {
                list.remove();
            }

            public void display() {
                list.display();

            }

        }
    }}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

In your code, the newPlane() always returns false, because it is false, the i values are not getting inserted. I have made a few chages to the code. Below is the modified code.

PlaneSimulation.java:

public class PlaneSimulation {

public static void main(String[] args) {
int landTime = 2;
int takeoffTime = 3;
int avgArrivalInterval = 3;
int avgDepartInterval = 3;
int totalTime = 65;
//regular queue takeOffQueue.insert()
LinkedList.Queue takeOffQueue = new LinkedList.Queue();
for (int i = 0; i < totalTime; i++) {
if (i % 3 == 0) {
               boolean flag = newPlane();
if(!flag) {
takeOffQueue.insert(i);
               }
}
}
takeOffQueue.display();
}

public static boolean newPlane() {
       double a = Math.random();
if (a < 1 / 2) {
System.out.println(a);
return true;
       }
else {
return false;
}

}

static class Node {
int data;
Node next;

public Node(int key) {
data = key;
next = null;
}

public void displayNode() {
System.out.println("Item: " + data);
}
}

static class LinkedList {
Node first;

public LinkedList() {
first = null;
}

public void insert(int z) {
Node newNode = new Node(z);

if (first == null) {
first = newNode;
return;
}
Node tempNode = first;
while (tempNode.next != null) {
tempNode = tempNode.next;
}
tempNode.next = newNode;
}

public Node remove() {
Node temp = first;
first = first.next;
return temp;
}

public void display() {
Node tempNode = first;
while (tempNode != null) {
tempNode.displayNode();
tempNode = tempNode.next;
}
System.out.println();
}

static class Queue {
LinkedList list = new LinkedList();

public void insert(int z) {
list.insert(z);
}

public void remove() {
list.remove();
}

public void display() {
list.display();

}

}
}}

output:

Eile Edit Search View Document Project Bid Iools Help New Open Save Save AllRevert CloseBack Forward Compile Build Execute Co

Add a comment
Know the answer?
Add Answer to:
I am trying to make a linked list queue and I am trying to use the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how...

    Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how to get size. I'm not sure. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null; prev = null; data = 0; } /* Constructor */ public Node(int d, Node n, Node p) { data = d; next = n; prev = p; } /* Function...

  • Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time...

    Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...

  • iImplement a Singly Linked List detectLoop in Java, it would check whether the linked list contains...

    iImplement a Singly Linked List detectLoop in Java, it would check whether the linked list contains a loop. Print true if yes, false if not. Test by using the following code: LL<Integer> L = new LL<>(); for (int i = 1000; i > 0; i-=3) sl.add(i); try { L.insert(122, L.getNode(70), L.getNode(21)); if (L.detectLoop()) System.out.println("True"); else System.out.println("False."); } catch(Exception e){ e.printStackTrace(); } class Linkedlist<E>{ private static class Node<E>{ private E element; private Node<E> next; public Node(E e, Node<E> n){ element =...

  • I need help with todo line please public class LinkedList { private Node head; public LinkedList()...

    I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...

  • This Individual Assignment is a set of three problems. The first is a recursion "warm up"...

    This Individual Assignment is a set of three problems. The first is a recursion "warm up" exercise, and the second two are QuickSort variations. The "warm up" should be implemented as a static method in your main App class and the second two will use additional classes (as instructed below). All three problems should be included in the same NetBeans project (exported to ZIP as usual). ----------------- All of your classes should be properly encapsulated, follow all proper coding conventions...

  • // 1. Add methods to get and set, Data and Link. Data should be any Comparable...

    // 1. Add methods to get and set, Data and Link. Data should be any Comparable object. class Node { Integer data; // Integer is Comparable Node link; public Node(Integer data, Node link) { this.data = data; this.link = link; } public Integer getData() { return data; } public void setData(Integer data) { this.data = data; } public Node getLink() { return link; } public void setLink(Node link) { this.link = link; } } // b. Create MyLinkedList class and...

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

  • Java - I need help creating a method that removes a node at the specific index...

    Java - I need help creating a method that removes a node at the specific index position. The * first node is index 0. public boolean delAt(int index) { src code 2 different classes ******************************************** public class Node { private String data; private Node next; public Node(String data, Node next) { this.data = data; this.next = next; } public Node() { } public String getData() { return data; } public void setData(String data) { this.data = data; } public void...

  • 3. (Gaddis Exercises 20.4) Tree Height Write a recursive member function for the BinaryTree class that...

    3. (Gaddis Exercises 20.4) Tree Height Write a recursive member function for the BinaryTree class that returns the height of the tree. The height of the tree is the number of levels it contains. Demonstrate the function in a driver program. CPP FILE CODE: #include "BinaryTree.h" #include <iostream> using namespace std; BinaryTree::BinaryTree() { root = NULL; } BinaryTree::~BinaryTree() { destroy(root); } bool BinaryTree::search(int data) { return search(data, root); } void BinaryTree::insert(int data) { insert(data, root); } void BinaryTree::traverseInOrder() { traverseInOrder(root);...

  • Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked list and returns the new linked list of unique elements (The order does not ma...

    Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked list and returns the new linked list of unique elements (The order does not matter). Example: Input: 1-»2->3->1-2 Output: 1->2->3 public class Node f int iterm Node next; Node(int d) t item = d; next-null; ) import java.util.ArrayList; public class ExtraLab public static void main (String[] args)t PROBLEM 1 System.out.println("PROBLEM 1"); Node head new Node(1); head.next-new Node (2); head.next.next-new Node(3);...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT