это то, что я пытался ...как я могу сортировать связанный список в порядке возрастания
это только обмените первую ссылку ... как я могу решить эту проблему, чтобы отсортировать весь связанный список ????
LinkedList класс
public class LinkedList {
private Link first;
public void LinkList() {
first = null;
}public Link find(int key) {
Link current = first;
while (current.iData != key) {
if (current.next == null) {
return null;
} else {
current = current.next;
}
}
return current;
}
public void insertFirst(int idata, String sdata) {
Link nl1 = new Link(idata, sdata);
nl1.next = first;
first = nl1;
}
public Link deleteFirst() {
Link temp = first;
first = first.next;
return temp;
}
public void displayList() {
System.out.println("List : ");
Link current = first;
while (current != null) {
current.displayLink();
current = current.next;
}
System.out.println("");
}
public Link delete(int key) {
Link current = first;
Link previous = first;
while (current.iData != key) {
if (current.next == null) {
return null;
} else {
previous = current;
current = current.next;
}
}
if (current == first) {
first = first.next;
} else {
previous.next = current.next;
}
return current;
}
public void insertmidl(int key, int idata, String sdata) {
Link nl = new Link(2, "name2");
Link current = first;
Link previous = first;
while (current.iData != key) {
if (current.next == null)
{
System.out.println("");
} else {
previous = current;
current = current.next;
}
}
if (current == first) {
nl.next = current.next;
current.next = nl;
}
previous.next = nl;
nl.next = current;
}
public void update(int key, int i, String n) {
Link tobeupdated = find(key);
tobeupdated.iData = i;
tobeupdated.sData = n;
}
public void sort(){
Link current = first;
if(current.iData > current.next.iData) {
Link temp = current.next;
current.next = current.next.next;
temp.next = current;
first = temp;
}
}
ссылка класс
public class Link {
public int iData;
public String sData;
public Link next;
public Link(int id,String sd)
{
iData =id;
sData =sd;
next = null;
}
public void displayLink()
{
System.out.println(iData+""+sData);
}
}
есть кто-нибудь, кто может мне помочь ?? ................................................. ................................
Создайте BST из связанного списка. Выполнение обхода порядка. Создайте LinkedList на основе обхода InOrder. – Sandeep