-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortedLinkedList.java
46 lines (35 loc) · 1.03 KB
/
SortedLinkedList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.*;
/** This method extends from LinkedList and implements comparable
* Also, any object is stored here, will either implement comparable, or is extending from class which implements comparable.
* @author b1014963, Abdul Al-Faraj
* @version 12/10/2012
*
*/
@SuppressWarnings("serial")
public class SortedLinkedList<E extends Comparable<? super E>> extends LinkedList<E> {
/**
*
*/
//private static final long serialVersionUID = 1L;
ListIterator<E> iter = this.listIterator();
public void insert(E obj)
{
ListIterator<E> iter = this.listIterator();
if (!iter.hasNext()){
iter.add(obj);
//THIS IS THE FIRST CASE, WHERE WE DON'T HAVE ANY ELEMENT
}else {
E elem=iter.next();
int comparison=elem.compareTo(obj); // to save the comparison between the two objects
while (iter.hasNext() && comparison<0)
{
elem=iter.next();
comparison= elem.compareTo(obj);
}
if(comparison>0){
iter.previous();
}
iter.add(obj);
}
}
}