Here is what a linked list implementing a stack with 3 elements might look like:
list
|
v
-------- -------- ---------
| C | -+-->| B | -+-->| A | 0 |
-------- -------- ---------
Where should we consider the top of the stack to be, the beginning or end of the list, and why?
Thanks in advance.
The fastest element to access in a linked list is usually the head (some implementations also keep a reference to the tail element though). Since the stack only ever needs to access the top element, that should be the head element of the linked list. This will avoid having to iterate over the entire list for every operation.