Итак, у меня есть класс под названием List
и класс с именем SortedList
, который наследует класс List. Также есть класс, называемый узлом. Я создал другой класс, который содержит метод печати.Как реализовать отсортированный список
Но каждый раз, когда я вставляю три имени, и я вызываю метод печати, он только печатает фамилию, которую я вставил. Поэтому мой вопрос: делает ли этот код отсортированным списком? И если да, то почему он печатает только фамилию?
классСписок:
public class List {
protected Node head;
protected int length;
public void list()
{
head=null;
length=0;
}
public boolean isEmpty()
{
return head==null;
}
public Node insert(Item a)
{
length++;
head=new Node(a, head);
return head;
}
список сортировки класс:
public class SortList extends List {
private Node head;
public SortList()
{
this.head=null;
}
public Node getFirst()
{
return head;
}
public Node Insert(Item newitem)
{
Node node = new Node(newitem);
Node previous = null;
Node current = head;
while(current!=null && current.getValue().less(newitem))
{
previous=current;
current=current.getNext();
}
if(previous==null)
{
head=node;
}
else
{
previous.setNext(node);
node.setNext(current);
}
return head;
}
public void printlist()
{
Node current = head; //ΑΡΧΗ ΤΗΣ ΛΙΣΤΑΣ.
while(current!=null)
{
current.print();
current = current.getNext();
}
}
Node Класс:
public class Node {
private Item info;
private Node next;
public Node(Item dat)
{
info=dat;
}
public Node (Item dat, Node b)
{
info=dat;
next=b;
}
public Item getValue()
{
return info;
}
public void setNext(Node a)
{
next=a;
}
public Node getNext()
{
return next;
}
public void print()
{
info.print();
}
}
Я сделал, как вы просили. – Maria
@ ᴳᵁᴵᴰᴼ Вы ... должны ... использовать ... Python ...! Не ... JAVA! – Zizouz212
Почему вы не можете использовать 'Collections.sort()' при каждом изменении содержимого списка? –