Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me with the driver program and the display method. Should replicate the output. Assignment: Write an implementation of the LinkedOrderedList class. LinkedListDriver package

Please help me with the driver program and the display method. Should replicate the output.

Assignment: Write an implementation of the LinkedOrderedList class.

LinkedListDriver

package jsjf;

import java.util.Scanner;

public class LinkedListDriver {public static void main(String[] args) {Scanner input=new Scanner(System.in); LinkedListlist=new LinkedList(); int menu = 0; do {System.out.println(" List menu selection 1.Add element 2.Remove element 3.Head 4.Display 5.Exit");

System.out.print("Enter your choice: "); menu=Integer.parseInt(input.next()); switch(menu) { case 1: break; case 2: break; case 3: break; case 4:

break;

} } while(menu!=5); //iterates until 5 (Exit) is entered input.close(); } }

LinearNode.java

package jsjf;

public class LinearNode {private LinearNode next; private E element;

public LinearNode() {next = null; element = null; }

public LinearNode(E elem) {next = null; element = elem; }

public LinearNode getNext() {return next; }

public void setNext(LinearNode node) {next = node; }

public E getElement() {return element; }

public void setElement(E elem) {element = elem; } }

LinkedList.java

package jsjf;

import jsjf.exceptions.*; import java.util.*;

public abstract class LinkedList implements ListADT, Iterable { protected int count; protected LinearNode head, tail; protected int modCount;

public LinkedList() { count = 0; head = tail = null; modCount = 0; }

public T removeFirst() throws EmptyCollectionException { if (isEmpty()) { System.out.println("The collection is empty!"); } else { LinearNode result = head; head = head.getNext(); if (head == null) tail = null; count--; modCount++; return result.getElement(); } return null; }

public T removeLast() throws EmptyCollectionException { if (isEmpty()) { System.out.println("The collection is empty!"); } else { LinearNode previous = null; LinearNode current = head;

while (current.getNext() != null) { previous = current; current = current.getNext(); } LinearNode result = tail; tail = previous; if (tail == null) // only one element in list head = null; else tail.setNext(null); count--; modCount++; return result.getElement(); } return null; }

public T remove(T targetElement) throws EmptyCollectionException, ElementNotFoundException { if (isEmpty()) throw new EmptyCollectionException("LinkedList");

boolean found = false; LinearNode previous = null; LinearNode current = head;

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(); }

public T first() throws EmptyCollectionException { if (isEmpty()) { System.out.println("The collection is empty!"); } else { return head.getElement(); } return null; }

public T last() throws EmptyCollectionException { if (isEmpty()) { System.out.println("The collection is empty!"); } else { return tail.getElement(); } return null; }

public boolean contains(T targetElement) throws EmptyCollectionException { if (isEmpty()) { System.out.println("The collection is empty!"); } else { boolean found = false; LinearNode current = head;

while (current != null && !found) if (targetElement.equals(current.getElement())) found = true; else current = current.getNext(); return found; } return false;

}

public boolean isEmpty() { return (count == 0); }

public int size() { return count; }

public String toString() { LinearNode current = head; String result = "";

while (current != null) { result = result + current.getElement() + " "; current = current.getNext(); } return result; }

public Iterator iterator() { return new LinkedListIterator(); }

private class LinkedListIterator implements Iterator { private int iteratorModCount; // the number of elements in the collection private LinearNode current; // the current position

public LinkedListIterator() { current = head; iteratorModCount = modCount; }

public boolean hasNext() throws ConcurrentModificationException { if (iteratorModCount != modCount) throw new ConcurrentModificationException();

return (current != null); }

public T next() throws ConcurrentModificationException { if (!hasNext()) throw new NoSuchElementException();

T result = current.getElement(); current = current.getNext(); return result; }

public void remove() throws UnsupportedOperationException { throw new UnsupportedOperationException(); } }

}

LinkedOrderedList.java

package jsjf;

import jsjf.exceptions.*;

public class LinkedOrderedList extends LinkedList implements OrderedListADT { public LinkedOrderedList() { super(); }

public void add(T element) { if (!(element instanceof Comparable)) { throw new NonComparableElementException("LinkedOrderedList"); } else { Comparable comparableElement = (Comparable)element;

LinearNode current = head; LinearNode previous = null; LinearNode newNode = new LinearNode(element); boolean found = false; if (isEmpty()) // list is empty { head = newNode; tail = newNode; } else if (comparableElement.compareTo(head.getElement()) = 0)// element goes at tail { tail.setNext(newNode); tail = newNode; } else // element goes in the middle { while ((comparableElement.compareTo(current.getElement()) > 0)) { previous = current; current = current.getNext(); } newNode.setNext(current); previous.setNext(newNode); } count++; modCount++; } } }

ListADT.java

package jsjf;

import java.util.Iterator;

public interface ListADT extends Iterable {public T removeFirst();

public T removeLast();

public T remove(T element);

public T first();

public T last();

public boolean contains(T target);

public boolean isEmpty();

public int size();

public Iterator iterator();

public String toString(); }

OrderedListADT.java

package jsjf;

public interface OrderedListADT extends ListADT {public void add(T element); }

ElementNotFoundException.java

package jsjf.exceptions;

public class ElementNotFoundException extends RuntimeException {public ElementNotFoundException (String collection) {super ("The target element is not in this " + collection); } }

EmptyCollectionException.java

package jsjf.exceptions;

public class EmptyCollectionException extends RuntimeException {public EmptyCollectionException(String collection) {super("The " + collection + " is empty."); } }

NonComparableElementException.java

package jsjf.exceptions;

public class NonComparableElementException extends RuntimeException {public NonComparableElementException (String collection) {super ("The " + collection + " requires Comparable elements."); } }

Sample Output

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

List Menu Selections 1.add element 2.remove element 3. head element 4.display 5. Exit Enter your choice: 1 Enter element: Mary List Menu Selections 1.add element 2.remove element 3. head element 4.display 5. Exit Enter your choice: 3 Element head: Mary List Menu Selections 1. add element 2. remove element 3. head element 4.display 5. Exit Enter your choice: 4 1. Mary List Menu Selections 1.add element 2. remove element 3. head element 4.display 5.Exit Enter your choice: 1 Enter element: James List Menu Selections 1. add element 2.remove element 3. head element 4.display 5.Exit Enter your choice: 3 Element head: James List Menu Selections 1. add element 2.remove element 3. head element 4.display 5. Exit Enter your choice: 4 1. James 2. Mary List Menu Selections 1. add element 2.remove element 3. head element 4.display 5. Exit Enter your choice: 1 Enter element: Nancy List Menu Selections 1. add element 2.remove element 3. head element 4.display 5. Exit Enter your choice: 4 1. James 2. Mary 3. Nancy List Menu Selections 1. add element 2.remove element 3. head element 4. display 5. Exit Enter your choice: 1 Enter element: Karen List Menu Selections 1. add element 2.remove element 3. head element 4.display 5. Exit Enter your choice: 1. James 2. Karen 3. Mary 4. Nancy List Menu Selections 1. add element 2.remove element 3. head element 4. display 5. Exit Enter your choice: 1 Enter element: Aaron List Menu Selections 1. add element 2.remove element 3. head element 4.display 5. Exit Enter your choice: 3 Element @head: Aaron List Menu Selections 1. add element 2.remove element 3. head element 4.display 5. Exit Enter your choice: 4 1. Aaron 2. James 3. Karen 4. Mary 5. Nancy Enter your choice: 2 Enter element: Aaron Aaron removed. List Menu Selections 1. add element 2. remove element 3. head element 4.display 5. Exit Enter your choice: 1. James 2. Karen 3. Mary 4. Nancy

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

150% of $60 is what amount?

Answered: 1 week ago