用java编写程序实现单链表,要提供插入,删除,排序,统计等功能,链表节点中的数据要求是整数。
1个回答
展开全部
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;
}
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;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询