Question

In: Computer Science

Can you fix this code please. the removing methods id no doing anything. this is java...

Can you fix this code please. the removing methods id no doing anything. this is java code

import java.util.NoSuchElementException;

public class DoublyLinkedList<E> {

   public int size;
   public Node head;
   public Node tail;
  
  
  
   @Override
   public boolean isEmpty() {
      
       return size == 0;
   }

   @Override
   public int getSize() {
      
       return 0;
   }

   @Override
   public void addAtFront(E element) {
      
Node<E> temp = new Node<E>(element, head, null);
      
       if(head != null) {
          
           head.prev = temp;
          
       }
      
       head = temp;
      
       if (tail == null) {
          
           tail = temp;
       }
      
       size++;
   }
      
      
  

   @Override
   public void addAtBack(E element) {
      
       Node temp = new Node(element);
      
       Node prev ;
      
       if(isEmpty()) {
          
           head = temp;
       }
      
       else {
          
           tail.next = temp;
          
           temp.prev = tail;
       }
      
       tail = temp;
      
       size++;
   }

   @Override
   public void addAtLocation(E newNode, int location) {
      
Node node = new Node(newNode);
      
       node.data = newNode;
      
       Node previous = head;
      
      
       int counter = 1;
      
       while(counter < location -1) {
          
          
           previous = previous.next;
          
           counter++;
       }
      
      
       Node current = previous.next;
      
       node.next = current;
       previous.next = node;
      
       node.prev = previous;
      
       if(current != null) {
          
           current.prev = node;
       }
  
       size++;
   }

   @Override
   public void removeFromFront() {
      
       if(isEmpty()) {
          
           throw new NoSuchElementException();
       }
      
       Node temp = head;
       if(head == tail) {
          
           tail = null;
       }
       else
       {
           head.next.prev = null;
       }
      
      
       head = head.next;
       temp.next = null;
      
      
   }

   @Override
   public void removeFromBack() {
      
if (tail == null) {
          
           System.out.println("is emty");
       }
      
      
      
       else
          
       {
          
       Node temp = tail.prev;
      
       tail.next = null;
       tail = temp;
      
      
       }
   }
      
      
  

   @Override
   public void removeFromLocation(int location) {
      
       Node previous = head;
       int counter = 1;
      
       System.out.print("\nthe deleted element in any location is: ");
      
       while(counter < location -1) {
          
           previous = previous.next;
          
           counter++;
          
          
       }
      
           Node current = previous.next;
          
           previous.next = current.next;
           System.out.println(current.data);
           current.next = null;
          
          
      
      
   }

   @Override
   public void printForward() {
      
if (tail == null) {
          
           System.out.println("is emty");
       }

else {
      
       Node temp = head;
      
       System.out.print("\nthis is traverse Forward: ");
      
       while (temp != null){
          
           System.out.print(" " + temp.element);
          
           temp = temp.next;
       }
      
   }
  
   }
      
      
  

   @Override
   public void printBackward() {
      
if (tail == null) {
          
           System.out.println("is emty");
       }
else
{
      
       Node node = tail;
      
       System.out.print("\nthis is traverse Backward: ");
      
       while (node != null) {
          
           System.out.print(" " + node.element);
          
           node = node.prev;
          
          
          
       }
      
}
      
      
   }

public void show1() {
      
   Node temp = head;
  
   if(tail == null) {
      
       System.out.println("it is empty");
   }
  
   else
      
       System.out.print("\nThis is the Doubly LinkList is: ");
       do {
          
           System.out.print(" " + temp.element);
          
           temp = temp.next;
          
          
          
       }
       while (temp != null);
  
  
  
}
  
}

this is node class


public class Node<E> {
  
  
  
   E element;
   Node next;
E data;
   Node prev;
   public Node previous;
  
public Node(E element, Node next, Node prev) {
      
       this.element = element;
       this.next = next;
       this.prev = prev;
   }

public Node(E element) {
  
   this.element = element;
  


}
  


}

Solutions

Expert Solution

/***********************Node.java*********************/


/**
* The Class Node.
*
* @param <E> the element type
*/
public class Node<E> {

   /** The element. */
   E element;
  
   /** The next. */
   Node<E> next;
  
   /** The data. */
   E data;
  
   /** The prev. */
   Node<E> prev;
  
   /** The previous. */
   public Node<E> previous;

   /**
   * Instantiates a new node.
   *
   * @param element the element
   * @param next the next
   * @param prev the prev
   */
   public Node(E element, Node<E> next, Node<E> prev) {

       this.element = element;
       this.next = next;
       this.prev = prev;
   }

