[英]LinkedList to store LinkedList of Nodes
I have written a LinkedList
class that accepts Nodes
that stores Integers
. 我编写了一个
LinkedList
类,该类接受存储Integers
Nodes
。
I have then created a LinkedList stack = new LinkedList()
, and have added Node
s to it if the data of the Node
s are less than the data of the Node
s that already exist in this stack
. 我已经然后,创建一个
LinkedList stack = new LinkedList()
并增加了Node
s到它,如果数据Node
s为小于所述的数据Node
s表示已经在此存在stack
。
If not, I want to put this old stack
into a new LinkedList
called LinkedList pilesOfStacks
, and create a newStack
called LinkedList newStack = new LinkedList()
, and add the larger Node
into this newStack
which will also go into the LinkedList pilesOfStacks
. 如果没有,我想把这个老
stack
到一个新LinkedList
称为LinkedList pilesOfStacks
,并创建一个newStack
称为LinkedList newStack = new LinkedList()
并添加较大的Node
进入这个newStack
这也将进入LinkedList pilesOfStacks
。
My question is; 我的问题是; since I already created my
LinkedList
class to accept Node
s, how do I make it a new LinkedList
to accept LinkedList
s of these Nodes
, essentially creating different piles of LinkedList
s in a LinkedList
? 因为我已经创造了我
LinkedList
类接受Node
S,我怎么做一个新LinkedList
接受LinkedList
这帮的Nodes
,基本上是建立不同桩LinkedList
在A S LinkedList
?
This is what I have so far: 这是我到目前为止的内容:
public void sort(LinkedList listOfInts)
{
LinkedList<LinkedList> piles = new LinkedList<LinkedList>();
LinkedList stack = new LinkedList();
for(int i = 0; i < listOfInts.getSize(); i++)
{
Node x = listOfInts.pop();
for(int j = 0; j < piles.getSize(); j++)
{
Node y = piles.peek(); //check first element of each pile
if( ( ((Comparable)y.getData()).compareTo(x.getData()) ) <= 0)
{
stack.push(x);
break;
}
}
stack.push(x); //put value in stack
piles.add(stack);
}
}
Edit: If I could use an array, I would create a double array to something of the effect Node[][] array = new Node[20][20];
编辑:如果我可以使用数组,我会创建一个双精度数组来影响
Node[][] array = new Node[20][20];
and then search it with Node[i][0]
, but since I can only use LinkedList
, I'm wondering how to do this? 然后使用
Node[i][0]
搜索它,但是由于我只能使用LinkedList
,所以我想知道如何执行此操作?
Okay, I just gave it a shot - using the Java Collection Framework as mentioned by Kami and Roman C . 好的,我只是试一试-使用Kami和Roman C提到的Java Collection Framework。 To avoid confusion I always used the full qualified name of the interfaces/classes involved - even though that makes the code look big and ugly .
为避免混淆,我始终使用所涉及的接口/类的全限定名- 即使这会使代码看起来笨拙 。
I used the java.util.LinkedList
which implements the java.util.List
as well as the java.util.Deque
interface. 我用的是
java.util.LinkedList
它实现了java.util.List
还有java.util.Deque
接口。 The latter one gives you the methods to treat it as a stack. 后者为您提供了将其视为堆栈的方法。
I assume from the method name, that you actually want to sort the Nodes in your stack. 我从方法名称假设,您实际上要对堆栈中的节点进行排序。 For that I had to change some parts of your original example, since it seemed not to behave like you described.
为此,我不得不更改您原始示例的某些部分,因为它似乎不像您描述的那样运行。
I ended up with the following variation of your example: 我得到了您的示例的以下变体:
public void sort(java.util.Deque<Node> stackOfIntNodes) {
java.util.List<java.util.Deque<Node>> piles =
new java.util.LinkedList<java.util.Deque<Node>>();
java.util.Deque<Node> currentStack = new java.util.LinkedList<Node>();
inputLoop : while (!stackOfIntNodes.isEmpty()) {
Node currentNode = stackOfIntNodes.pop();
for (java.util.Deque<Node> singlePile : piles) {
// check first element of each pile
Node smallestNodeInSinglePile = singlePile.peek();
Object valueOfSmallestNodeInSinglePile =
smallestNodeInSinglePile.getData();
if ((((java.lang.Comparable) valueOfSmallestNodeInSinglePile)
.compareTo(currentNode.getData())) <= 0) {
singlePile.push(currentNode);
continue inputLoop;
}
}
piles.add(currentStack);
currentStack = new java.util.LinkedList<Node>();
currentStack.push(currentNode); // put value in stack
}
piles.add(currentStack);
java.util.Deque<Node> sortedStackOfIntNodes = new java.util.LinkedList<Node>();
for (java.util.Deque<Node> singlePile : piles) {
while (!singlePile.isEmpty()) {
sortedStackOfIntNodes.push(singlePile.pop());
}
}
// RESULT: you got all your Node elements in sorted order
}
But if you were really using the java.util.LinkedList
instead of your own implementation, you could easily use this equivalent method: 但是,如果您确实使用
java.util.LinkedList
而不是自己的实现,则可以轻松使用此等效方法:
public void sort(java.util.Deque<Node> stackOfIntNodes) {
java.util.LinkedList<Node> sortedListOfIntNodes =
new java.util.LinkedList<Node>(stackOfIntNodes);
java.util.Collections.sort(sortedListOfIntNodes,
new Comparator<Node>() {
@Override
public int compare(Node nodeOne, Node nodeTwo) {
return ((java.lang.Comparable) nodeOne.getData())
.compareTo(nodeTwo.getData());
}
});
// RESULT: you got all your Node elements in sorted order
}
Depending on your actual Node class and/or own LinkedList, you might have to apply further changes here. 根据您实际的Node类和/或自己的LinkedList,您可能必须在此处应用进一步的更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.