[Solved] Using Java Typical Implementation Contain Outer Class Called Doublylinkedlist However Ins Q37167287
using JAVA- in a typical implementation it will contain an outerclass called DoublyLinkedList. However, inside of this will be aninner class called Node (examples of inner and outer classes).You will use the Node class in building your linked list. Each nodeobject will have 3 fields:
-
● elem – this contains the information stored in thisposition
-
● next – a reference to the next node
-
● prev – a reference to the previous node
You will chain node objects together using next and prev tocreate a list.
Now that we have a chain of nodes, we need some way of accessingthem. Your DoublyLinkedList class with have three fields to handlethis:
-
● head – a node with no value whose next field is the start ofthe list
-
● tail – a node with no value whose prev field is the end of thelist
-
● size – the number of nodes in the list
For your implementation, you must use a dummy head and tail,as this greatly simplifies the implementation of variousmethods.
In the Node class you will implement the followingmethods:
private Node(T element)
Creates a singleton node.
private Node(T element, Node nextNode, Node prevNode)
Creates a node that is linked to other nodes.
public void setElement(T element)
Alters the data stored at this node.
public T getElement()
Returns the data stored at this node.
public void setNext(Node n)
Set the successor node.
public Node getNext()
Access the successor node.
public void setPrev(Node p)
Set the predecessor node.
public Node getPrev()
Access the predecessor node.
public void remove()
Unlinks the node from its list.
In DoublyLinkedList class you will need to implement thefollowing methods:
public DoublyLinkedList()
Creates an empty linked-list with a dummy head and tail.
public boolean add(T element)
Appends a node containing the specified data to the end of thelist. Returns true if the item is added and false otherwise.
@throws NullPointerException – if data received is null
public void add(int index, T element)
Inserts a node containing the specified data at the desiredlocation.
@throws IndexOutOfBoundsException – index is outside the range[0, size] @throws NullPointerException – if data received isnull
public void clear()
Removes all elements from the list
public boolean contains(Object element)
Returns true if the list contains the specified element at leastonce.
**This method overrides an older Java method that did not usegenerics. Before generics, programmers had to use type casting toensure that parameters passed in were of the correct type. Since wecannot change the method signature when overriding a method we mustpass in the parameter as an Object and then cast it to type T (thiscode is given in the starter code)
public T get(int index)
Access the data contained in the node at the specifiedindex.
@throws IndexOutOfBoundsException – index is outside the range[0, size – 1]
private Node getNth(int index)
Helper method for accessing the node in the specified index ofthe list.
Note: you should use this method as less as possibleconsidering its O(n) run time. If you need to get access to a Nodeat certain index for multiple times in a same method, please callgetNth() only once and store it into a local variable.
public boolean isEmpty()
Return true if the list contains no elements, falseotherwise.
public T remove(int index)
Remove the element at the specified index from the list andreturn the data it contained
@throws IndexOutOfBoundsException – index is outside the range[0, size – 1]
public T set(int index, T element)
Alters the data stored in the node at the specified index andreturns the data previously at the specified position.
@throws IndexOutOfBoundsException – index is outside the range[0, size – 1] @throws NullPointerException - if data received isnull
public int size()
Return the number of elements stored in the list.
Expert Answer
Answer to using JAVA- in a typical implementation it will contain an outer class called DoublyLinkedList. However, inside of… . . .
OR

