Implement multiset using sorted linkedList - java

Hello I implemented a multiset using a linkedlist and I want to implement the multiset using sorted linkedlist. This is multiset abstract class.
import java.io.PrintStream;
public abstract class Multiset<T> {
/**
* Delimiter string for print operation.
*/
protected static final String printDelim = " | ";
public abstract void add(T item);
public abstract int search(T item);
public abstract void removeOne(T item);
public abstract void removeAll(T item);
public abstract void print(PrintStream out);
}
This is my implementation of linkedlist.
import java.io.PrintStream;
public class LinkedListMultiset<T> extends Multiset<T> {
protected Node mHead;
protected int mLength;
public LinkedListMultiset() {
// Implement me!
mHead = null;
mLength = 0;
}
public void add(T item) {
Node newNode = new Node((String) item);
if (mHead == null)
mHead = newNode;
else {
Node currNode = mHead;
Node parentNode = null;
while (currNode != null) {
if (currNode.getValue().
equals(newNode.getValue())) {
currNode.addCounter();
return;
}
parentNode = currNode;
currNode = currNode.getNext();
}
parentNode.setNext(newNode);
}
mLength++;
}
public int search(T item) {
Node currNode = mHead;
while (currNode != null) {
if (currNode.getValue().equals((String) item)) {
return currNode.getCounter();
}
currNode = currNode.getNext();
}
return 0;
}
public void removeOne(T item) {
Node currNode = mHead;
Node lastNode = null;
while (currNode != null) {
if (currNode.getValue().equals((String) item)) {
currNode.minusCounter();
if (currNode.getCounter() == 0) {
if (currNode == mHead)
mHead = currNode.getNext();
else
lastNode.setNext
(currNode.getNext());
mLength--;
}
return;
}
lastNode = currNode;
currNode = currNode.getNext();
}
}
public void removeAll(T item) {
Node currNode = mHead;
Node lastNode = null;
while (currNode != null) {
if (currNode.getValue().equals((String) item)) {
if (currNode == mHead)
mHead = currNode.getNext();
else
lastNode.setNext(currNode.getNext());
mLength--;
return;
}
lastNode = currNode;
currNode = currNode.getNext();
}
}
public void print(PrintStream out) {
Node currNode = mHead;
while (currNode != null) {
out.printf("%s | %d\n", currNode.getValue()
, currNode.getCounter());
currNode = currNode.getNext();
}
}
private class Node {
protected String mValue;
protected Node mNext;
int counter;
public Node(String value) {
mValue = value;
mNext = null;
counter = 1;
}
public void addCounter() {
counter++;
}
public void minusCounter() {
counter--;
}
public int getCounter() {
return counter;
}
public String getValue() {
return mValue;
}
public Node getNext() {
return mNext;
}
public void setValue(String value) {
mValue = value;
}
public void setNext(Node next) {
mNext = next;
}
}
}
I want to implement sorted linkedlist but I want to change my code as minimum as possible.

Related

Time out issue in java

