The best part, I think, is that your index number, say your list
has
[5, 3, 2, 1, 9]
left would be
index 0 = 5;
index 1 = 3;
index 2 =2;
index 3 = 1;
Type: Posts; User: javapenguin
The best part, I think, is that your index number, say your list
has
[5, 3, 2, 1, 9]
left would be
index 0 = 5;
index 1 = 3;
index 2 =2;
index 3 = 1;
Well, add a method called toString() to your generic stack class.
public String toString()
{
return list.toString();
}
then later, in your main class, you can
stack.toString() to show a...
import java.util.*;
public class GenericStack<E>
{
private LinkedList<E> list = new LinkedList<E>();
public void push(E item) //the parameter is an item of type E
{
...
For instance, if you want to get the top, usually the only place a Stack can get to,
you'd be this:
public E top()
{
return list.getFirst();
}
if you want to get an item at an index, you'd...
Besides, if you're trying to get the item itself, your return type would be type E.
Also, maybe this is just to show what it is, but I can't find this variable/class
...
list.get(item) is invalid.
List.contains(item)
is valid
list.get(int index) is valid.
If you want the index, do a for loop and check for all the indexes to see if
item =...
The LinkedList class already has a get(int index) method.
But again, a Stack usually is only concerned with get(0).
The get method returns a variable of type E.
If, using the for loop I gave...
Well, hop through the list
public int index (E item)
{
for (int i =0; i < size(); i++)
{
if (list.get(i).equals(item))
return i;
}
return -1;