Exercise 10.2-2

Implement a stack using a singly linked list \(L\). The operations PUSH and POP should still take \(O(1)\) time.

In my implementation, PUSH accepts integer inputs and POP outputs integers.

STACK-EMPTY(L)

1 return L.head == NIL

PUSH(L, x)

1 node = new LinkedListNode(x)

2 LIST-INSERT(L, node)

POP(L)

1 if STACK-EMPTY(L):

2     error "underflow"

3 result = L.head.key

4 LIST-DELETE(L, L.head)

5 return result