Lecture 4
Common questions
The things people most often get confused about in stack.
What makes a stack different from a linked list or an array?Basics
It restricts access. An array lets you read any a[i] and a list lets you insert anywhere, but a stack only lets you touch the top: push adds there and pop removes from there. That restriction gives LIFO order, and it's exactly what problems like undo, matching brackets and function calls need.
What is the difference between pop and stackTop?Basics
Both give you the top element through a reference parameter. pop(el) also removes it, so top moves down. stackTop(el) only reads it, so the stack is unchanged (it's const). In other books stackTop is called peek or top().
What are overflow and underflow?Basics
Overflow is pushing onto a full stack, which only an array stack can be. Underflow is popping (or reading the top of) an empty stack, which can happen to any stack. Check stackIsFull() before push and stackIsEmpty() before pop and stackTop.
Why is every stack operation O(1)?Basics
Each operation touches only the top. In the array version that's one index change and one array access; in the linked version it's one node allocated or freed and two pointer assignments. Nothing is shifted or searched, no matter how many elements there are.
Why does the array stack start with top = -1?Array stack
top is the index of the top element. With no elements there is no valid index, and -1 is just below 0, so the first push does ++top and writes into stack[0]. That makes empty top < 0 and full top == MaxSize - 1.
What is the difference between stack[++top] = v and stack[top++] = v?Array stack
++top increments first and then indexes, so with top = -1 it writes stack[0]: correct. top++ indexes with the old value first, so it writes stack[-1] (out of bounds) and leaves top pointing at an empty slot. For pop it's the opposite: v = stack[top--] reads the top, then moves down.
// top == 2, elements in [0..2]
stack[++top] = v; // writes stack[3], top == 3
v = stack[top--]; // reads stack[3], top == 2Does pop erase the value from the array?Array stack
No. It only moves top down. The old value is still in the slot, but it's now outside the stack (above top), and the next push overwrites it. That's why the stack's contents are only stack[0] to stack[top].
Why does Stackt need a copy constructor and a destructor?Array stack
It owns a heap array through Type* stack. The destructor must delete[] it, or every stack leaks. Without a copy constructor, C++ copies the pointer, so two stacks share one array: a push on one changes the other, and both destructors delete[] the same memory. The slides' copy constructor allocates a new array and copies elements 0 to top (a deep copy).
template <class Type>
Stackt<Type>::Stackt(const Stackt<Type>& original) {
MaxSize = original.MaxSize;
top = original.top;
stack = new Type[MaxSize];
for (int i = 0; i <= top; i++)
stack[i] = original.stack[i];
}Where does the default capacity of 128 go?Array stack
Only in the declaration in the header: Stackt(int nelements = 128);. The definition in Stackt.cpp is written Stackt<Type>::Stackt(int nelements) without the = 128; repeating the default there is a compile error. Stackt<int> s; gets 128 slots and Stackt<int> s(10); gets 10.
What's wrong with the slides' Stack<T>::push?Array stack
When isFull() is true it prints "Stack is full" but doesn't stop: it still does top = top + 1 and writes st[top], which is past the end of the array. Add a return; inside the if (or put the push in an else).
if (isFull()) {
cout << "Stack is full\n";
return; // the missing line
}
st[++top] = k;Why is the top of a linked stack the head of the list?Linked stack
A singly linked list can insert and delete at the head in O(1). At the tail you can insert quickly with a tail pointer, but deleting the tail needs the node before it, which means walking the whole list. So top is the head pointer, push is "insert at head" and pop is "delete head" from Lecture 3.
Why doesn't StackL have stackIsFull()?Linked stack
It has no fixed capacity: every push allocates a new node, so it's only "full" when the heap runs out and new fails. That flexibility costs one next pointer per element, which is why the slides say a linked stack uses more space.
In linked pop, why save the value and move top before delete?Linked stack
After delete cursor the node is gone, so reading cursor->e or cursor->next is undefined behaviour. Read the value, keep the old node in cursor, move top to top->next, and only then free it.
v = top->e; // 1. save the value
cursor = top; // 2. remember the node
top = top->next; // 3. advance top
delete cursor; // 4. free itWhat must the linked stack's destructor do?Linked stack
Free every node. The stack object only contains the top pointer, so when it goes out of scope the nodes stay on the heap unless the destructor walks the list and deletes them. The simplest version pops until the stack is empty.
Why does a stack solve the balanced-brackets problem?Applications
A closing bracket must match the most recently opened bracket that's still open, and "most recent" is exactly what the top of a stack holds. Push each opener; each closer pops one and checks it's the right kind. It's unbalanced if a closer meets an empty stack, if the kinds don't match, or if openers are left when the input ends.
When evaluating postfix, which popped value is the left operand?Applications
The second one. The left operand was pushed first, so it's underneath. For 8 2 - you pop 2 (right), then 8 (left), and push 8 - 2 = 6. Getting this backwards only changes the result for - and /, which is why exam questions use them.
Why doesn't postfix need parentheses?Applications
Every operator comes right after its two operands, so the order of evaluation is fixed by the order of the tokens. (A+B)*C is A B + C * and A+(B*C) is A B C * +; the different token order already says which operation happens first.
How does a stack convert infix to postfix (self-study)?Applications
Operands go straight to the output. An operator waits on the stack; before pushing it, pop to the output every operator on top with greater or equal precedence. ( is pushed, and ) pops to the output until the matching (. At the end pop everything. A + B * C - D becomes A B C * + D -.
How is a stack used for decimal to hexadecimal (self-study)?Applications
Dividing by 16 produces the digits from the least significant to the most, which is backwards for printing. Push each n % 16 digit, set n /= 16 until n is 0, then pop them all: they come out most significant first. 300 → push C, 2, 1 → pop 12C.
How does a stack find a path through a maze?Applications
Push each cell (or track) as you move onto it, trying directions in a fixed order. At a dead end, pop to backtrack to the previous position and try its next direction. When you reach the exit, the stack from bottom to top is the path. Doing the same thing recursively uses the system stack and is called depth first search (DFS).
What does the run-time stack have to do with this lecture?Applications
Every function call pushes a frame (its parameters, locals and return address) on the program's call stack, and returning pops it. The function that was called last returns first: LIFO. That's also why infinite recursion ends in a stack overflow: frames are pushed until the call stack's fixed space runs out.
No questions match.
Ready to check yourself? Take the quiz →