I have been running this code and it seems like it times out. I'm not sure how to fix it as it doesn't show me an actual error message or anything like that. The first block of code is the class that the methods in the demo are coming from, and it seems like all the methods that are being called for stringList are just not working. When I test the same thing with an int list it works fine.
public class DoublyLinkedList<E> {
private static class Node<E> {
// Node Fields
private E element; //data
private Node<E> prev; //previous Node
private Node<E> next; //next Node
// Node Constructor
public Node(E e, Node<E> p, Node<E> n) {
this.element = e;
this.prev = p;
this.next = n;
}
// Node Methods
public E getElement() {
return element;
}
public Node<E> getPrev() {
return this.prev;
}
public Node<E> getNext() {
return this.next;
}
public void setPrev(Node<E> p) {
this.prev = p;
}
public void setNext(Node<E> n) {
this.next = n;
}
}
// DLinkedList Fields
private Node<E> header;
private Node<E> trailer;
int size;
// DLinkedList Constructor
public DoublyLinkedList() {
this.header = new Node<>(null, null, null);
this.trailer = new Node<>(null, this.header, null);
this.header.setNext(this.trailer);
}
// DLinkedList Methods
public int size() {
return this.size;
}
public E first() {
if (isEmpty()) {
return null;
}
return this.header.next.getElement();
}
public E last () {
if (isEmpty()) {
return null;
}
return this.trailer.prev.getElement();
}
public boolean isEmpty() {
return size == 0;
}
public void addFirst (E e) {
addBetween(e, this.header, this.header.getNext());
}
public void addLast (E e) {
addBetween(e, this.trailer.getPrev(), this.trailer);
}
private void addBetween(E e, Node<E> predecessor, Node<E> successor) {
Node<E> newest = new Node<>(e, predecessor, successor);
predecessor.setNext(newest);
successor.setPrev(newest);
this.size++;
}
public E removeFirst() {
if (this.isEmpty()) {
return null;
}
return this.remove(header.getNext());
}
public E removeLast() {
if (this.isEmpty()) {
return null;
}
return this.remove(trailer.getPrev());
}
public E remove(Node<E> e) {
e.next.setPrev(e.prev);
e.prev.setNext(e.next);
this.size--;
return e.getElement();
}
public String toString() {
StringBuilder sb = new StringBuilder("(");
Node<E> walk = this.header.next;
while (walk != this.trailer) {
sb.append(walk.element);
if (walk.next != this.trailer)
sb.append(", ");
walk = walk.next;
}
sb.append(")");
return sb.toString();
}
//DONE
public void add(int index, E element) {
Node<E> pred = header;
Node<E> succ = pred.getNext();
int count = 0;
while(succ != null) {
if(count == index) addBetween(element, pred, succ);
count++;
pred = pred.getNext();
succ = succ.getNext();
}
}
//DONE
public void add(E e) {
add(size, e);
}
//DONE
public void clear() {
while(!isEmpty()) {
removeFirst();
}
}
public E get(int index) {
if (isEmpty()) return null;
Node<E> current = header;
int count = 0;
while(current != null) {
if(count == index) return current.getElement();
count++;
current = current.getNext();
}
return null;
}
public E set(int index, E element) {
if(isEmpty()) return null;
Node<E> current = header;
E returnVal = null;
int count = 0;
while(current != null) {
if(count == index) {
if(count == 0) {
returnVal = get(0);
removeFirst();
add(0, element);
}
else if(count == size) {
returnVal = get(size);
removeLast();
add(size, element);
}
else {
returnVal = get(index);
remove(current);
add(index, element);
}
}
}
return returnVal;
}
}
package labs;
public class DoublyLinkedListDemo {
public static void main(String[] args) {
//testing methods on a String DoublyList
DoublyLinkedList<String> stringList = new DoublyLinkedList<>();
stringList.addFirst("Strawberry");
stringList.addFirst("Banana");
stringList.addFirst("Apple");
stringList.set(0, stringList.get(1));
System.out.println(stringList);
stringList.add(1, "Pear");
System.out.println(stringList);
stringList.add("Blueberry");
System.out.println(stringList);
System.out.println(stringList.get(1));
stringList.clear();
System.out.println(stringList);
System.out.println(stringList.set(0, stringList.get(1)));
System.out.println(stringList.get(0));
}
}

how to make an .add method which adding objects in a reference based list in alphabetically order

