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?
Question
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?
OTHER TIPS
You want something like this for a linked list. Just remember, you can't set initial capacity, as capacity is determined by the number of elements in the list, you don't pre-create space for them.
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;
}
}