I'm new to the programming and I want to ask a question about my linkedlist stack implementation if it's correct and if it meets the requirements of the linkedlist stack implementation. If there is any suggestion I'm glad to hear...
public class DynamicStack {
private class Node {
private Object item;
private Node next;
Node(Object item){
this.item = item;
this.next = null;
}
Node(Object item, Node nextNode){
this.item = item;
this.next = nextNode;
}
}
private int count;
private Node top;
public DynamicStack() {
this.top = null;
this.count = 0;
}
public void push(Object item) {
if(top == null) {
top = new Node(item);
}else {
Node newNode = new Node(item,top);
top = newNode;
}
count++;
}
public Object peek() {
if(top == null) {
throw new NoSuchElementException("Underflow Exception");
}else {
return top.item;
}
}
public Object pop() {
Node currentNode = top;
if(top == null) {
throw new NoSuchElementException("Underflow Exception");
}else {
Node nextNode = top.next;
top = null;
top = nextNode;
}
count--;
return currentNode.item;
}