[英]Linked list merge sort using java
我想使用Java仅使用链接列表而不使用任何数组来实现合并排序。 但是我陷入了逻辑错误; 我的代码消除了一些输入并对剩余部分进行排序。 我已经应用了三个类: Divide
, Merg
, MergSort
,如下所示:
public class Divide {
List firstList = new List();
List secondList = new List();
public int GetLength(List list) {
int Length = 0;
Link temp = new Link();
temp.next = list.head.next;
while (temp.next != null) {
temp.next = temp.next.next;
Length++;
}
return Length;
}
public List rightSide(List list) {
int Length = GetLength(list);
Link temp = new Link();
temp.next = list.head.next;
for (int i = 1; i <= Math.floor(Length / 2); i++) {
firstList.Insert(temp.next.data);
temp.next = temp.next.next;
}
return firstList;
}
public List leftSide(List list) {
int Length = GetLength(list);
Link temp = new Link();
temp.prev = list.head.prev;
for (int i = 1; i <= Math.ceil(Length / 2); i++) {
secondList.Insert( temp.prev.data);
temp.prev = temp.prev.prev;
}
return secondList;
}
}
合并:
public class Merg {
public List MergedList = new List();
private List Temp = new List();
public List Merg (List one, List two)
{
Link onelink = new Link();
Link twolink = new Link();
onelink.next = one.head.next;
twolink.next = two.head.next;
while (onelink.next!= null || twolink.next!= null)
{
if(onelink.next!= null && twolink.next != null)
{
if(onelink.next.data < twolink.next.data)
{
Temp.Insert(onelink.next.data);
onelink.next = onelink.next.next;
}
else
{
Temp.Insert(twolink.next.data);
twolink.next = twolink.next.next;
}
}
if (onelink.next != null && twolink.next == null)
{
Temp.Insert(onelink.next.data);
onelink.next = onelink.next.next;
}
if (twolink.next != null && onelink.next == null)
{
Temp.Insert(twolink.next.data);
twolink.next = twolink.next.next;
}
}
if (Temp.head.next.data > Temp.head.prev.data)
{
while (Temp.head.next != null)
{
MergedList.Insert(Temp.head.next.data);
Temp.head.next = Temp.head.next.next;
}
}
else
{
MergedList.head.next = Temp.head.next;
}
return MergedList;
}
}
MergSort:
public class MergSort {
public List mergSort (List list)
{
Divide divide = new Divide();
Merg merg = new Merg();
if (divide.GetLength(list) > 1) {
return merg.Merg(mergSort(divide.leftSide(list)), mergSort(divide.rightSide(list)));
}
else
{
return list;
}
}
}
尽管我也编写了link
和list
类,但我认为其中没有麻烦。 (但是,如果有必要,我会提及它们)现在,当我导入一些输入,例如:{100,3,1,7,6}时,输出为:3,6,7,100。 (已消除1!)或另一个示例:{100,3,1}输出为:1,100(其中3是??)
我想知道是否有人可以帮助我...
链接:
public class Link {
public double data;
public Link prev;
public Link next;
}
清单:
public class List {
public Link head = new Link();
public void setHead(Link head) {
this.head = head;
head.prev = null;
head.next = null;
}
public void Insert(double x)
{
Link link = new Link();
link.data = x;
if (head.next == null)
{
head.next = link;
head.prev = link;
}
else
{
head.next.prev = link;
link.next = head.next;
head.next = link;
}
}
public Link delete()
{
Link temp = new Link();
temp = head;
head = head.next;
return temp;
}
public Link search(double x)
{
Link link = new Link();
link.next = head;
while (link.next.data != x)
{
link.next = link.next.prev;
}
return link.next;
}
}
我已经调试了很多东西,但是我对您的Divide
课堂还是没什么意思。 您根本不需要它。
public class MergSort {
public List mergSort(List list) {
Divide divide = new Divide();
Merg merg = new Merg();
int n = list.getLength();
if (n > 1) {
// return merg.Merg(mergSort(divide.leftSide(list)), mergSort(divide.rightSide(list)));
Link cursor = list.head.next;
List left = new List();
List right = new List();
// for (i = 0; ....... i will be 0 if head is not dummy
for (int i = 1; cursor != null; i++) {
if (i <= n/2)
left.Insert(cursor.data);
else
right.Insert(cursor.data);
cursor = cursor.next;
}
left = mergSort(left);
right = mergSort(right);
return merg.Merg(left, right);
} else {
return list;
}
}
}
在List
类中创建一个getLength()
方法。 它应该在List
类中,因为您需要列表的长度。
public int getLength() {
int Length = 0;
Link temp = head.next;
while (temp != null) {
temp = temp.next;
Length++;
}
return Length;
}
另外,将您的Link
类重命名为Node
。 因为Link
是指对象之间的链接或节点之间的边。 您真正想要的是一个Node
。 令人困惑。
在List
类中,您已经定义了head
如下所示:
public Link head = new Link();
我不明白为什么您需要一个假名的链表 。
具有虚拟头的链表基本上是没有值的头,因此是虚拟的。
由于您的虚拟头,您正在从i = 1
开始在Divide
类中进行for循环。 为什么使事情复杂化? 如果您确实需要虚拟头,则保留它或像这样定义它:
public Link head;
// don't instantiate the head. It's just a reference
另外,您似乎创建了一个半圆形的链表。 我的意思是head.prev
指向列表中的最后一个链接/节点。 如果您确实想要一个圆形链表,则将tail.next
指向head。
循环链表如下所示:
或者,如果您不需要循环列表,则不要将head.prev
指向最后一个对象。
在List.java
Insert()
方法中:
if (head.next == null)
{
head.next = link;
// remove head.prev = link;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.