This is my user class and i found that i have to use the compareTo method but i need a method which adding in a Rb list.
There is an already existed add method and i have make a similar which order the users alphabetically.
import java.lang.Comparable;
public class LaptopUser implements Comparable<LaptopUser>
{
private String username;
private String password;
public LaptopUser(String username,String password)
{
this.username=username;
this.password=password;
}
public String getUsername(){
return username ;
}
public String getPass()
{
return password;
}
public String toString(){
return(username+","+password);
}
#Override
public int compareTo(LaptopUser n)
{
if(this.toString().compareTo(n.toString())>0)
{
return 1;
}
else if(this.toString().compareTo(n.toString())<0)
{
return -1;
}
return 0;
}
public boolean equals(LaptopUser p)
{
return(this.toString().equals(p.toString()));
}
}
public class ReferenceBasedList implements ListInterface
{
private ListNode head;
private ListNode tail;
int numItems;
public ReferenceBasedList()
{
head = tail = null;
numItems = 0;
}
public int size()
{
return numItems;
}
public boolean isEmpty()
{
return (numItems == 0);
}
public void removeAll()
{
head = tail = null;
numItems = 0;
}
private ListNode find(int index)
{
ListNode curr = head;
for (int skip = 1; skip < index; skip++)
curr = curr.getNext();
return curr;
}
public Object get(int index)
throws ListIndexOutOfBoundsException
{
if (index >= 1 && index <= numItems)
{
ListNode curr = find(index);
return curr.getItem();
}
else
{
throw new ListIndexOutOfBoundsException(
"List index out of bounds exception on get");
}
}
public void add(int index, Object newDataItem)
throws ListIndexOutOfBoundsException
{
if (index >= 1 && index <= numItems+1)
{
if ( index == 1 )
{
ListNode newNode = new ListNode(newDataItem, head);
head = newNode;
if (tail==null)
tail = head;
}
else if ( index==numItems+1 )
{
ListNode newNode = new ListNode(newDataItem);
tail.setNext(newNode);
tail = newNode;
}
else
{
ListNode prev = find(index-1);
ListNode newNode = new ListNode(newDataItem, prev.getNext());
prev.setNext(newNode);
}
numItems++;
}
else
{
throw new ListIndexOutOfBoundsException(
"List index out of bounds exception on add");
}
}
public void insert(Object newDataItem)
{
this.add(1,newDataItem);
}
public void append(Object newDataItem)
{
this.add(numItems+1,newDataItem);
}
public Object showFront()
{
return this.get(1);
}
public Object showLast()
{
return this.get(numItems);
}
public void remove(int index)
throws ListIndexOutOfBoundsException
{
if (index >= 1 && index <= numItems)
{
if (index == 1)
{
head = head.getNext();
if (head == null)
tail = null;
}
else
{
ListNode prev = find(index-1);
ListNode curr = prev.getNext();
prev.setNext(curr.getNext());
if (index == numItems)
tail = prev;
}
numItems--;
}
else
{
throw new ListIndexOutOfBoundsException(
"List index out of bounds exception on remove");
}
}
public boolean exists(Object dataItem)
{
for (ListNode tmp=head; tmp!=null; tmp=tmp.getNext())
if (tmp.getItem().equals(dataItem))
return true;
return false;
}
public Object removeLast() throws ListException
{
if (isEmpty())
throw new ListException("The linked list is empty");
else
{
Object lastDataItem = tail.getItem();
if (head == tail)
head = tail = null;
else
{
ListNode tmp = head;
while (tmp.getNext().getNext() != null)
tmp = tmp.getNext();
tail = tmp;
tail.setNext(null);
}
numItems--;
return lastDataItem;
}
}
public Object removeFront() throws ListException
{
if (isEmpty())
throw new ListException("The linked list is empty");
else
{
Object frontDataItem = head.getItem();
head = head.getNext();
if (head == null)
tail = null;
numItems--;
return frontDataItem;
}
}
}
You can use the Collections.binarySearch method to find at what index a LaptopUser should be added to a List in order for it to be alphabetically ordered.
SortedList:
public class SortedList implements Iterable<LaptopUser> {
public List<LaptopUser> users = new ArrayList<LaptopUser>();
public void add(LaptopUser user) {
int index = Collections.binarySearch(users, user);
if (index < 0)
users.add(-index - 1, user);
else
users.add(index, user);
}
#Override
public Iterator<LaptopUser> iterator() {
return users.iterator();
}
Example Code:
public class LaptopUser implements Comparable<LaptopUser> {
public String username;
public String password;
public LaptopUser(String username, String password) {
this.username = username;
this.password = password;
}
#Override
public int compareTo(LaptopUser o) {
return toString().compareTo(o.toString());
}
#Override
public String toString() {
return username.concat(password);
}
}
public Sorted() {
LaptopUser a =new LaptopUser("a", "password");
LaptopUser b =new LaptopUser("b", "password");
LaptopUser c =new LaptopUser("c", "password");
SortedList list = new SortedList();
list.add(c);
list.add(a);
list.add(b);
for(LaptopUser user : list)
System.out.println(user);
}
public static void main(String[] args) {
new Sorted();
}
Output:
apassword
bpassword
cpassword

How I have to return in "next" method of Iterator<E>?

in method "next" implemented in Iterator there is a mistake. I cannot return 'e' value, because Java says it is Object type. I don't understand why. Because in Elem class if I give for instance Point object, it must return it. But I don't know generics well, maybe someone could explain plz. Thnx)
package Tutorial3;
import java.util.Iterator;
public class MyLinkedList<E> implements Iterable<E> {
Elem<E> head;
Elem<E> tail;
public MyLinkedList() {
head = null;
tail = null;
}
public void add(E e) {
Elem<E> newElem = new Elem<E>(e);
if (head == null) {
tail.setNext(newElem);
head = newElem;
tail = newElem;
} else {
tail.setNext(newElem);
tail = newElem;
}
}
public void addToHead(E e) {
Elem<E> newElem = new Elem<E>(e);
newElem.setNext(head);
head = newElem;
}
public void addToTail(E e) {
add(e);
}
public void removeFirstValue() {
head = head.getNext();
}
public void removeLastValue() {
Elem<E> cursor;
cursor = head;
while (cursor.getNext() != tail) {
cursor = cursor.getNext();
}
tail = cursor;
tail.setNext(null);
}
public E get(int index) {
if ((index + 1) > size()) {
throw new IndexOutOfBoundsException("Index: " + index + " Size: " + size());
} else {
Elem<E> cursor;
cursor = head;
int i = 0;
if (i == index) {
return cursor.getE();
} else {
while (i != index) {
cursor = cursor.getNext();
i++;
}
return cursor.getE();
}
}
}
public int size() {
Elem<E> cursor;
cursor = head;
int size = 0;
while (cursor != null) {
size++;
cursor = cursor.getNext();
}
return size;
}
#Override
public Iterator<E> iterator() {
return new MyLinkedListIterator<E>();
}
private class MyLinkedListIterator<T> implements Iterator<T> {
private Elem elem = head;
#Override
public boolean hasNext() {
return elem.getNext() != null;
}
#Override
public T next() {
return elem.getNext().getE(); //here is mistake: can't return e
//required : T, Found: Object
}
}
}
package Tutorial3;
public class Elem<E> {
private E e;
private Elem<E> next;
public Elem(E e) {
this.e = e;
}
public Elem(E e, Elem<E> next) {
this.e = e;
this.next = next;
}
public E getE() {
return e;
}
public void setE(E e) {
this.e = e;
}
public Elem<E> getNext() {
return next;
}
public void setNext(Elem<E> next) {
this.next = next;
}
}
It should be
private class MyLinkedListIterator implements Iterator<E> {
private Elem<E> elem = head;
#Override
public boolean hasNext() {
return elem.getNext() != null;
}
#Override
public E next() {
return elem.getNext().getE();
}
}
Your MyLinkedListIterator type should inherit E from the outer MyLinkedList<E> type, instead of declaring its own element type T, and then you compounded that by using a raw Elem type instead of Elem<E>.

