Question
Create a NetBeans project called LiskedList. A LinkedList.java class is given below. Add the following 3 methods to the class: 1. append(LinkedList ) Append takes
Create a NetBeans project called LiskedList. A LinkedList.java class is given below. Add the following 3 methods to the class:
1. append(LinkedList
Append takes a LinkedList as a parameter and appends that LinkedList on the current LinkedList.
2. removeDuplicates()
removeDuplicates removes duplicate elements from the current Linkedlist.
3. moveNode(LinkedList
moveNode(LinkedList
A ListDriver.java is given. Dont change this class.
Finally, make sure it can run in Netbeans and can have the output as following:
List with duplicates is
1
2
3
3
4
4
List without duplicates is
1
2
3
4
List after appending list2 onto list
1
2
3
4
5
6
List after moving 7 from the list3 to list
7
1
2
3
4
5
6
////////// below is LinkedList.java
import jsjf.ListADT;
import jsjf.exceptions.*;
import java.util.*;
/**
* LinkedList represents a linked implementation of a list.
*
* @author Java Foundations
* @version 4.0
*/
public abstract class LinkedList
{
protected int count,data;
protected LinearNode
protected LinearNode
protected int modCount;
/**
* Creates an empty list.
*/
public LinkedList()
{
count = 0;
head = tail = null;
modCount = 0;
}
Public static void LinkedList
{
if (tail == null) // Check if it is the first node in the list
{
tail = other; // make this node as tail of the list
}
else
{
// set next of other node to tail
tail -> next = other;
// make prev of other node to tail
other -> previous = tail;
tail = other; // since other is appending at the end of the list so other would be the new tail
}
}
public T removeDuplicates (LinearNode n) throws EmptyCollectionException
{
Hashtable table = new Hashtable();
LinearNode previous = null;
while(n!=null){
if(table.containsKey(n.data)){
previous.next = n.next;
} else {
table.put(n.data, true);
previous = n;
}
n = n.next;
}
}
public T moveNode() throws EmptyCollectionException
{
while (next != null)
{
node = next;
next = next.previous;
if (next.Contains(node))
{
move.Add(node);
list.Remove(node);
}
else
{
foreach (var item in move)
{
list.AddBefore(node, item);
node = node.Previous;
}
move.Clear();
}
if (next == null)
{
foreach (var item in move)
{
list.AddFirst(item);
}
move.Clear();
}
}
}
/**
* Removes the first element in this list and returns a reference
* to it. Throws an EmptyCollectionException if the list is empty.
*
* @return a reference to the first element of this list
* @throws EmptyCollectionException if the list is empty
*/
public T removeFirst() throws EmptyCollectionException
{
// To be completed as a Programming Project
return null; // temp
}
/**
* Removes the last element in this list and returns a reference
* to it. Throws an EmptyCollectionException if the list is empty.
*
* @return the last element in this list
* @throws EmptyCollectionException if the list is empty
*/
public T removeLast() throws EmptyCollectionException
{
// To be completed as a Programming Project
return null; // temp
}
/**
* Removes the first instance of the specified element from this
* list and returns a reference to it. Throws an EmptyCollectionException
* if the list is empty. Throws a ElementNotFoundException if the
* specified element is not found in the list.
*
* @param targetElement the element to be removed from the list
* @return a reference to the removed element
* @throws EmptyCollectionException if the list is empty
* @throws ElementNotFoundException if the target element is not found
*/
public T remove(T targetElement) throws EmptyCollectionException,
ElementNotFoundException
{
if (isEmpty())
throw new EmptyCollectionException("LinkedList");
boolean found = false;
LinearNode
LinearNode
while (current != null && !found)
if (targetElement.equals(current.getElement()))
found = true;
else
{
previous = current;
current = current.getNext();
}
if (!found)
throw new ElementNotFoundException("LinkedList");
if (size() == 1) // only one element in the list
head = tail = null;
else if (current.equals(head)) // target is at the head
head = current.getNext();
else if (current.equals(tail)) // target is at the tail
{
tail = previous;
tail.setNext(null);
}
else // target is in the middle
previous.setNext(current.getNext());
count--;
modCount++;
return current.getElement();
}
/**
* Returns the first element in this list without removing it.
*
* @return the first element in this list
* @throws EmptyCollectionException if the list is empty
*/
public T first() throws EmptyCollectionException
{
// To be completed as a Programming Project
return null; // temp
}
/**
* Returns the last element in this list without removing it.
*
* @return the last element in this list
* @throws EmptyCollectionException if the list is empty
*/
public T last() throws EmptyCollectionException
{
// To be completed as a Programming Project
return null; // temp
}
/**
* Returns true if the specified element is found in this list and
* false otherwise. Throws an EmptyCollectionException if the list
* is empty.
*
* @param targetElement the element that is sought in the list
* @return true if the element is found in this list
* @throws EmptyCollectionException if the list is empty
*/
public boolean contains(T targetElement) throws EmptyCollectionException
{
// To be completed as a Programming Project
return true; // temp
}
/**
* Returns true if this list is empty and false otherwise.
*
* @return true if the list is empty, false otherwise
*/
public boolean isEmpty()
{
// To be completed as a Programming Project
return true; // temp
}
/**
* Returns the number of elements in this list.
*
* @return the number of elements in the list
*/
public int size()
{
// To be completed as a Programming Project
return 0; // temp
}
/**
* Returns a string representation of this list.
*
* @return a string representation of the list
*/
public String toString()
{
// To be completed as a Programming Project
return ""; // temp
}
/**
* Returns an iterator for the elements in this list.
*
* @return an iterator over the elements of the list
*/
public Iterator
{
return new LinkedListIterator();
}
/**
* LinkedIterator represents an iterator for a linked list of linear nodes.
*/
private class LinkedListIterator implements Iterator
{
private int iteratorModCount; // the number of elements in the collection
private LinearNode
/**
* Sets up this iterator using the specified items.
*
* @param collection the collection the iterator will move over
* @param size the integer size of the collection
*/
public LinkedListIterator()
{
current = head;
iteratorModCount = modCount;
}
/**
* Returns true if this iterator has at least one more element
* to deliver in the iteration.
*
* @return true if this iterator has at least one more element to deliver
* in the iteration
* @throws ConcurrentModificationException if the collection has changed
* while the iterator is in use
*/
public boolean hasNext() throws ConcurrentModificationException
{
if (iteratorModCount != modCount)
throw new ConcurrentModificationException();
return (current != null);
}
/**
* Returns the next element in the iteration. If there are no
* more elements in this iteration, a NoSuchElementException is
* thrown.
*
* @return the next element in the iteration
* @throws NoSuchElementException if the iterator is empty
*/
public T next() throws ConcurrentModificationException
{
if (!hasNext())
throw new NoSuchElementException();
T result = current.getElement();
current = current.getNext();
return result;
}
/**
* The remove operation is not supported.
*
* @throws UnsupportedOperationException if the remove operation is called
*/
public void remove() throws UnsupportedOperationException
{
throw new UnsupportedOperationException();
}
}}
//////////////below is ListDriver.java
public class ExamList {
public static void main(String[] args) { // create 3 LinkedLists LinkedUnorderedList list = new LinkedUnorderedList(); LinkedUnorderedList list2 = new LinkedUnorderedList(); LinkedUnorderedList list3 = new LinkedUnorderedList();
// add some nodes to "list" // note that there are duplicates list.addToRear(new Integer(1)); list.addToRear(new Integer(2)); list.addToRear(new Integer(3)); list.addToRear(new Integer(3)); list.addToRear(new Integer(4)); list.addToRear(new Integer(4));
//print the list with duplicates System.out.println("list with duplicates is " + list);
//remove the duplicates list.removeDuplicates();
//print the modified list without duplicates System.out.println("list without duplicates is " + list);
//add some nodes to list2 list2.addToRear(new Integer(5)); list2.addToRear(new Integer(6));
//append list2 to list list.append(list2);
//print list after appending list2 onto it System.out.println("List after appending list2 onto list " + list + " ");
//add some nodes to list3 list3.addToRear(new Integer(7)); list3.addToRear(new Integer(8)); list3.addToRear(new Integer(9)); list3.addToRear(new Integer(10));
//move the head node from list3 to the front of list list.moveNode(list3);
System.out.println("List after moving 7 from list 3 to list " + list); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started