Question: Why is this not running? what am i doing wrong? public class DoublyLinkedList { private static class Node { private E element; // the element
Why is this not running? what am i doing wrong? public class DoublyLinkedList { private static class Node { private E element; // the element stored at ths node/cell private DoublyLinkedList.Node next; // reference to next node/cell //Constructor public Node(E e, DoublyLinkedList.Node n) { element = e; next = n; } // Access methods public E getElement() { return element; } public DoublyLinkedList.Node getNext() { return next; } // Update method public void setNext(DoublyLinkedList.Node n) { next = n; } } // end of nested Node class // Instance variable for DoublyLinkedList private DoublyLinkedList.Node head = null; // Head node of list (or null if empty) private DoublyLinkedList.Node tail = null; // tail node of list (or null if empty) private int size = 0; // number of node in the list //Constructor public DoublyLinkedList() {} // construct an initially empty list //Access methods public int size() { return size; } public boolean isEmpty() { return size==0; } public E first() { //return element of first node (head) if (isEmpty()) return null; return head.getElement(); } public E last() { // return element of last node (tail) if (isEmpty()) return null; return tail.getElement(); } // Update methods public void addFirst(E e) { // add element e to the front of the list head = new DoublyLinkedList.Node<>(e, head); if (size==0) { tail = head; // special case of just one node } size++; } public void addLast(E e) { // add element e to the end of the list DoublyLinkedList.Node newest = new DoublyLinkedList.Node<>(e, null); //our new tail if (isEmpty()) { head = newest; // special case of just one mode } else { tail.setNext(newest); // point old tail to new tail } tail = newest; // new mode becomes tail size++; } public E removeFirst() { // removes and returns the first element if (isEmpty()) return null; E answer = head.getElement(); head = head.getNext(); size--; if (size==0) { tail = null; // special case of one mode (before removing first) } return answer; } public String toString() { String out = "Singly Linked List: "; out += "0 " + head.getElement() + " "; DoublyLinkedList.Node nextnode; nextnode = head.getNext(); for (int i=1; i names = new DoublyLinkedList<>(); System.out.println(names.size()); names.addLast( "Leslie"); System.out.println(names.size()); names.addLast("Eric"); names.addLast("Emily"); System.out.println(names); names.addFirst("Joseph"); System.out.println(names); names.removeFirst("Eric"); System.out.println(names); } }
2. Write a Java program to simulate an ecosystem containing two types of creatures, bears and fish . The ecosystem consists of a river, which is modeled as a relatively large array (i.e. 500 cells). Each cell of the array should contain an Animal object, which can be a bear, a fish, or null . Initialize the array to randomly contain these three types. In each time step, based on a random process, each bear/fish either attempts to move into an adjacent array cell or stay where it is. If two animals of the same type are about to collide in the same cell, then they stay where they are, but they create a new instance of that type of animal, which is placed in an empty (i.e. previously null ) cell in the array. If a bear and a fish collide, then the fish dies (i.e., it disappears). Provide summary information on the array after each time step (time step number and how many bears, fish, and null ). Run the simulation until the ecosystem contains all bears.
Re-write your river of bears and fish from Homework 2, using the new DoublyLinkedList class (instead of an array).
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
