使用冒泡排序的链表排序
Linked List Sorting Using Bubble Sort
我正在尝试使用气泡对 link 列表进行排序 sort.But 我写的算法没有 work.Can 有人帮助我吗???
还有一个linkclass。
Link class
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);
}
}
LinkKist class 包括排序算法。
public class LinkedList {
private Link first;
public void LinkList() {
first = null;
}
public void insertFirst(int idata, String sdata) {
Link nl1 = new Link(idata, sdata);
nl1.next = first;
first = nl1;
}
public void displayList() {
System.out.println("List : ");
Link current = first;
while (current != null) {
current.displayLink();
current = current.next;
}
System.out.println("");
}
public void sortll(){
Link current = first;
Link nextLink = first.next;
while(current.next != null){
while(nextLink.next != null)
if(nextLink.iData < current.iData){
Link temp = nextLink;
nextLink = current;
current = temp;
nextLink = nextLink.next;
current = current.next;
}
current = current.next;
}
}
}
测试应用程序。
public class LLtest {
public static void main(String[] args) {
LinkedList ll1 = new LinkedList();
ll1.insertFirst(11, "UWU0011");
ll1.insertFirst(3, "UWU0003");
ll1.insertFirst(1, "UWU0001");
ll1.insertFirst(4, "UWU0004");
ll1.insertFirst(5, "UWU0005");
ll1.insertFirst(6, "UWU0006");
ll1.insertFirst(7, "UWU0007");
ll1.insertFirst(10, "UWU0010");
ll1.insertFirst(9, "UWU0009");
ll1.insertFirst(2, "UWU0002");
ll1.insertFirst(8, "UWU0008");
ll1.sortll();
ll1.displayList();
}
}
谁能帮帮我?????
你的排序算法有问题,我觉得你没有做冒泡 sort.i 添加了交换链表数据但不交换链接的 sortll() 方法。我认为您正在尝试交换链接,如果您正在这样做,那么您在交换链接时需要更加小心,因为您需要仔细考虑最终情况。替换 sortll() 方法并检查它
public void sortll(){
Link current = first;
System.out.println(first.iData);
Link nextLink = first.next;
/*while(current.next != null){
while(nextLink.next != null)
if(nextLink.iData < current.iData){
Link temp = nextLink;
nextLink = current;
current = temp;
nextLink = nextLink.next;
current = current.next;
}
current = current.next;
}*/
int length=0;
while(current!=null)
{
length++;
current=current.next;
}
System.out.println(length);
for(int i=0;i<length;i++)
{
Link temp=first;
for(int j=0;j<length-i-1;j++)
{
if(temp.iData>temp.next.iData)
{
int tempiData = temp.iData;
String tempsData =temp.sData;
temp.iData =temp.next.iData;
temp.sData =temp.next.sData;
temp.next.iData=tempiData;
temp.next.sData=tempsData;
}
temp=temp.next;
}
}
}
}
这适用于上述问题。
public class LinkedList {
private Link first;
public void LinkList() {
first = null;
}
public void sortingLinkList(){ //working
boolean flag = true;
while (flag) {
flag = false;
Link position = first;
Link positionNext = position.next;
Link positionPrev = null;
while (positionNext != null) {
if(position.iData > positionNext.iData) {
Link temp = position;
Link tempNextNext = positionNext.next;
position = positionNext;
position.next = temp;
positionNext = temp;
positionNext.next = tempNextNext;
if (positionPrev == null) { // position is head
first = position;
} else {
positionPrev.next = position;
}
flag = true;
}
positionPrev = position;
position = position.next;
positionNext = position.next;
}
}
}
}
我正在尝试使用气泡对 link 列表进行排序 sort.But 我写的算法没有 work.Can 有人帮助我吗???
还有一个linkclass。
Link class
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);
}
}
LinkKist class 包括排序算法。
public class LinkedList {
private Link first;
public void LinkList() {
first = null;
}
public void insertFirst(int idata, String sdata) {
Link nl1 = new Link(idata, sdata);
nl1.next = first;
first = nl1;
}
public void displayList() {
System.out.println("List : ");
Link current = first;
while (current != null) {
current.displayLink();
current = current.next;
}
System.out.println("");
}
public void sortll(){
Link current = first;
Link nextLink = first.next;
while(current.next != null){
while(nextLink.next != null)
if(nextLink.iData < current.iData){
Link temp = nextLink;
nextLink = current;
current = temp;
nextLink = nextLink.next;
current = current.next;
}
current = current.next;
}
}
}
测试应用程序。
public class LLtest {
public static void main(String[] args) {
LinkedList ll1 = new LinkedList();
ll1.insertFirst(11, "UWU0011");
ll1.insertFirst(3, "UWU0003");
ll1.insertFirst(1, "UWU0001");
ll1.insertFirst(4, "UWU0004");
ll1.insertFirst(5, "UWU0005");
ll1.insertFirst(6, "UWU0006");
ll1.insertFirst(7, "UWU0007");
ll1.insertFirst(10, "UWU0010");
ll1.insertFirst(9, "UWU0009");
ll1.insertFirst(2, "UWU0002");
ll1.insertFirst(8, "UWU0008");
ll1.sortll();
ll1.displayList();
}
}
谁能帮帮我?????
你的排序算法有问题,我觉得你没有做冒泡 sort.i 添加了交换链表数据但不交换链接的 sortll() 方法。我认为您正在尝试交换链接,如果您正在这样做,那么您在交换链接时需要更加小心,因为您需要仔细考虑最终情况。替换 sortll() 方法并检查它
public void sortll(){
Link current = first;
System.out.println(first.iData);
Link nextLink = first.next;
/*while(current.next != null){
while(nextLink.next != null)
if(nextLink.iData < current.iData){
Link temp = nextLink;
nextLink = current;
current = temp;
nextLink = nextLink.next;
current = current.next;
}
current = current.next;
}*/
int length=0;
while(current!=null)
{
length++;
current=current.next;
}
System.out.println(length);
for(int i=0;i<length;i++)
{
Link temp=first;
for(int j=0;j<length-i-1;j++)
{
if(temp.iData>temp.next.iData)
{
int tempiData = temp.iData;
String tempsData =temp.sData;
temp.iData =temp.next.iData;
temp.sData =temp.next.sData;
temp.next.iData=tempiData;
temp.next.sData=tempsData;
}
temp=temp.next;
}
}
}
}
这适用于上述问题。
public class LinkedList {
private Link first;
public void LinkList() {
first = null;
}
public void sortingLinkList(){ //working
boolean flag = true;
while (flag) {
flag = false;
Link position = first;
Link positionNext = position.next;
Link positionPrev = null;
while (positionNext != null) {
if(position.iData > positionNext.iData) {
Link temp = position;
Link tempNextNext = positionNext.next;
position = positionNext;
position.next = temp;
positionNext = temp;
positionNext.next = tempNextNext;
if (positionPrev == null) { // position is head
first = position;
} else {
positionPrev.next = position;
}
flag = true;
}
positionPrev = position;
position = position.next;
positionNext = position.next;
}
}
}
}