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 please look at the following code and fix it for me so that it...
can you please look at the following code and fix it for me so that it does not have any syntax errors. also can you tell me what was fixed /** * Driver program to demonstrate calling methods of Client class. * * @author Doyt Perry/Tina Comston * @version Fall 2019 */ public class ClientDemo { public static void main() { /** * main method - makes this an executable program. */ // create a client with placeholder values System.out.println("Client...
Can you fix please? this is adding the given location of doubly linked list in java...
Can you fix please? this is adding the given location of doubly linked list in java but if I make reverse is not adding the new node that I added but is printing forward correctly. just fix to me that part public void addAtLocation(E newNode, int location) {        Node node = new Node(newNode);               node.data = newNode;               Node previous = head;                      int counter = 1;...
I wrote this code and it produces a typeError, so please can you fix it? import...
I wrote this code and it produces a typeError, so please can you fix it? import random def first_to_a_word(): print("###### First to a Word ######") print("Instructions:") print("You will take turns choosing letters one at a time until a word is formed.") print("After each letter is chosen you will have a chance to confirm whether or not a word has been formed.") print("When a word is formed, the player who played the last letter wins!") print("One of you has been chosen...
(java)Fix the code in this program. Fix lines, add lines… Comment each line that you fixed...
(java)Fix the code in this program. Fix lines, add lines… Comment each line that you fixed … what you did to fix it. import java.util.Scanner; public static void main(String[] args) { // This program Converts grade Points into a Letter grade. int gradePoints == 00; // Declare variable and assign initial value System.out.printl("Enter Grade Points: "; //Input gradePoints; if ( gradePoints >= -42 ) { System.out.println("Grade = A"); } // if true, then ends here, if false drop to next...
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...
Can you fix my code and remove the errors in java language. public class LinkedStack<T> implements...
Can you fix my code and remove the errors in java language. public class LinkedStack<T> implements Stack<T> { private Node<T> top; private int numElements = 0; public int size() { return (numElements); } public boolean isEmpty() { return (top == null); } public T top() throws StackException { if (isEmpty()) throw new StackException("Stack is empty."); return top.info; } public T pop() throws StackException { Node<T> temp; if (isEmpty()) throw new StackException("Stack underflow."); temp = top; top = top.getLink(); return temp.getInfo();...
can you please convert this python code into java? Python code is as shown below: #...
can you please convert this python code into java? Python code is as shown below: # recursive function def row_puzzle_rec(row, pos, visited):    # if the element at the current position is 0 we have reached our goal    if row[pos] == 0:        possible = True    else:        # make a copy of the visited array        visited = visited[:]        # if the element at the current position has been already visited then...
PLEASE CODE IN JAVA In this assignment you will write a program in that can figure...
PLEASE CODE IN JAVA In this assignment you will write a program in that can figure out a number chosen by a human user. The human user will think of a number between 1 and 100. The program will make guesses and the user will tell the program to guess higher or lower.                                                                   Requirements The purpose of the assignment is to practice writing functions. Although it would be possible to write the entire program in the main function, your...
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() {...
Java question: I need to fix a point class (code below) Thank you! /** * A...
Java question: I need to fix a point class (code below) Thank you! /** * A point, implemented as a location without a shape. */ public class Point extends Location { // TODO your job // HINT: use a circle with radius 0 as the shape! public Point(final int x, final int y) { super(-1, -1, null); assert x >= 0; assert y >= 0; } }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT