Answered step by step
Verified Expert Solution
Question
1 Approved Answer
class StackList implements StackInterface { / / Note: Stack follows the LIFO ( Last In First Out ) principle. private Node top; private int
class StackList implements StackInterface
Note: Stack follows the LIFO Last In First Out" principle.
private Node top;
private int size;
add item on top
O
public void pushT item
save the prev. topnode in a temp variable
Node prev top;
init. top to a new node and pass in the item and prev. node
top new Nodeitem prev;
increment size
size;
remove item on top and returns it back to the usercaller
O
public T pop
iftop null OR size
System.out.printlnunable to pop, stack is empty";
return null;
save the item that will be returned back to the caller
T itemOnTop top.getItem;
remove the item on top
top top.getNext;
decrement size
size;
return item that was on top
return itemOnTop;
without popping, return item on top of the stack
O
public T peek return null;
O
public boolean isEmpty return false;
O
public int size return ;
ON
public boolean containsT item return false;
Convert StackList from "Week to use Arrays instead of Nodes, call it: StackArray.java
Unlike Nodes, arrays have a hard capacity which needs to be taken into account when implementing this data structure
A Stack follows the LIFO Last In First Out principle, which means items are added pushed at the "top" of the data structure and removed popped from the "top"
In an array, "top" would the the last item in the array that is not null
HINT: One can use an integer variable to keep track bookkeeping of the index that is considered the "top" most item in the Stack array
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started