Search code examples
javalistlinked-liststackcapacity

Stack linkedlist with initial Capacity


I am doing a stack using LinkedList and LinearNode Class in Java and I have a problem:

........

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

........

thanks!!!


Solution

  • LinkedList allocate a memory when item is added in it. There is no meaning of initial capacity. Each item has a pointer to next item.

    Why don't you use Stack that has a ensureCapacity() method?

    enter image description here