Question: What is the issue with peek2 package stackdriver; public interface StackInterface { public boolean isEmpty(); public void push(T newEntry); public T pop(); public T peek();

What is the issue with peek2

package stackdriver;

public interface StackInterface {

public boolean isEmpty();

public void push(T newEntry);

public T pop();

public T peek();

public T peek2();

public void clear();

}

package driverstack;

import java.util.Stack;

public class DriveStack {

public static void main(String[] args) {

Stack stack = new Stack();

System.out.println(stack.isEmpty());

stack.push("Star Wars");

System.out.println(stack.isEmpty());

stack.push("Black Panther");

System.out.println("ttt" +stack.peek2());

}

}

package stackdriver;

import java.util.EmptyStackException;

public class LinkedListStack implements StackInterface {

private Node topNode;

public boolean isEmpty() {

return topNode == null;

}

public void push(T newEntry) {

Node newNode = new Node(newEntry, topNode);

topNode = newNode;

}

public T pop() {

T top = peek();

topNode = topNode.forward;

return top;

}

public T peek() {

if (isEmpty()) {

throw new EmptyStackException();

}else {

return topNode.getInfo();

}

}

public T peek2() {

if (isEmpty()) {

return null;

}else {

T top = null;

if(topNode.forward == null) {

return null;

} else {

top = topNode.forward.getInfo();

return top;

}

}

}

public void clear() {

topNode = null;

}

private class Node {

private T info;

private Node forward;

public Node(T newInfo) {

info = newInfo;

forward = null;

}

public Node(T infoStack, Node connectStack) {

info = infoStack;

forward = connectStack;

}

public void setInfo(T newInfo) {

info = newInfo;

}

public T getInfo() {

return info;

}

public Node getNextNode() {

return forward;

}

public void setNextInfo(Node forwardNode) {

forward = forwardNode;

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!