gpt4 book ai didi

java - 在 Java 中对双链表进行排序

转载 作者:行者123 更新时间:2023-12-04 05:53:25 25 4
gpt4 key购买 nike

我必须为我的编程课制作一个链表程序。它可以工作,每次插入一个数字时,它都会放在列表的开头。现在我的老师希望我们使用链表程序并按升序对数字进行排序。我完全不知道如何做到这一点。任何人都可以指出我正确的方向吗?
这是我的列表代码:

public class SortedList {

private DoubleNode head = null;
private int listLength;

public static void main(String[] args) {
SortedList list = new SortedList();
list.insert(6);
list.insert(7);

System.out.println(list.toString());

}

public void insert(double value) {

head = new DoubleNode(value, head);
listLength++;

}

public String toString() {

String answer = "[ ";
for (DoubleNode current = head; current != null; current = current
.getLink()) {
answer += current.getData() + " ";
}
answer += "]";
return answer;
}

public int find(double value) {
if (listLength == 0)
return -1;

int pos = 1;
for (DoubleNode current = head; current != null; current = current.getLink()) {
if (current.getData() == value)
return pos;
pos++;
}
return -1;
}

public int size() {
return listLength;
}

public boolean removeAt(int index) {
if (index < 1 || index > listLength)
return false;

if (index == 1) {
if (head != null) {
head = head.getLink();
listLength--;
}
return true;
}

DoubleNode current = head;
for (int i = 1; i < (index - 1); i++) {
if (current.getLink() == null)
return false;
current = current.getLink();
}
current.setLink(current.getLink().getLink());
listLength--;
return true;
}

}

这是我的老师给出的节点的代码:
// File: DoubleNode.java based on the DoubleNode class by Michael Main

/**************************************************************************
* DoubleNode provides a node for a linked list with double data in each node.
*
* @note
* Lists of nodes can be made of any length, limited only by the amount of
* free memory in the heap.
*
* @author Michael Main
* shortened by Beth Katz and Stephanie Elzer to be only the basics
*
* @version
* February 2007
***************************************************************************/
public class DoubleNode
{
// Invariant of the DoubleNode class:
// 1. The node's double data is in the instance variable data.
// 2. For the final node of a list, the link part is null.
// Otherwise, the link part is a reference to the next node of the list.
private double data;
private DoubleNode link;

/**
* Initialize a node with a specified initial data and link to the next
* node. Note that the initialLink may be the null reference, which
* indicates that the new node has nothing after it.
* @param initialData
* the initial data of this new node
* @param initialLink
* a reference to the node after this new node--this reference may be
* null to indicate that there is no node after this new node.
* @postcondition
* This node contains the specified data and link to the next node.
**/
public DoubleNode(double initialData, DoubleNode initialLink)
{
data = initialData;
link = initialLink;
}

/**
* Accessor method to get the data from this node.
* @param - none
* @return
* the data from this node
**/
public double getData( )
{
return data;
}

/**
* Accessor method to get a reference to the next node after this node.
* @param - none
* @return
* a reference to the node after this node (or the null reference if
* there is nothing after this node)
**/
public DoubleNode getLink( )
{
return link;
}

/**
* Modification method to set the data in this node.
* @param newData
* the new data to place in this node
* @postcondition
* The data of this node has been set to newData.
**/
public void setData(double newData)
{
data = newData;
}

/**
* Modification method to set the link to the next node after this node.
* @param newLink
* a reference to the node that should appear after this node in the
* linked list (or the null reference if there is no node after this node)
* @postcondition
* The link to the node after this node has been set to newLink. Any other
* node (that used to be in this link) is no longer connected to this node.
**/
public void setLink(DoubleNode newLink)
{
link = newLink;
}
}

最佳答案

想法

1) 在最简单的情况下,列表已经排序:

-> A



2)现在,考虑“下一个”情况(即向大小为 1 的列表中添加 1 个新元素)

-> A [now, i am going to try adding C]



您可以简单地检查 C 是否 > 比 A,在这种情况下,您在末尾添加“C” (->A->C)

3)我们可以概括情况(2):在任何后续情况下,您都必须沿着列表向下走,直到您“看到”一个新节点,该节点大于您插入的节点。

-> A -> C [adding B]


check 1: A (B > A)
check 2: C (B < C) !

这意味着我们可以按如下方式替换链接:

replace A -> C with 2 new links, 1 from A -> B , and also another from B -> C.



以这种方式插入保证您的列表保持排序。

具体

因此,您必须修改应用程序的 insert(..) 方法,从列表的开头开始,检查每个 DoubleNode,向下走,并“记住”即存储前一个 DoubleNode,直到它到达末尾列表的 --- OR 它看到最后一个节点 < 比新节点,而当前节点 > 比新节点。

关于java - 在 Java 中对双链表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9799671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com