   /**
   * Instantiates a new node.
   *
   * @param element the element
   */
   public Node(E element) {

       this.element = element;

   }

}

/********************************DoublyLinkedList.java**********************/

import java.util.NoSuchElementException;


/**
* The Class DoublyLinkedList.
*
* @param <E> the element type
*/
public class DoublyLinkedList<E> {

   /** The size. */
   public int size;
  
   /** The head. */
   public Node<E> head;
  
   /** The tail. */
   public Node<E> tail;

   /**
   * Checks if is empty.
   *
   * @return true, if is empty
   */
   public boolean isEmpty() {

       return size == 0;
   }

   /**
   * Gets the size.
   *
   * @return the size
   */
   public int getSize() {

       return 0;
   }

   /**
   * Adds the at front.
   *
   * @param element the element
   */
   public void addAtFront(E element) {

       Node<E> temp = new Node<E>(element, head, null);

       if (head != null) {

           head.prev = temp;

       }

       head = temp;

       if (tail == null) {

           tail = temp;
       }

       size++;
   }

   /**
   * Adds the at back.
   *
   * @param element the element
   */
   public void addAtBack(E element) {

       Node<E> temp = new Node<E>(element);

       Node<E> prev;

       if (isEmpty()) {

           head = temp;
       }

       else {

           tail.next = temp;

           temp.prev = tail;
       }

       tail = temp;

       size++;
   }

   /**
   * Adds the at location.
   *
   * @param newNode the new node
   * @param location the location
   */
   public void addAtLocation(E newNode, int location) {

       Node<E> node = new Node<E>(newNode);

       node.data = newNode;

       Node<E> previous = head;

       int counter = 1;

       while (counter < location - 1) {

           previous = previous.next;

           counter++;
       }

       Node<E> current = previous.next;

       node.next = current;
       previous.next = node;

       node.prev = previous;

       if (current != null) {

           current.prev = node;
       }

       size++;
   }

   /**
   * Removes the from front.
   */
   public void removeFromFront() {

       if (isEmpty()) {

           throw new NoSuchElementException();
       }

       Node<E> temp = head;
       if (head == tail) {

           tail = null;
       } else {
           head.next.prev = null;
       }

       head = head.next;
       temp.next = null;

   }

   /**
   * Removes the from back.
   */
   public void removeFromBack() {

       if (tail == null) {

           System.out.println("is emty");
       }

       else

       {

           Node<E> temp = tail.prev;

           tail.next = null;
           tail = temp;

       }
   }

   /**
   * Removes the from location.
   *
   * @param location the location
   */
   public void removeFromLocation(int location) {

       Node<E> previous = head;
       int counter = 1;

       System.out.print("\nthe deleted element in any location is: ");

       while (counter < location - 1) {

           previous = previous.next;

           counter++;

       }

       Node<E> current = previous.next;

       previous.next = current.next;
       System.out.println(current.data);
       current.next = null;

   }

   /**
   * Prints the forward.
   */
   public void printForward() {

       if (tail == null) {

           System.out.println("is emty");
       }

       else {

           Node<E> temp = head;

           System.out.print("\nthis is traverse Forward: ");

           while (temp != null) {

               System.out.print(" " + temp.element);

               temp = temp.next;
           }

       }

   }

   /**
   * Prints the backward.
   */
   public void printBackward() {

       if (tail == null) {

           System.out.println("is emty");
       } else {

           Node<E> node = tail;

           System.out.print("\nthis is traverse Backward: ");

           while (node != null) {

               System.out.print(" " + node.element);

               node = node.prev;

           }

       }

   }

   /**
   * Show 1.
   */
   public void show1() {

       Node<E> temp = head;

       if (tail == null) {

           System.out.println("it is empty");
       }

       else

           System.out.print("\nThis is the Doubly LinkList is: ");
       do {

           System.out.print(" " + temp.element);

           temp = temp.next;

       } while (temp != null);

   }

   /**
   * The main method.
   *
   * @param a the arguments
   */
    public static void main(String a[]){

   DoublyLinkedList<Integer> doublyLinkedList = new DoublyLinkedList<Integer>();
   doublyLinkedList.addAtFront(10);
   doublyLinkedList.addAtFront(34);
   doublyLinkedList.addAtBack(57);
   doublyLinkedList.addAtBack(353);
   doublyLinkedList.printForward();
   doublyLinkedList.removeFromFront();
   doublyLinkedList.removeFromBack();
   doublyLinkedList.printBackward();
   }
}

/*********************output*******************/


this is traverse Forward: 34 10 57 353
this is traverse Backward: 57 10

Please let me know if you have any doubt or modify the answer, Thanks :)


