

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


First Name: _______________ Last Name: _____ __________
This is a closed book examination.
insert(a); insert(b), insert(c), search(a), search(a), search(a), search(c), search(c),
NULL NULL
Head Tail
(b) (20 points) Let us perform the following operations on a queue: push(1), push(2), push(3), pop(), pop(), push(4), push(5), push(6), push(7). (i) Draw a figure to show the state of the queue. If the underlying data structure were a deque, with initial capacity 4, then show the final state of the deque too. Mention which deque operation you used to push, and which operation you used to pop.
push = push_back => pop = pop_front push = push_front => pop = pop_back
O(n 2 log n)
(b) (10 points) How many times is the statement with cout << ... executed, in the following pseudocode? Derive the exact number, showing all steps.
for i = 1 to n for j = 1 to i for k = 1 to n
c a b
cout << i << j << k << endl;
n^ i=1 i^ j=1 n^ k=1 1 = n n^ i=1 i^ j=1 1 = n n^ i=1 i = n 2 (n+1)/
void reversetranspose(Node *N){
if(N == tail) return;
N->next->prev = N->prev; if(N != head) N->prev->next = n->next; else head = N-> next;
if(N-next->next) N->next->next->prev = N; else tail = N;
N->prev = N->next; N->next = N->next->next; N->prev->next = N;
return;
}
First, implement a templated class called Stack, which implements a generic stack using an STL deque. (Unlike with the STL stack, the underlying container type will always be a deque.) The following features of a stack should be implemented: (i) void pop(), (ii) void push(T &), (iii) T &top(), and (iv) bool empty(). These functions will call appropriate functions of a deque. Use this class in a main program, which reads integers from a file called file.in. file.in (which you donโt need to write) contains one integer on each line. The main program will read each integer, and using the stack, output the values read in reverse order.
Compilation command: g++ main.cpp