當前位置:成語大全網 - 新華字典 - 用java編寫程序實現單鏈表,要提供插入,刪除,排序,統計等功能,鏈表節點中的數據要求是整數。

用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;

}