本文目錄一覽:
java 單鏈表排序問題
你的這個類,按照你的意思,應該有處理鏈表的方法。
比如有class A{
//there are many methods which you use them to do different jobs.
LinkList sort(LinkList p)
{
//處理鏈表排序
//返回鏈表
}
}
在你的主函數中,new 一個A,用A的對象調用A中那個排序的方法。作為一個方法寫在一個類中,提高了代碼的復用性。如果寫在主函數中,首先,看起來,很不爽,其次,如果你的程序中需要多次調用,你就沒啥子辦法。JAVA中不能使用GOTO語句。
用java編寫程序實現單鏈表,要提供插入,刪除,排序,統計等功能,鏈表節點中的數據要求是整數。
public class Link {
Node head = null;
Node point = null;
Node newNode = null;
public int Count = 0;//統計值
//插入
public void AddNode(int t) {
newNode = new Node();
if (head == null) {
head = newNode;
} else {
point = head;
while (point.next != null) {
point = point.next;
}
point.next = newNode;
}
point = newNode;
point.vlaue = t;
point.next = null;
Count++;
}
//返回值
public int GetValue(int i) {
if (head == null || i 0 || i Count)
return -999999;
int n;
Node temp = null;
point = head;
for (n = 0; n = i; n++) {
temp = point;
point = point.next;
}
return temp.vlaue;
}
//刪除
public void DeleteNode(int i) {
if (i 0 || i Count) {
return;
}
if (i == 0) {
head = head.next;
} else {
int n = 0;
point = head;
Node temp = point;
for (n = 0; n i; n++) {
temp = point;
point = point.next;
}
temp.next = point.next;
}
Count–;
}
//排序
public void Sotr() {
for (Node i = head; i != null; i = i.next) {
for (Node j = i.next; j != null; j = j.next) {
if (i.vlaue j.vlaue) {
int t = i.vlaue;
i.vlaue = j.vlaue;
j.vlaue = t;
}
}
}
}
}
class Node {
int vlaue;
Node next;
}
怎樣對java中的單向鏈表中的數據進行排序
可行,不過貌似有些麻煩的說,你用一個LIST的迭代器把鏈表中的元素迭代到LIST裏面以後,有專門的方法進行排序的!!!
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/304109.html