Question

Please answer in Java. The code that needs to be modified is below. Thank you. Question:...

Please answer in Java. The code that needs to be modified is below. Thank you.

Question: Implement Doubly Linked List add method which add an element to a specific position. - It’s an instance method that takes a position and an element, then adds the element to this specific position and shifts the element currently at this position and any subsequent elements to the right. It throws an exception if the position is out of bound. It traverses the list from header if the position is closer to the header and traverses the list from trailer otherwise.

CODE:

class DLinkedList<E>{

private static class DNode<E>{

private E element;

private DNode<E> prev;

private DNode<E> next;

public DNode(E e){

this(e, null, null);

}

public DNode(E e, DNode<E> p, DNode<E> n){

element = e;

prev = p;

next = n;

}

public E getE(){

return element;

}

public DNode<E> getPrev(){

return prev;

}

public DNode<E> getNext(){

return next;

}

public void setE(E e){

element = e;

}

public void setPrev(DNode<E> p){

prev = p;

}

public void setNext(DNode<E> n){

next = n;

}

}

private DNode<E> header;

private DNode<E> trailer;

private int size;

public DLinkedList(){

header = new DNode<E>(null);

trailer = new DNode<E>(null, header, null);

header.setNext(trailer);

size = 0;

}

public void print(){

DNode<E> temp = header.getNext();

while (temp != trailer){

System.out.print(temp.getE().toString() + ", ");

temp = temp.getNext();

}

System.out.println();

}

}

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

class A{
   public static void main(String[] args)throws Exception {
       DLinkedList dl = new DLinkedList();
       dl.add(0,20);
       dl.add(0,25);
       dl.print();
   }
}

class DLinkedList<E>{

   private static class DNode<E>{

       private E element;

       private DNode<E> prev;

       private DNode<E> next;

       public DNode(E e){

       this(e, null, null);

       }

       public DNode(E e, DNode<E> p, DNode<E> n){

       element = e;

       prev = p;

       next = n;

       }

       public E getE(){

       return element;

       }

       public DNode<E> getPrev(){

       return prev;

       }

       public DNode<E> getNext(){

       return next;

       }

       public void setE(E e){

       element = e;

       }

       public void setPrev(DNode<E> p){

       prev = p;

       }

       public void setNext(DNode<E> n){

       next = n;

       }

   }

   private DNode header;

   private int size;

   public DLinkedList(){

   size = 0;

   }

   public void add(int index, E value) throws Exception
   {
   if(index > size || index < 0)
   {
   throw new IndexOutOfBoundsException();
   }
   if (header == null)
   {
   DNode n = new DNode(value, null, null);
   n.next = n;
   n.prev = n;
   header = n;
   }
   else
   {
   DNode current = header;
   for (int i = 0; i < index; i++)
   {
   current = current.next;
   }
   DNode n2 = new DNode(value, current, current.prev);
   current.prev.next = n2;
   current.prev = n2;
   if(index == 0)
   {
   header = n2;
   }
   }
   size++;
   }
public void print(){
   DNode current = header;
   for (int i = 0; i < size; i++)
{
   System.out.println(current.element);
current = current.next;
}
}

}

THE CHANGES ARE IN BOLD

Add a comment
Know the answer?
Add Answer to:
Please answer in Java. The code that needs to be modified is below. Thank you. Question:...
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
  • solve this Q in java languege Write a method that return DoublyLinkedList as reversed string For...

    solve this Q in java languege Write a method that return DoublyLinkedList as reversed string For example: If the elements of a list is 1, 2, 3, 4, 5, 6 the reverse string should be 6, 5, 4, 3, 2, 1 implement reverse method you have two steps: 1- you should start traversing from the last element of DoublyLinkedList (the previous of the trailer) 2- you should add the element inside each node to string don't forget the space in...

  • In Java Language. Modify the LinkedPostionalList class to support a method swap(p,q) that causes the underlying...

    In Java Language. Modify the LinkedPostionalList class to support a method swap(p,q) that causes the underlying nodes referenced by positions p and q to be exchanged for each other. Relink the existing nodes, do not create any new nodes. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- package lists; import java.util.Iterator; import java.util.NoSuchElementException; public class LinkedPositionalList implements PositionalList { //---------------- nested Node class ---------------- /** * Node of a doubly linked list, which stores a reference to its * element and to both the previous and next...

  • 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 =...

  • Language is in java. package com.cse.ds; public class Song<E> { E data; Song next; Song prev;...

    Language is in java. package com.cse.ds; public class Song<E> { E data; Song next; Song prev; /** Constructor to create singleton Song */ public Song(E element) { } /** Constructor to create singleton link it between previous and next * @param element Element to add, can be null * @param prevSong predecessor Song, can be null * @param nextSong successor Song, can be null */ public Song(E element, Song prevSong, Song nextSong) { //To be implemented } /** Remove this...

  • v. 15 points) implementing data structure operations C++ code for a new public member function to...

    v. 15 points) implementing data structure operations C++ code for a new public member function to be added to the doubly linked list data structure used in class. Return the average value of all elements in the list ple a list of integers) Throws a length error exception if the list is empty. template «typename E> E& 0; average You may use loops or recursion as needed. If necessary, you may add private helper function0s) The definition of the DLinkedList...

  • Using a doubly linked list as the underlying data structure, implement a list ADT that implements...

    Using a doubly linked list as the underlying data structure, implement a list ADT that implements the ListInterface.java found in the ProgProjTwo Eclipse project starting point for this assignment. In addition to the forward iterator defined by resetIterator( ) and getNextItem( ) in ListInterface.java, implement a backwards iterator by providing resetBackIterator( ) and getPreviousItem( ) methods. As noted in the syllabus addendum, you are encouraged to develop a find( ) helper method that can support various list ADT operations. A...

  • Please help with the codes for the implementation of java.util.List, specifically for the 3 below overridden...

    Please help with the codes for the implementation of java.util.List, specifically for the 3 below overridden methods @Override        public int lastIndexOf(Object arg0) { required code }        @Override        public ListIterator<R> listIterator() { required code }        @Override        public ListIterator<R> listIterator(int arg0) { required code } PLEASE NOTE: Below are some of other overridden methods that are already implemented, they are meant to be examples of what the answers to the above questions for the 3 methods should...

  • public class myLinkedList<E extends Comparable<E>>{       private Node<E> head = null;                    private Node<E>...

    public class myLinkedList<E extends Comparable<E>>{       private Node<E> head = null;                    private Node<E> tail = null;                       public myLinkedList() {        head = null;       } public void concatenateList (myLinkedList<E> M) {//attach another linkedList refered by M to the end of this linkedList       }          public int searchElement (E targetElement){//test if a target element can be found on the list, return the occurrences of the target element         ...

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • For some reason I am getting errors in this code. It is saying that the variables...

    For some reason I am getting errors in this code. It is saying that the variables are not visible. I will be providing one of the methods as well as the Node class. This is for a Linked list in Java. Here is the method giving me errors saying "the field Node<E>.item is not visible" as well as "Node<E>.next is not visible. public void addFirst(E e) {                // adds element e to the front of the list          ...

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