Related Solutions

Can you fix the code and comment the fix Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -39 at...
Can you fix the code and comment the fix Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -39 at CaesarCipher.encrypt(CaesarCipher.java:28) at CaesarCipher.main(CaesarCipher.java:52) public class CaesarCipher{     char[] encoder = new char[52];     char[] decoder = new char[52];      public CaesarCipher(int rotation)      {        for(int k=0 ; k < 26 ; k++)        {            encoder[k] = (char) ('A' + (k + rotation) % 26);            decoder[k] = (char) ('A' + (k - rotation + 26) % 26);        }        for(int j...
Please can you draw a flow chart for the following code : Program code for Payroll,java:...
Please can you draw a flow chart for the following code : Program code for Payroll,java: public class Payroll { public Payroll(String name,int ID,double payRate) { this.name=name; this.ID=ID; this.payRate=payRate; } private String name; private double payRate,hrWorked; private int ID; public Payroll() { name="John Doe"; ID=9999; payRate=15.0; hrWorked=40; } public String getName() { return name; } public int getID() { return ID; } public void setPayRate(int payRate) { this.payRate=payRate; } public void setHrWorked(double hrWorked) { this.hrWorked=hrWorked; } public double getPayRate() {...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog {...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog { String catalog_name; ArrayList<Item> list; Catalog(String cs_Gift_Catalog) { list=new ArrayList<>(); catalog_name=cs_Gift_Catalog; } String getName() { int size() { return list.size(); } Item get(int i) { return list.get(i); } void add(Item item) { list.add(item); } } Thanks!
JAVA CODE PLEASE write a casino membership application. in the casino membership class: data: membership ID...
JAVA CODE PLEASE write a casino membership application. in the casino membership class: data: membership ID points method: add points display points Main: create a membership object (a client) give initial points (600) increase the points by (try both 200 and 500)
Please finish this code (Java) 1. Fill in the compare methods for the CompareByPlayoffsAndSalary and CompareByWinsLossesChamps...
Please finish this code (Java) 1. Fill in the compare methods for the CompareByPlayoffsAndSalary and CompareByWinsLossesChamps classes 2. In the CompareByPlayoffsAndSalary the compare method should list: (a) teams that made the playoffs last year before teams that did not. (b) If this is the same then it should list teams with lower salary first. 3. In the CompareByWinsLossesChamps list: (a) teams with more wins first. (b) If the same then list by teams with fewer losses. (c) If this is...
Can you fix the errors in this code? package demo; /** * * */ import java.util.Scanner;...
Can you fix the errors in this code? package demo; /** * * */ import java.util.Scanner; public class Booolean0p {        public class BooleanOp {            public static void main(String[] args) {                int a = 0, b = 0 , c = 0;                Scanner kbd = new Scanner(System.in);                System.out.print("Input the first number: ");                a = kbd.nextInt();                System.out.print("Input...
Please I can get a flowchart and a pseudocode for this java code. Thank you //import...
Please I can get a flowchart and a pseudocode for this java code. Thank you //import the required classes import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BirthdayReminder {       public static void main(String[] args) throws IOException {        // declare the required variables String sName = null; String names[] = new String[10]; String birthDates[] = new String[10]; int count = 0; boolean flag = false; // to read values from the console BufferedReader dataIn = new BufferedReader(new...
Question: Can I get the code in Java for this assignment to compare? Please and thank you....
Question: Can I get the code in Java for this assignment to compare? Please and thank you. Can I get the code in Java for this assignment to compare? Please and thank you. Description Write a Java program to read data from a text file (file name given on command line), process the text file by performing the following: Print the total number of words in the file. Print the total number of unique words (case sensitive) in the file. Print...
Can you please rewrite this Java code so it can be better understood. import java.util.HashMap; import...
Can you please rewrite this Java code so it can be better understood. import java.util.HashMap; import java.util.Scanner; import java.util.Map.Entry; public class Shopping_cart {       static Scanner s= new Scanner (System.in);    //   declare hashmap in which key is dates as per mentioned and Values class as value which consists all the record of cases    static HashMap<String,Item> map= new HashMap<>();    public static void main(String[] args) {        // TODO Auto-generated method stub        getupdates();    }...
//Complete the incomplete methods in the java code //You will need to create a driver to...
//Complete the incomplete methods in the java code //You will need to create a driver to test this. public class ManagedArray { private int[] managedIntegerArray; //this is the array that we are managing private int maximumSize; //this will hold the size of the array private int currentSize = 0; //this will keep track of what positions in the array have been used private final int DEFAULT_SIZE = 10; //the default size of the array public ManagedArray()//default constructor initializes array to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT