我正在使用Java中的Linked List和Linear Node Class进行堆栈,我遇到了一个问题:
........
public BoundedStackADTLinkedImpl() {
count = 0;
top = null;
}
public BoundedStackADTLinkedImpl(int stackCapacity) {
//I dont know how to do a builder using LinearNode with a initial Capacity
}
public BoundedStackADTLinkedImpl(int stackCapacity, T... initialContent) {
//Here I have the same problem.
count = 0;
top = new LinearNode<T>();
for (T Vi : initialContent) {
push(Vi);
}
........
感谢!!!
答案 0 :(得分:0)
答案 1 :(得分:0)
你想要这样的链接列表。请记住,您无法设置初始容量,因为容量取决于列表中的元素数量,您不需要为它们预先创建空间。
public class LinkedList<T>
{
Node<T> topNode = null;
public LinkedList()
{
}
public LinkedList( Node<T>... nodes )
{
for( Node<T> n : nodes )
{
push( n );
}
}
public void push(Node<T> node)
{
if ( topNode == null )
{
topNode = node;
return;
}
Node<T> n = topNode;
while( n.nextNode != null )
{
n = n.nextNode;
}
n.nextNode = node;
}
public Node<T> pop()
{
if ( topNode != null )
{
Node<T> n = topNode;
Node<T> p = n;
while( n.nextNode != null )
{
n = n.nextNode;
if ( n.nextNode != null )
{
p = n;
}
}
p.nextNode = null;
return n;
}
return null;
}
}
class Node <T>
{
Node<T> nextNode = null;
T value;
public Node( T val )
{
value = val;
}
}