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

I am trying to make a linked list queue and I am trying to use the...
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 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 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() { 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);...
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...
Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...
Improve the speed of public T get(int i) by having it work backward from the end of the array if you attempt to get a member which is closer to the end than the start. This improvement will be tested through timing tests on large lists. The method should still return null if i is not a valid index. Code import java.util.Iterator; public class DLList<T> implements Iterable<T> { private static class Node<T> { public Node<T> prev, next;...
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 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...
Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(), peek(), size(), isEmpty(). my linkedlist class is: public class Lab8_problem1 { private Node head, tail; private int size; public Lab8_problem1() { this.head = null; this.tail = null; this.size = 0; } //Insert a node at specifc Location public void insertAt(int value, int index) { Node newNode = new Node(); newNode.data = value; if(head == null){ head = newNode; tail = newNode; head.next...