Question: 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
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