Endless iterator loop on doubly linked list using Java

I have created a doubly-linked list and implemented my own iterator.
However, I have done something wrong and my iterator results in endless loop.
Have been struggling to find the error, so any feedback is much appreciated. Thanks in advance. I apologise for the wall of code, I think the error lies within my iterator of the way I have created my Node class.
My Code:
import java.util.Iterator;
import java.util.NoSuchElementException;
public class MyDoubleEndedLinkedList<T extends Comparable<T>> implements
Iterable<T> {
// initialising Nodes including the two sentinal nodes
private Node<T> head;
private Node<T> tail;
private Node<T> current;
private int currentsize;
MyDoubleEndedLinkedList() {
head = new Node<T>();
tail = new Node<T>();
head.setNext(tail);
tail.setPrevious(head);
current = head;
currentsize = 0;
// Methods used to help loop and iterate through the list
public boolean isEmpty() {
return (current == head && current == tail);
}
public boolean endList() {
return (current != tail);
}
public void resetCurrent() {
current = head;
}
public void nextCurrent() {
current = current.getNext();
}
public T getCurrent() {
return current.getData();
}
public int size() {
return this.currentsize;
}
#Override
public Iterator<T> iterator() {
return new LinkedListIterator<T>();
}
// Node class for doublyLinkedList
public class Node<E> {
private Node<E> previous;
private Node<E> next;
private E data;
Node() {
previous = null;
next = null;
data = null;
}
Node(Node<E> newPrevious, Node<E> newNext, E newData) {
previous = newPrevious;
next = newNext;
data = newData;
}
// set previous node
public void setPrevious(Node<E> newPrevious) {
previous = newPrevious;
}
// set Next node
public void setNext(Node<E> newNext) {
next = newNext;
}
public void setData(E newData) {
data = newData;
}
public Node<E> getPrevious() {
return previous;
}
public Node<E> getNext() {
return next;
}
public E getData() {
return data;
}
}
class LinkedListIterator<E> implements Iterator<T> {
private Node<T> current;
private Node<T> previous;
private Node<T> previous2;
private boolean removeCalled;
public LinkedListIterator() {
current = head;
previous = null;
previous2 = null;
removeCalled = false;
}
public boolean hasNext() {
return (current != null);
}
public T next() {
if (hasNext()) {
T temp = current.getData();
previous2 = previous;
previous = current;
current = current.next;
removeCalled = false;
return temp;
}
throw new NoSuchElementException();
}
public void remove() {
if (previous == null || removeCalled) {
throw new IllegalStateException();
}
if (previous2 == null) {
head = current;
} else {
previous2.setNext(current);
previous = previous2;
}
removeCalled = true;
throw new UnsupportedOperationException();
}
}}
So i can't find the bug in your code but here is a simpler implementation of a basic linked list in Java. If you show me how you're adding elements to the list it would be easier to track down.
import java.util.Iterator;
public class MyLinkedList<T> implements Iterable<T> {
private Node head = null;
private Node tail = null;
public static void main(String[] args) {
MyLinkedList<String> li = new MyLinkedList<>();
li.add("1");
li.add("2");
li.add("3");
li.add("4");
li.add("5");
for (String s : li) {
System.out.println(s);
}
}
public void add(T data) {
if (head == null) {
head = new Node(data, null);
tail = head;
} else {
Node n = new Node(data, tail);
tail.next = n;
tail = n;
}
}
#Override
public Iterator<T> iterator() {
return new Iterator<T>() {
Node current = head;
#Override
public boolean hasNext() {
return current != null;
}
#Override
public T next() {
T data = current.data;
current = current.next;
return data;
}
#Override
public void remove() {
}
};
}
class Node {
final T data;
Node prev = null;
Node next = null;
Node(T data, Node prev) {
this.data = data;
this.prev = prev;
}
}
}

How to sorting list interface

I am having problem with sorting patient list based on seriousness and arrival time. Can take a look what wrong with my code? See the LList.java, the SortPatient method is correct?
ListInterface.java
public interface ListInterface<T> {
public boolean add(T newEntry);
public boolean add(int newPosition, T newEntry);
public T remove(int givenPosition);
public void clear();
public boolean replace(int givenPosition, T newEntry);
public T getEntry(int givenPosition);
public boolean contains(T anEntry);
public int getLength();
public boolean isEmpty();
public boolean isFull();
public int getPosition(T anObject);
public void SortPatient(ListInterface<Patient> patientList);
}
Llist.java
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
public class LList<T> implements ListInterface<T> {
private Node firstNode; // reference to first node
private int length; // number of entries in list
public LList() {
clear();
}
public LList(T[] arr) {
clear();
firstNode = new Node(arr[0], null);
Node p = firstNode;
for (int index = 1; index < arr.length; index++) {
p.next = new Node(arr[index], null);
p = p.next;
//index must start with 0
//or add(arr[index]);
}
length = arr.length;
}
public int getPosition(T anObject) {
Node currentNode = firstNode;
int position = 0;
while (currentNode != null) {
position++;
if (currentNode.data.equals(anObject)) {
return position;
}
currentNode = currentNode.next;
}
return -1;
}
public final void clear() {
firstNode = null;
length = 0;
}
public boolean add(T newEntry) {
Node newNode = new Node(newEntry);
// create the new node
if (isEmpty()) // if empty list
{
firstNode = newNode;
} else { // add to end of nonempty list
Node currentNode = firstNode; // traverse linked list with p pointing to the current node
while (currentNode.next != null) { // while have not reached the last node
currentNode = currentNode.next;
}
currentNode.next = newNode; // make last node reference new node
}
length++;
return true;
}
public boolean add(int newPosition, T newEntry) { // OutOfMemoryError possible
boolean isSuccessful = true;
if ((newPosition >= 1) && (newPosition <= length + 1)) {
Node newNode = new Node(newEntry);
if (isEmpty() || (newPosition == 1)) { // case 1: add to beginning of list
newNode.next = firstNode;
firstNode = newNode;
} else { // case 2: list is not empty and newPosition > 1
Node nodeBefore = firstNode;
for (int i = 1; i < newPosition - 1; ++i) {
nodeBefore = nodeBefore.next; // advance nodeBefore to its next node
}
newNode.next = nodeBefore.next; // make new node point to current node at newPosition
nodeBefore.next = newNode; // make the node before point to the new node
}
length++;
} else {
isSuccessful = false;
}
return isSuccessful;
}
public T remove(int givenPosition) {
T result = null; // return value
if ((givenPosition >= 1) && (givenPosition <= length)) {
if (givenPosition == 1) { // case 1: remove first entry
result = firstNode.data; // save entry to be removed
firstNode = firstNode.next;
} else { // case 2: givenPosition > 1
Node nodeBefore = firstNode;
for (int i = 1; i < givenPosition - 1; ++i) {
nodeBefore = nodeBefore.next; // advance nodeBefore to its next node
}
result = nodeBefore.next.data; // save entry to be removed
nodeBefore.next = nodeBefore.next.next; // make node before point to node after the
} // one to be deleted (to disconnect node from chain)
length--;
}
return result; // return removed entry, or
// null if operation fails
}
public boolean replace(int givenPosition, T newEntry) {
boolean isSuccessful = true;
if ((givenPosition >= 1) && (givenPosition <= length)) {
Node currentNode = firstNode;
for (int i = 0; i < givenPosition - 1; ++i) {
// System.out.println("Trace| currentNode.data = " + currentNode.data + "\t, i = " + i);
currentNode = currentNode.next; // advance currentNode to next node
}
currentNode.data = newEntry; // currentNode is pointing to the node at givenPosition
} else {
isSuccessful = false;
}
return isSuccessful;
}
public T getEntry(int givenPosition) {
T result = null;
if ((givenPosition >= 1) && (givenPosition <= length)) {
Node currentNode = firstNode;
for (int i = 0; i < givenPosition - 1; ++i) {
currentNode = currentNode.next; // advance currentNode to next node
}
result = currentNode.data; // currentNode is pointing to the node at givenPosition
}
return result;
}
public boolean contains(T anEntry) {
boolean found = false;
Node currentNode = firstNode;
while (!found && (currentNode != null)) {
if (anEntry.equals(currentNode.data)) {
found = true;
} else {
currentNode = currentNode.next;
}
}
return found;
}
public int getLength() {
return length;
}
public boolean isEmpty() {
boolean result;
if (length == 0) {
result = true;
} else {
result = false;
}
return result;
}
public boolean isFull() {
return false;
}
public String toString() {
String outputStr = "";
Node currentNode = firstNode;
while (currentNode != null) {
outputStr += currentNode.data;
currentNode = currentNode.next;
}
return outputStr;
}
private class Node {
private T data;
private Node next;
private Node(T data) {
this.data = data;
this.next = null;
}
private Node(T data, Node next) {
this.data = data;
this.next = next;
}
} // end Node
public void SortPatient(ListInterface<Patient> patientList) {
if (patientList == null) {
return;
}
boolean swapped;
Patient patient;
do {
swapped = false;
patient = head;
while (patientList.next != null) {
if (patient.compareTo(patientList.next) < 1) {
swap(patientList.current, patientList.next);
swapped = true;
}
patient = patientList.next;
}
} while (swapped);
}
}
Patient.java
import java.util.Date;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import static javafx.scene.input.KeyCode.T;
public class Patient implements Comparable<Patient> {
String patientId;
String patientName;
String patientGender;
String patientIcNumber;
String patientContactNumber;
Date date;
int seriousness;
static int count = 0;
public Patient() {
}
public Patient(String patientId, String patientName, String patientGender, String patientIcNumber, String patientContactNumber, Date date, int seriousness) {
this.patientId = patientId;
this.patientName = patientName;
this.patientGender = patientGender;
this.patientIcNumber = patientIcNumber;
this.patientContactNumber = patientContactNumber;
this.date = date;
this.seriousness = seriousness;
count++;
}
public String getPatientId() {
return patientId;
}
public void setPatientId(String patientId) {
this.patientId = patientId;
}
public String getPatientName() {
return patientName;
}
public void setPatientName(String patientName) {
this.patientName = patientName;
}
public String getPatientGender() {
return patientGender;
}
public void setPatientGender(String patientGender) {
this.patientGender = patientGender;
}
public String getPatientIcNumber() {
return patientIcNumber;
}
public void setPatientIcNumber(String patientIcNumber) {
this.patientIcNumber = patientIcNumber;
}
public String getPatientContactNumber() {
return patientContactNumber;
}
public void setPatientContactNumber(String patientContactNumber) {
this.patientContactNumber = patientContactNumber;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public int getSeriousness() {
return seriousness;
}
public void setSeriousness(int seriousness) {
this.seriousness = seriousness;
}
public int getCount() {
return count;
}
#Override
public String toString() {
return patientId + " " + patientName + " " + patientGender + " " + patientIcNumber + " " + patientContactNumber + " " + date + " " + seriousness + "\n";
}
#Override
public int compareTo(Patient t) {
int patientSeriouness = t.seriousness;
Date arrival = t.date;
if (this.seriousness == patientSeriouness) {
return this.date.compareTo(arrival);
} else {
return Integer.compare(this.seriousness, patientSeriouness);
}
}
}
Couple of issues in your code (I just went through add,getentry and sort method):
In getEntry method, Remember index always start with 0 and go after till length - 1, so you should change our method to:
public T getEntry(int givenPosition) {
T result = null;
if ((givenPosition >= 0) && (givenPosition < length)) {
Node currentNode = firstNode;
for (int i = 0; i < givenPosition; ++i) {
currentNode = currentNode.next; // advance currentNode to next node
}
result = currentNode.data; // currentNode is pointing to the node at givenPosition
}
return result;
}
Change your interface and class implementation to remove passing on the ListInterface reference and your implementation should look like:
public void SortPatient() {
if (firstNode == null) {
return;
}
boolean swapped;
Node patient;
do {
swapped = false;
patient = firstNode;
while (patient.next != null) {
if (patient.data.compareTo(patient.next.data) > 0) {
swap(patient, patient.next);
swapped = true;
}
patient = patient.next;
}
} while (swapped);
}
private void swap(Node patient, Node nextPatient) {
T temp = patient.data;
patient.data = nextPatient.data;
nextPatient.data = temp;
}
Your compare to method in Patient class needs some improvements and it should be as below:
#Override
public int compareTo(Patient t) {
int patientSeriouness = t.seriousness;
Date arrival = t.date;
if (this.seriousness == patientSeriouness) {
return this.date.compareTo(arrival);
} else {
return ((Integer)this.seriousness).compareTo(patientSeriouness);
}
}
Last but not the least, your class definition LList should take generics of type Comparable (As you are using compareTo method), so you need to mandate that and it should be defined as
public class LList<T extends Comparable<T>> implements ListInterface<T>

Categories