JAVA ONLY please mark names of the java files thanks public
JAVA ONLY
please mark names of the java files
thanks
public class LinkedList3<T>
{
private class Node<T>
{
private T data;
private Node<T> link;
public Node( )
{
data = null;
link = null;
}
public Node(T newData, Node<T> linkValue)
{
data = newData;
link = linkValue;
}
}//End of Node<T> inner class
private Node<T> head;
public LinkedList3( )
{
head = null;
}
/**
Adds a node at the start of the list with the specified data.
The added node will be the first node in the list.
*/
public void addToStart(T itemData)
{
head = new Node<T>(itemData, head);
}
/**
Removes the head node and returns true if the list contains at least
one node. Returns false if the list is empty.
*/
public boolean deleteHeadNode( )
{
if (head != null)
{
head = head.link;
return true;
}
else
return false;
}
/**
Returns the number of nodes in the list.
*/
public int size( )
{
int count = 0;
Node<T> position = head;
while (position != null)
{
count++;
position = position.link;
}
return count;
}
public boolean contains(T item)
{
return (find(item) != null);
}
/**
Finds the first node containing the target item, and returns a
reference to that node. If target is not in the list, null is returned.
*/
private Node<T> find(T target)
{
Node<T> position = head;
T itemAtPosition;
while (position != null)
{
itemAtPosition = position.data;
if (itemAtPosition.equals(target))
return position;
position = position.link;
}
return null; //target was not found
}
/**
Finds the first node containing the target and returns a reference
to the data in that node. If target is not in the list, null is returned.
*/
public T findData(T target)
{
return find(target).data;
}
public void outputList( )
{
Node<T> position = head;
while (position != null)
{
System.out.println(position.data);
position = position.link;
}
}
public boolean isEmpty( )
{
return (head == null);
}
public void clear( )
{
head = null;
}
/*
For two lists to be equal they must contain the same data items in
the same order. The equals method of T is used to compare data items.
*/
public boolean equals(Object otherObject)
{
if (otherObject == null)
return false;
else if (getClass( ) != otherObject.getClass( ))
return false;
else
{
LinkedList3<T> otherList = (LinkedList3<T>)otherObject;
if (size( ) != otherList.size( ))
return false;
Node<T> position = head;
Node<T> otherPosition = otherList.head;
while (position != null)
{
if (!(position.data.equals(otherPosition.data)))
return false;
position = position.link;
otherPosition = otherPosition.link;
}
return true; //no mismatch was not found
}
}
}
IN JAVA Take the code (it is from the Savitch student data files) listed here and 1) make it doubly linked. 2) Add a copy constructor 3) Add a clone method 4) Replace outputList with toString 5) Code an iterator method 6) Keep track of the tail and add a method outputBackwards to prove the list is doubly linked. Or use the itch code in the book for a doubly linked list and a the functionality mentioned above think you will learn less that way but it is less work. To make the node doubly linked change private Node link, to private Node T> next; private Node T> prev; Test all functionality by hard coding test cases into your demo fileSolution
class LinkedList3<T> implements Cloneable{
    private class Node<T> {
        private T data;
        private Node<T> next;
        private Node<T> prev;
       public Node() {
            data = null;
            next = null;
            prev = null;
        }
       public Node(T newData, Node<T> nextValue, Node<T> prevValue) {
            data = newData;
            next = nextValue;
            prev = prevValue;
        }
    }// End of Node<T> inner class
   private Node<T> head;
    private Node<T> tail;// for doubly linked list we need tail Node
   public LinkedList3() {
        head = null;
    }
   /**
    * Adds a node at the start of the list with the specified data. The added
    * node will be the first node in the list.
    */
    public void addToStart(T itemData) {
        // head = new Node<T>(itemData, head);
        Node tmp = new Node(itemData, head, null);
        if (head != null) {
            head.prev = tmp;
        }
        head = tmp;
        if (tail == null) {
            tail = tmp;
        }
    }
   /**
    * Removes the head node and returns true if the list contains at least one
    * node. Returns false if the list is empty.
    */
    public boolean deleteHeadNode() {
       if (head != null) {
            head = head.next;
            head.prev = null;// making previous link as null
            return true;
        } else
            return false;
    }
   /**
    * Returns the number of nodes in the list.
    */
    public int size() {
        int count = 0;
        Node<T> position = head;
        while (position != null) {
            count++;
            position = position.next;
        }
        return count;
    }
   public boolean contains(T item) {
        return (find(item) != null);
    }
   /**
    * Finds the first node containing the target item, and returns a reference
    * to that node. If target is not in the list, null is returned.
    */
    private Node<T> find(T target) {
        Node<T> position = head;
        T itemAtPosition;
        while (position != null) {
            itemAtPosition = position.data;
            if (itemAtPosition.equals(target))
                return position;
            position = position.next;
        }
        return null; // target was not found
    }
   /**
    * Finds the first node containing the target and returns a reference to the
    * data in that node. If target is not in the list, null is returned.
    */
    public T findData(T target) {
        return find(target).data;
    }
   public void outputList() {
        Node<T> position = head;
        while (position != null) {
            System.out.println(position.data);
            position = position.next;
        }
    }
   public String toString() {
        StringBuilder s = new StringBuilder();
        Node<T> position = head;
        while (position != null) {
            s.append(position.data + \" \");
            position = position.next;
        }
        return s.toString();
    }
   public boolean isEmpty() {
        return (head == null);
    }
   public void clear() {
        head = null;
    }
   /*
    * For two lists to be equal they must contain the same data items in the
    * same order. The equals method of T is used to compare data items.
    */
    public boolean equals(Object otherObject) {
        if (otherObject == null)
            return false;
        else if (getClass() != otherObject.getClass())
            return false;
        else {
            LinkedList3<T> otherList = (LinkedList3<T>) otherObject;
            if (size() != otherList.size())
                return false;
            Node<T> position = head;
            Node<T> otherPosition = otherList.head;
            while (position != null) {
                if (!(position.data.equals(otherPosition.data)))
                    return false;
                position = position.next;
                otherPosition = otherPosition.next;
            }
            return true; // no mismatch was not found
        }
    }
   /**
    * * this method walks forward through the linked list
    */
    public void iterateForward() {
        System.out.println(\"iterating forward..\");
        Node tmp = head;
        while (tmp != null) {
            System.out.println(tmp.data);
            tmp = tmp.next;
        }
    }
   /**
    * * this method walks backward through the linked list
    */
    public void iterateBackward() {
        System.out.println(\"iterating backword..\");
        Node tmp = tail;
        while (tmp != null) {
            System.out.println(tmp.data);
            tmp = tmp.prev;
        }
    }
   
    @Override
    protected Object clone() throws CloneNotSupportedException {
        // TODO Auto-generated method stub
        return super.clone();
    }
}
public class DoublyLinkedList {
   /**
    * @param args
    * @throws CloneNotSupportedException
    */
    public static void main(String[] args) throws CloneNotSupportedException {
        LinkedList3<String> linkedList3= new LinkedList3<>();
        linkedList3.addToStart(\"Hello1\");
        linkedList3.addToStart(\"Hello2\");
        linkedList3.addToStart(\"Hello3\");
        linkedList3.addToStart(\"Hello4\");
       
        linkedList3.iterateForward();
        linkedList3.iterateBackward();
        System.out.println(\"To String method\");
        System.out.println(linkedList3.toString());
       
        LinkedList3<String> clonedObj=(LinkedList3<String>) linkedList3.clone();
        System.out.println(\"printing cloned object\");
        clonedObj.iterateForward();
       
    }
}
 ------------------output-----------------------
 iterating forward..
 Hello4
 Hello3
 Hello2
 Hello1
 iterating backword..
 Hello1
 Hello2
 Hello3
 Hello4
 To String method
 Hello4 Hello3 Hello2 Hello1
 printing cloned object
 iterating forward..
 Hello4
 Hello3
 Hello2
 Hello1
 -----------output ends------------------
Note: Feel free to ask any doubts/queries. God bless you!!








