Java Assignment Your job in this assignment is to create a generic linked list.
ID: 3678885 • Letter: J
Question
Java Assignment
Your job in this assignment is to create a generic linked list. The linked list should have the ability to perform the following actions:
check if the list is empty
check the size of the list
add data to the list
add data to a specific (valid) location in the list
remove an item from the list based on position
remove an item from the list based on data value
removes all elements from the list
gets an item from the list based on the position
outputs the contents of the list
Use the dat file from the previous assignment (names.dat) to demonstrate effectiveness of the linked list you created.
Or you can simply use integers to display them in the list.
Explanation / Answer
public class LinkedList<T>
{
protected Node<T> head;
protected int size;
public LinkedList()
{
head = null;
size = 0;
}
public int size() {
return size;
}
public void add(T value)
{
head = addAtEnd(head, value);
size++;
}
private Node<T> addAtEnd(Node<T> node, T value)
{
if (node == null) {
return new Node<T>(value, null);
}
else if (node.getNext() == null)
{
node.setNext(new Node<T>(value, null));
} else {
addAtEnd(node.getNext(), value);
}
return node;
}
public void add2(T value)
{
if (head == null) {
head = new Node<T>(value, null);
}
else
{
Node<T> node = head;
while (node.getNext() != null)
{
node = node.getNext();
}
node.setNext(new Node<T>(value, null));
}
size++;
}
public void remove(int position) throws BadItemCountException
{
if ((position < 1) || (position > size)) {
throw new
BadItemCountException("invalid position " + position +
", only 1.." + size + " available");
}
if (position == 1) {
head = head.getNext();
} else {
Node<T> node = head;
for (int i = 2; i < position; i++) {
node = node.getNext();
}
node.setNext(node.getNext().getNext());
}
size--;
}
public String toString() {
return toString(head);
}
private String toString(Node<T> node) {
if (node == null) {
return "";
} else {
return node.getValue() + " " + toString(node.getNext());
}
}
public static void main(String[] args) {
LinkedList<String> list1 = new LinkedList<String>();
LinkedList<String> list2 = new LinkedList<String>();
System.out.println("list1 = '" + list1 + "', list2 = '" + list2 + "'");
System.out.println("list1.size() = " + list1.size() +
", list2.size() = " + list2.size());
list1.add("hello");
list1.add("world");
list2.add("How");
list2.add("are");
list2.add("you");
System.out.println("list1 = '" + list1 + "', list2 = '" + list2 + "'");
System.out.println("list1.size() = " + list1.size() +
", list2.size() = " + list2.size());
boolean caught = false;
try {
list2.remove(4);
} catch (BadItemCountException e)
{
caught = true;
}
if (! caught)
{
System.out.println("error: no exception for invalid remove");
System.out.println("list1 = '" + list1 +
"', list2 = '" + list2 + "'");
}
System.out.println("list1 = '" + list1 + "', list2 = '" + list2 + "'");
try {
list1.remove(1);
System.out.println("list1 = '" + list1 +
"', list2 = '" + list2 + "'");
list2.remove(2);
System.out.println("list1 = '" + list1 +
"', list2 = '" + list2 + "'");
list2.remove(2);
System.out.println("list1 = '" + list1 +
"', list2 = '" + list2 + "'");
} catch (Exception e) {
System.out.println("caught unexpected exception " + e +
", list1 = '" + list1 + ", list2 = " + list2);
}
System.out.println("list1.size() = " + list1.size() +
", list2.size() = " + list2.size());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.