Ich versuche Insertion sort zu verwenden, um verknüpfte Liste von Buch alphabetisch nach Titel zu sortieren.Insertion Sortierung mit verknüpften Liste für Buch-Objekte
, was ich bisher getan haben:
public void insertSorted(Book book){
if(books.getfirst()==null)
books.addFirst(book); //books is the LinkedList name
Node<Book> current =books.getfirst();
for(int i=0; i<books.getSize(); i++){
if(book.getTitle().compareToIgnoreCase(current.element.getTitle())<=0){
books.add(book, i);
}
}
die Add-Methode in VerketteteListe:
public void add(Object x,int index){
if(index==0)addFirst(x);
else if(index>=getSize())addLast(x);
else{
Node current=first;
for(int i=0; i<index-1;i++)
current=current.next;
Node temp = new Node(x);
temp.next=current.next;
current.next=temp;
count++;
}
}
was genau mache ich falsch?
In der for-Schleife von insertSorted sollten Sie gegen Buch [i] überprüfen. Nichts in der if-Anweisung hängt von i ab. –
Wiederhole das erste Buch zweimal, stimmt etwas nicht mit der Schleife? – Nicky