0

Java で LinkedList と LinearNode クラスを使用してスタックを実行していますが、問題があります。

.........

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);
    }

.........

ありがとう!!!

4

2 に答える 2

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;
}
}
于 2014-04-24T16:40:56.033 に答える
0

LinkedListアイテムが追加されたときにメモリを割り当てます。初期容量の意味はありません。各アイテムには、次のアイテムへのポインターがあります。

ensureCapacity()メソッドを持つStackを使用しないのはなぜですか?

ここに画像の説明を入力

于 2014-04-24T16:33:02.987 に答える