Self-test 18:
Stack and Queue
-
What policy does stack use when inserting and removing items
- LILO
- FIFO
- LIFO
SolutionC
-
What policy does queue use when inserting and removing items?
- LILO
- FIFO
- LIFO
SolutionB
-
What do we say when we add an item to a stack?
- push
- add
- enqueue
SolutionA
-
What do we say when we delete an item from a queue?
- pop
- poll
- dequeue
SolutionC
-
What is the result of the following program?
int main(void) { int_stack s; int_queue q; int b; s.push(2); s.push(1); q.enqueue(2); q.enqueue(1); b = a.top(); a.pop(); cout << b << endl; b = a.top(); a.pop(); cout << b << endl; b = q.front(); q.dequeue(); cout << b << endl; b = q.front(); q.dequeue(); cout << b << endl; return 0; }Solution1 2 2 1
-
Following the example "Balanced Parentheses" in lecture notes, what are the items in the stack at the time we reach the first "[" when scanning "{ ( ( ) ( ) ( [ ] ) ) }"?
SolutionAnswer: { ( (
-
Fill in the blanks in the following block of code that reverse the items in an int_queue q by using a intermediate int_stack s.
void reverse_queue(int_queue q) { int_stack s; while (!q.empty()) { int a = q.front(); q.dequeue(); s.push(a); } while (________) { ______________; ______________; ______________; } }Solutionvoid reverse_queue(int_queue q) { int_stack s; while (!q.empty()) { int a = q.front(); q.dequeue(); s.push(a); } while (!s.empty()) { int a = s.top(); s.pop(); q.enqueue(a); } }