Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

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

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. - Its 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{

private static class DNode{

private E element;

private DNode prev;

private DNode next;

public DNode(E e){

this(e, null, null);

}

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

element = e;

prev = p;

next = n;

}

public E getE(){

return element;

}

public DNode getPrev(){

return prev;

}

public DNode getNext(){

return next;

}

public void setE(E e){

element = e;

}

public void setPrev(DNode p){

prev = p;

}

public void setNext(DNode n){

next = n;

}

}

private DNode header;

private DNode trailer;

private int size;

public DLinkedList(){

header = new DNode(null);

trailer = new DNode(null, header, null);

header.setNext(trailer);

size = 0;

}

public void print(){

DNode temp = header.getNext();

while (temp != trailer){

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

temp = temp.getNext();

}

System.out.println();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started