Question: public class Link { public long dData; public Link next; public Link previous; public Link( long d) { dData = d; } // ------------------------------------------------------------- public

public class Link
{
public long dData;
public Link next;
public Link previous;
public Link(long d)
{
dData = d;
}
// -------------------------------------------------------------
public void displayLink()
{
System.out.print(dData + " ");
}
class DoublyLinkedList
{
private Link first;
private Link last;
public DoublyLinkedList() // constructor
{
first = null;
last =null;
}
public boolean isEmpty()
{ return first==null; }
public void insertFirst(long dd)
{
Link newLink = new Link(dd);
if( isEmpty() )
last = newLink;
else
{
first.previous = newLink;
newLink.next = first;
first = newLink;
}
}
public void insertLast(long dd) {
Link newLink = new Link(dd);
if( isEmpty() )
first = newLink;
else
{
last.next = newLink;
newLink.previous = last;
}
}
public Link deleteFirst() {
Link temp = first;
if(first.next == null)
last = null;
else
first.next.previous = null;
first = first.next;
return temp;
}
// -------------------------------------------------------------
public Link deleteLast() // delete last link
{
Link temp = last;
if (first.next == null)
first = null;
else
last.previous.next = null;
last = last.previous;
return temp;
}
public boolean insertAfter(long key, long dd)
{
Link current = first;
while(current.dData != key)
{
current = current.next;
if(current == null)
return false;
}
Link newLink = new Link(dd);
if(current==last)
{
newLink.next = null;
last = newLink;
}
else
{
newLink.next = current.next;
current.next.previous = newLink;
}
newLink.previous = current;
current.next = newLink;
return true;
}
public Link deleteKey(long key) {
Link current = first;
while(current.dData != key) {
current = current.next;
if(current == null)
return null;
}
if(current==first)
first = current.next;
else
current.previous.next = current.next;
if(current==last)
last = current.previous;
else
current.next.previous = current.previous; return current;
return current;
}
public void displayForward()
{
System.out.print("List (first-->last): ");
Link current = first;
while(current != null)
{
current.displayLink();
current = current.next; }
}
System.out.print(" ");
}
public void displayBackward() {
System.out.print("List (last-->first): ");
Link current = last;
while(current != null)
{
current.displayLink();
current = current.previous;
}
System.out.println();
}
} //
public class InBetweenDemo public static void main(String args) // make a new list DoublyLinkedList theList new DoublyLinkedList) theList.insertFirst (22); theList.insertFirst (44) theList.insertPirst (66) // insert at front theList.insertLast (11); theList.insertLast (33) theList.insertLast (55) // insert at rear theList.displayForward); int n-55;// display list forward System.out.println("inBetween("+n+tList.inBetween (n)) theList.deleteFirst ( theList.displayForward(); n#55; System.out.println("inBetween("+n+thList.inBetween (n)) // delete first item theList.deleteLast() theList.displayForward(); n-33 System.out.println("inBetweentheList.inBetween (n) theList.deleteKey (22); theList.displayForward // delete item with key 11 System.out.println("inBetween("+n++tList.inBetween (n)) n-99 System.out.println( "inBetween("++ theList.inBetween (n) theList.displayForward ); // display list forward theList.insertAfter (11, 77); I/ insert 77 after 22 theList.insertAfter (33, 88 / insert 88 after 33 theList.displayForward ); // display list forward // end main () // end class DoublyLinkedApp // doublyLinked.java // demonstrates doubly-linked list // to run this program: c>java DoublyLinkedApp Suppose we define an operation on a DoublyLinkedList called inBetween that accepts an item as a parameter and returns true if the item is "in between" the smallest and largest list elements. That is, based on the compareTo method defined for list elements, the item is larger than the smallest list element and smaller than the largest list element. Otherwise, the method returns false (even if the item matches" the smallest or largest element). Design and code inBetween as a public method of the DoublyLinkedList class. To test, use the provided demo file
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
