I Need help to understand How I can Implement Stack With Linked List in Java. This is for my assignment and I got stuck here,
I have to Implement the interface DStack, which has the methods push(), pop(), peek(), and isEmpty() in our LinkedList.java file.
Code Start ——->
public class ListStack implements DStack{
private Node head;
private int size;
private class Node{
double d;
Node next;
public Node(){
head = null;
next = null;
}
public Node(double data){
d = data;
}
}
public boolean isEmpty() {
return (head == null);
}
@Override
public void push(double d) {
if(!isEmpty()){
Node newHead = new Node(d);
newHead.next = head;
head = newHead;
size ;
}
else
throw new EmptyStackException(); //To change body of generated methods, choose Tools | Templates.
}
@Override
public double pop() {
if(!isEmpty()){
double d = head.d;
head = head.next;
size–;
return d;
}
else
throw new EmptyStackException(); //To change body of generated methods, choose Tools | Templates.
}
@Override
public double peek() {
if(!isEmpty()){
return head.d;
}
else
throw new EmptyStackException(); //To change body of generated methods, choose Tools | Templates.
}
Code End <—————
Can someone help me to understand If I am doing the push() and pop() methods correctly?
I was taking reference from this article.