I\'m doing a program for my java class and I\'m supposed to make it doubly linke
ID: 3636088 • Letter: I
Question
I'm doing a program for my java class and I'm supposed to make it doubly linked, add a copy constructor, add a clone method, replace outputList with toString, Code an iterator method, and keep track of the tail and add a method outputBackwards to prove the list is doubly linked. Can you help me fix my code, please?package LinkedList3;
//Matthew Romani Corbin
import java.util.NoSuchElementException;
//CIT130
//Alter LinkedList3
public class LinkedList3<T> implements Cloneable
{
public class Node<T>
{
private T data;
private Node<T> link;
private String item;
private Node<T> prev;//***************
private Node<T> next;//***************
public Node( )
{
//data = null;
item = null;
prev = null;
next = null;
}
public Node(T newData, Node<T> linkValue, Node<T> revValue)
{
data = newData;
prev = linkValue;
next = revValue;
}
}//End of Node<T> inner class
private Node<T> head;
private Node<T> tail;
public LinkedList3( )
{
head = null;
tail = null;
}
public LinkedList3(LinkedList3<T> otherList)
{
if (otherList == null)
throw new NullPointerException( );
else
head = copyOf(otherList.head);
}
/**
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);
tail = new Node<T>(itemData, tail);
}
/**
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;
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 outputForward( )
{
Node<T> position = head;
while (position != null)
{
System.out.println(position.data);
position = position.next;
}
}
public void outputBackward( )
{
Node<T> position = tail;
while(position != null)
{
System.out.println(position.data);
position = position.prev;
}
}
public boolean isEmpty( )
{
return (head == null);
}
public void clear( )
{
head = null;
}
public class List3Iterator
{
private Node position;
private Node previous;
public List3Iterator()
{
position = head;
previous = null;
}
public void restart()
{
position = head;
previous = null;
}
public String next()
{
if (!hasNext( ))
throw new NoSuchElementException( );
String toReturn = position.item;
previous = position;
position = position.link;
return toReturn;
}
public boolean hasNext( )
{
return (position != null);
}
public String peek( )
{
if (!hasNext( ))
throw new IllegalStateException( );
return position.item;
}
public void addHere(String newData)
{
if(position == null && previous != null)
previous.link = new Node<T>(newData, null);
//else if (position == null || previous == null)
//LinkedList3.this.addToStart(newData);
else
{
Node<T> temp = new Node<T>(newData, position);
previous.link = temp;
previous = temp;
}
}
public void delete( )
{
if (position == null)
throw new IllegalStateException( );
else if (previous == null)
{
head = head.link;
position = head;
}
else
{
previous.link = position.link;
position = position.link;
}
}
}
public List3Iterator iterator( )
{
return new List3Iterator( );
}
/*
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
}
}
public LinkedList3<T> clone( )
{
try
{
LinkedList3<T> copy = (LinkedList3<T>)super.clone( );
if(head == null)
copy.head = null;
else
copy.head = copyOf(head);
return copy;
}
catch(CloneNotSupportedException e)
{
return null;
}
}
private Node<T> copyOf(Node<T> otherHead)
{
Node<T> position = otherHead;
Node<T> newHead;
Node<T> end = null;
newHead = new Node<T>(position.data, null);
end = newHead;
position = position.next;
while (position != null)
{
end.next = new Node<T>(position.data, null);
end = end.next;
position = position.next;
}
return newHead;
}
}
package LinkedList3;
/**
*
* @author Matt Galaxy
*/
//import java.*;
public class LinkedList3Demo
{
public static void main(String[] args)
{
LinkedList3 list = new LinkedList3( );
LinkedList3.List3Iterator i = list.iterator( );
list.addToStart("Apples");
list.addToStart("Bananas");
list.addToStart("Cantaloupe");
//list.addToStart("Guava");
//list.addToStart("Pineapple");
//list.addToStart("Orange");
//list.addToStart("Clementine");
System.out.println("Iterator");
System.out.println("List contains");
i.restart( );
while (i.hasNext( ))
System.out.println( );
i.restart( );
i.next( );
i.next( );
System.out.println("Delete" + i.peek( ));
i.delete( );
System.out.println("List now contains");
i.restart( );
while (i.hasNext( ))
System.out.println(i.next());
System.out.println( );
i.restart( );
i.next( );
System.out.println("Inserting socks before " + i.peek( ));
i.restart( );
System.out.println("List now contains:");
while (i.hasNext( ))
System.out.println(i.next( ));
System.out.println();
System.out.println("List has " + list.size( )
+ " nodes.");
System.out.println("This is forward output.");
list.outputForward( );
System.out.println("This is reverse output.");
list.outputBackward( );
System.out.println("NEXT LINE");
if (list.contains("Cantaloupe"))
System.out.println("Cantaloupe is on list.");
else
System.out.println("Cantaloupe is NOT on list.");
list.deleteHeadNode( );
if (list.contains("Cantaloupe"))
System.out.println("Cantaloupe is on list.");
else
System.out.println("Cantaloupe is NOT on list.");
//while (list.deleteHeadNode( ))
// ; //Empty loop body
System.out.println("Start of list:");
list.outputForward( );
System.out.println("End of list.");
}
}
Explanation / Answer
Hi i have written the code on my machine and currently debugging it, but i can't type it and post it here because time has expired please rate me lifesaver and i'll make sure the answer is in your inbox! or my effort and time would go all in vain i don't do this generally but i am doing because of the circumstances
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.