TEMPLATED SORTED LIST My assignment is to write a TemplatedSortedList class (in
ID: 3824024 • Letter: T
Question
TEMPLATED SORTED LIST
My assignment is to write a TemplatedSortedList class (in TemplatedSortedList.h) that implements a sorted list (linked- or array-based). Additionally, you get to template the class and provide overloaded operators such that the driver code compiles and produces the correct output.
The driver code processes the following commands:
A (admits patient NAME)
D (discharges patient NAME if NAME is in the list, silently continues otherwise)
F (prints patient NAME if found in the list)
V (visits all patients and displays their names in list order)
Driver Code/ TemplatedSortedList-main.cpp.
INPUT File
OUTPUT
IF YOU NEED MORE INFORMATION ABOUT THIS PROJECT PLEASE LET ME KNOW
PLEASE HELP ME I AM NOT AT ALL GETTING HOW TO START THIS PROJECT.
Explanation / Answer
import java.util.*;
public class SortedLinkedList< E> extends LinkedList<E>
{
private Link<E> first;
private Link<E> last;
public SortedLinkedList()
{
//super();
first = null;
last = null;
}
/
private class Link<E>
{
public Comparable<E> data;
public Link next;
}
public boolean add(E obj)
{
Link newLink = new Link();
newLink.data = (Comparable<E>)obj;
if (first == null)
{
first = newLink;
last = newLink;
return true;
}
if (newLink.data.compareTo(first.data) < 0)
{
//newLink.data = obj;
newLink.next = first;
first = newLink;
return true;
}
if (newLink.data.compareTo(last.data) > 0)
{
//newLink.data = obj;
last.next = newLink;
last = newLink;
return true;
}
if (newLink.data.compareTo(first.data) >= 0 && newLink.data.compareTo(last.data) <= 0)
{
//newLink.data = obj;
Link current = first.next;
Link previous = first;
while (newLink.data.compareTo(current.data) <= 0)
{
previous = current;
current = current.next;
}
previous.next = newLink;
newLink.next = current;
}
return true;
}
public static void main (String[] args)
{
LinkedList<Integer> list = new SortedLinkedList<Integer>();
list.add(4);
list.add(5);
list.add(10);
list.add(9);
//list.add(5);
ListIterator<Integer> iterator = list.listIterator();
while (iterator.hasNext())
{
System.out.println(iterator.next());
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.