This class implements a doubly linked list in which the nodes are of the class DLNode. This class must implement the interface DLListADT.java that specifies the public methods in this class. The header for this class will then be public class DLList implements DLListADT This class will have three private instance variables: • private DLNode front. This is a reference to the first node of the doubly linked list. • private DLNode rear. This is a reference to the last node of the doubly linked list. • private int count. The value of this variable is the number of data items in the linked list. This class needs to implement the following methods. • public DLList(). Creates an empty list. • public void insert (T dataItem, int value). Adds a new DLNode storing the given dataItem and value to the rear of the doubly linked list. • public int getDataValue(T dataItem) throws InvalidDataItemException. Returns the integer value associated to the specified dataItem. An InvalidDataItemException is thrown if the given dataItem is not in the list. Note that to check whether dataItem is in the list you need to scan the linked list using linear search. To check if the data item stored in a node of the linked list is the same as dataItem you must use the equals method, not the “==” operator. • public void changeValue (T dataItem, int newValue) throws InvalidDataItemException. Changes the value of dataItem to newValue. An InvalidDataItemException is thrown if the given dataItem is not in the list. • public T getSmallest() throws EmptyListException. Removes and returns the data item in the list with smallest associated value. If several items in the list have the same associated value, any one of them is returned. An EmptyListException is thrown if the list is empty. Note that to find the data item with smallest value the entire linked list needs to be scanned using linear search. • public boolean isEmpty(). Returns true if the list is empty and it returns false otherwise. • public int size(). Returns the number of data items in the list. • String toString(). Returns a String representation of the linked list. This method will invoke the toString method from each data item in the priority queue and concatenate these strings to produce a string of the form: “List: dataItem1,value1; dataItem2,value2; …”. To make sure you understand the operations that you need to implement, consider the above doubly linked list where each nod stores a String and an integer value. If we invoke getDataValue (“data 2”), this method should return the value 8, while invoking getDataValue(“data 4”) should throw an InvalidDataItemException. Invoking changeValue(“data 1”, 0) will set the value stored in the first node to 0; if now we getSmallest(), this will return “data 1” (as now this data item has the smallest value) and the first node of the list will be removed.
DLLISTADT.java:
import java.util.ListIterator;
import java.util.NoSuchElementException;
public class DoublyLinkedList<Item> implements
Iterable<Item> {
private int n; // number of elements on list
private Node pre; //
sentinel before first item
private Node post; // sentinel after last
item
public DoublyLinkedList() {
pre = new Node();
post = new Node();
pre.next = post;
post.prev = pre;
}
public boolean isEmpty() { return n == 0; }
public int size() { return n;
}
//add the item to the list
public void add(Item item) {
Node last = post.prev;
Node x = new Node();
x.item = item;
x.next = post;
x.prev = last;
post.prev = x;
last.next = x;
n++;
}
public ListIterator<Item> iterator()
{ return new DoublyLinkedListIterator(); }
//assumes no calls to DoublyLinkedList.add() during
iteration.
private class DoublyLinkedListIterator implements
ListIterator<Item> {
private Node current = pre.next;
//the node that is returned by next()
private Node lastAccessed = null;
//the last nnode to be returned by prev() or neext()
private int index = 0; //reset to
nul upon inventimg remove() or add()
public boolean hasNext()
{ return index < n; }
public boolean hasPrevious()
{ return index > 0; }
public int previousIndex()
{ return index - 1 ; }
public int nextIndex()
{ return index; }
public Item next()
{
if(!hasNext())
throws new NoSuchElementException();
lastAccessed =
current;
Item item =
current.next;
current =
current.next;
index++;
return
item;
}
public Item previous()
{
if(!hasPrevious()) throws new NoSuchElementException();
current =
current.prev;
index--;
lastAccessed =
current;
return
current.item;
}
//replace the item of the element
that was last accessed by next() or previous()
//condition: no calls to remove()
or add() after last call to next() or previous()
public void set(Item item)
{
if(lastAccessed
== null) throw new IlegalStateException();
lastAccessed.item = item;
}
//remove the element that was last
accessed by next() or previous()
//condition: no calls to remove()
or add() after last call to next() or previous()
public void remove()
{
if(lastAccessed
== null) throw new IlegalStateException();
Node x =
lastAccessed.prev;
Node y =
lastAccessed.next;
x.next =
y;
y.prev =
x;
n--;
if(current ==
lastAccessed)
current = y;
else
index--;
lastAccessed =
null;
}
//add element to list
public void add(Item item)
{
Node x =
current.prev;
Node y = new
Node();
Node z =
current;
x.next =
y;
y.next =
z;
z.prev =
y;
y.prev =
x;
n++;
index++;
lastAccessed =
null;
}
public String toString()
{
StringBuilder s
= new StringBuilder();
for(Item item :
this)
s.append(item + " ");
return
s.toString();
}
//a test client
public static void main(String[]
args)
{
int n = Integer.parseInt(args[0]);
//add elements 1, ..... , n
stdOut.println(n + "ranndom integers between 0
and 99");
DoublyLinkedList<Integer> list = new
DoublyLinkedList<Integer>();
for(int i=0;i<n ;i++)
list.add(stdRandom.uniform(100));
stdOut.println(list);
stdOut.println();
ListIterator<Integer> iterator = list.iteraor();
//go forwards
with next() and set()
stdOut.println("add 1 to each element via next() and set()");
while(iterator.hasNext())
{
int x = iteraor.next();
iterator.set(x+1);
}
stdOut.println(list);
stdOut.println();
//go backwards
with previous() and set()
stdOut.println("multiply each element by 3 via previous() and
set()");
while(iterator.hasPrevious())
{
int x = iteraor.previous();
iterator.set(x + x + x);
}
stdOut.println(list);
stdOut.println();
//remove all
elements that are multiples of 4 via next() and remove()
stdOut.println("remove elements that are multiple of 4 via next()
and remove()");
while(iterator.hasNext())
{
int x = iteraor.next();
if(x % 4 == 0) iterator.remove();
}
stdOut.println(list);
stdOut.println();
//remove all
even elements via previous() and remove()
stdOut.println("remove elements that are even via previous() and
remove()");
while(iterator.hasprevious())
{
int x = iteraor.previous();
if(x % 2== 0) iterator.remove();
}
stdOut.println(list);
stdOut.println();
//add elements
via next() and add()
stdOut.println("add elements via next() and add()");
while(iterator.hasNext())
{
int x = iteraor.next();
iterator.add(x + 1);
}
stdOut.println(list);
stdOut.println();
//add elements
via previous() and add()
stdOut.println("add elements via previous() and add()");
while(iterator.hasprevious())
{
int x = iteraor.previous();
iterator.add(x * 10);
iterator.previous();
}
stdOut.println(list);
stdOut.println();
}
}
}
This class implements a doubly linked list in which the nodes are of the class DLNode....
Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface: /** * An ordered list of items. */ public interface ItemList<E> { /** * Append an item to the end of the list * * @param item – item to be appended */ public void append(E item); /** * Insert an item at a specified index position * * @param item – item to be...
C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...
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...
Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to understand what each field stores and what each method is doing. Modify and complete the class as described below •The field size was defined in the class but was never maintained. Set the current default value and modify it whenever it is needed in the existing methods and other methods you implement as it is needed. It should always include the number of Nodes inside the...
Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...
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...
Language: Java Topic: Doubly Linked Lists Write a method that removes and returns the last copy of the given data from the list. Do not return the same data that was passed in. Return the data that was stored in the list. Must be O(1) if data is in the tail and O(n) for all other cases. * @param data the data to be removed from the list * @return the data that was removed * @throws java.lang.IllegalArgumentException if...
Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...
Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for an implementation of a Singly Linked List. There are 3 files: Driver.java -- testing the List functions in a main() method. LinkNode.java -- Class definition for a Node, which is the underlying entity that stores the items for the linked list. SinglyLinkedList.java -- Class definition for the Singly Linked List. All the heavy lifting happens here. Task 1 - Review & Testing: Create...
Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for an implementation of a Singly Linked List. There are 3 files: Driver.java -- testing the List functions in a main() method. LinkNode.java -- Class definition for a Node, which is the underlying entity that stores the items for the linked list. SinglyLinkedList.java -- Class definition for the Singly Linked List. All the heavy lifting happens here. Task 1 - Review & Testing: Create...