Three Data Structures: Queue, Stack, and Deque: First-In-First-Out
Three Data Structures: Queue, Stack, and Deque: First-In-First-Out
Three Data Structures: Queue, Stack, and Deque: First-In-First-Out
2. pop(): Remove and return the top value of the stack. This changes the list [x, c, y] to [x, c] and returns y.
An example of a stack in real life is a stack of cafeteria trays. Workers add clean trays to the top, and you take
the tray from the top of the stack. A stack is also called a LIFO list. LIFO stands for Last-In-First-Out.
Implementation of a stack
A stack (of bounded size) can be efficiently implemented using an array b and an int variable n: The n elements
of the stack are in b[0..n-1], with b[0] being the bottom element and b[n-1] being the top element.
A stack can be efficiently implemented using a linked list. The first element is the top of the stack and the last el-
ement is the bottom. It’s easy to push (prepend) an element and pop (remove) the first element in constant time.
The deque
The word deque, usually pronounced deck, is short for double-ended queue. A deque is a list that supports inser-
tion and removal at both ends. Thus, a deque can be used as a queue or as a stack.