In Chapter 3, Using Linked Lists, we learned how to implement linked lists. We saw that in a linked list we can insert a node at the end, remove it from the end, insert it into the middle of the list, at the beginning, and so on. If we consider the insert at the end and remove at the end operations of a single linked list data structure, we can easily perform something similar with stack. So let us use our LinkedList class from the previous chapter to implement with the stack. This is how the code will look:
class BookList implements Stack {
private $stack;
public function __construct() {
$this->stack = new LinkedList();
}
public function pop(): string {
if ($this->isEmpty()) {
throw new UnderflowException('Stack is empty');
} else {
$lastItem = $this->top();
...