Stack with nodes question
I recently took a test for my class and got the test back and got this part of the test all wrong
Code:
public class Stack<E>
{
static class SNode<T>
{
T data;
SNode<T> next;
SNode(T d, SNode<T n>
{
data = d;
next = b;
}
SNode(){this(null,null)}
}
private SNode<E> top; //pointer to top of stack
private int size;
void push(E elt){ /*this needs to be defined */}
}
a)
SNode<E> tmp = new SNode<E>();
tmp.next = top;
top = tmp;
top.data = elt;
size++;
b)
SNode<E> tmp = new SNode<E>();
top = tmp;
tmp.next = top;
tmp.data = elt;
size++;
c)
top = new SNode<E>(elt,top);
size++
The question is whether those are the correct body for the push method. Those are the ones I got wrong so if anyone can help id appriciate it.
If you can give me step by step instructions on what points to what and how that means its a valid push method id appriciate it.
Oh and the answers are yes, no, and yes.
Re: Stack with nodes question
Better yet, why don't you explain, step for step, what you think needs to happen, and then we will help you to correct that.