Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi.. i\'m working in my java project which is 2 classes and driver fist class is

ID: 3620504 • Letter: H

Question

Hi..
i'm working in my java project which is 2 classes and driver
fist class is the DNode
public class DNode{
protected String element;
protected DNode prev, next;

public DNode(String e, DNode p, DNode n){
element=e;
prev=p;
next=n;
}
public String getElement(){
return element;
}
public DNode getPrev(){
return prev;
}
public DNode getNext(){
return next;
}
public void setElement(String newElem){
element=newElem;
}
public void setPrev(DNode newPrev){
prev=newPrev;
}
public void setNext(DNode newNext){
next=newNext;
}
}



=========
the second one is DList

public class DList
{
protected int size;
protected DNode header, trailer;
public DList()
{
size = 0;
header = new DNode(null, null, null);
trailer = new DNode(null, header, null);
header.setNext(trailer);
}
public int size()
{
return size;
}
public boolean isEmpty()
{
return (size == 0);
}
public DNode getFirst( )throws IllegalStateException
{
if (isEmpty())
{
throw new IllegalStateException("List is Empty");
}
return header.getNext();
}
public DNode getLast() throws IllegalStateException
{
if (isEmpty())
{
throw new IllegalStateException("List is Empty");
}
return trailer.getPrev();
}

public DNode getPrev(DNode v) throws IllegalArgumentException
{
if (v == header)
{
throw new IllegalArgumentException(" Can not move back past the header of the list");
}
return v.getPrev();
}

public DNode getNext(DNode v) throws IllegalArgumentException
{
if (v == trailer)
{
throw new IllegalArgumentException("Can not move forward Past the trailer of the list");
}
return v.getNext();
}

public void addBefore(DNode v, DNode z) throws IllegalArgumentException
{
DNode u = getPrev(v);
z.setPrev(u);
z.setNext(v);
v.setPrev(z);
u.setNext(z);
size++;
}
public void addAfter(DNode v, DNode z)
{
DNode w = getNext(v);
z.setPrev(v);
z.setNext(w);
w.setPrev(z);
v.setNext(z);
size++;
}

public void addFirst(DNode v)
{
addAfter(header,v);
}

public void addLast(DNode v)
{
addBefore(trailer,v);
}
public void remove(DNode v)
{
DNode u = getPrev(v);
DNode w = getNext(v);
w.setPrev(u);
u.setNext(w);
v.setPrev(null);
v.setNext(null);
size--;
}
public boolean hasPrev(DNode v)
{
return v != header;
}
public boolean hasNext(DNode v)
{
return v != trailer;
}
public String toString()
{
String s = "[";
DNode v = header.getNext();
while(v != trailer)
{
s += v.getElement();
v = v.getNext();
if(v != trailer)
{
s += ",";
}
}
s += "]";
return s;
}
public void addSorted(DNode v)
{

}
}


i couldn't do the last method add sorted
which is adding a node to the list and sorted it alphabetically
for example if we have a list of
Apple
Bread
Door

if we did
myList.addSorted( new DNode("Car");
the new list gonna be
Apple
Bread
Car
Door
???

thank you,,,,

Explanation / Answer

Dear... Sample code of AddSorted method for the Doubly Linked List: public DNode getLast() throws IllegalStateException { if (isEmpty()) { throw new IllegalStateException("the list is empty"); } return trailer.getPrev(); } public void insertionSort(DoublyLinkedList list) { if (list.size 0) { ins = ins.getPrev(); list.addAfter(ins, p); if (ins == end) { end = end.getNext(); } } } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote