Adrian
Posts: 1
Nickname: watts
Registered: Feb, 2003
Sorting a linked list
Posted: Feb 8, 2003 11:42 AM
Advertisement
Can someone help me with my sort method? public LinkedList sort(LinkedList unsorted){ if(unsorted == null)throw new IllegalArgumentException("unsorted LinkedList cannot be null"); if(unsorted.isEmpty() == true)throw new IllegalArgumentException("cannot sort an emty list"); //private temp varianles LinkedList sortedList = new LinkedList(); LinkedListIterator cursor1 = unsorted.zeroth(); LinkedListIterator cursor2 = unsorted.first(); cursor1.advance(); cursor2.advance(); Comparable maxStud = null; Comparable nextStud; while(unsorted.isEmpty() != true){ while(cursor2.isValid() == true){ maxStud = (Comparable)cursor1.retrieve(); nextStud = (Comparable)cursor2.retrieve(); if((maxStud.compareTo(nextStud)) == 0){ System.out.println("comparing: "+maxStud+" with :"+nextStud); //they are both the same, advance cursor1 cursor2.advance(); } else if((maxStud.compareTo(nextStud))==1){ System.out.println("comparing: "+maxStud+" with :"+nextStud); //item is larger cursor1= cursor2; cursor2.advance(); } else if((maxStud.compareTo(nextStud))==-1){ System.out.println("comparing: "+maxStud+" with :"+nextStud); cursor2.advance(); } } //if(cursor2.isValid() == false){ sortedList.insert(maxStud,sortedList.zeroth());//need to see how ti find the previos System.out.println("max student was: "+maxStud); unsorted.remove(maxStud); //System.out.println("sorted list:"); //printList(unsorted); cursor1 = unsorted.zeroth(); cursor2 = unsorted.first(); cursor1.advance(); cursor2.advance(); //} } return sortedList; }