首页 > 编程知识 正文

java实现单链表的排序(重排链表java)

时间:2023-11-30 11:44:51 阅读:310663 作者:KGSD

本文目录一览:

  • 1、java 单链表排序问题
  • 2、用java编写程序实现单链表,要提供插入,删除,排序,统计等功能,链表节点中的数据要求是整数。
  • 3、怎样对java中的单向链表中的数据进行排序

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里面以后,有专门的方法进行排序的!!!

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。