10. Use the ArrayStackType and ArrayQueueType classes given to you in Handout 13
ID: 3812792 • Letter: 1
Question
10. Use the ArrayStackType and ArrayQueueType classes given to you in Handout 13 Example Programs. Consider the following statements: ArrayStackType stack; ArrayQueueType queue; int x; Suppose that the input is: 15 28 14 22 64 35 19 32 7 11 13 30 -999 What output do you expect for the following program segment (without actually executing the program in Visual C++)? stack.push(0); queue.add(0); cin >> x; while ( x != -999 ) { switch (x % 4) { case 0: stack.push(x); break; case 1: if ( !stack.isEmpty() ) { cout << "Stack Item = " << stack.top() << endl; stack.pop(); } else cout << "Sorry, the stack is empty." << endl; break; case 2: queue.add(x); break; case 3: if ( !queue.isEmpty() ) { cout << "Queue Item = " << queue.front() << endl; queue.remove(); } else cout << "Sorry, the queue is empty." << endl; break; }//end switch cin >> x; }//end while cout << "Stack Items: "; while ( !stack.isEmpty() ) { cout << stack.top() << " "; stack.pop(); } cout << endl; cout << "Queue Items: "; while ( !queue.isEmpty() ) { cout << queue.front() << " "; queue.remove(); } cout << endl;
Explanation / Answer
Stack : top = 0
Queue : Front = Rear = 0
Switch cases
Stack Items : 64 28 0
Queue Items : 30
Input Operation Stack /Queue Change 15%4 = 3 Queue Item = 0 Queue:Empty 28%4 =0 push(28) Stack : 28 0 14%4 = 2 queue.add(14) Queue: 14 22%4 = 2 queue.add(22) Queue : 14 22 64%4 = 0 push(64) Stack : 64 28 0 35%4 = 3 Queue Item = 14 Queue : 22 19%4 = 3 Queue Item = 22 Queue : Empty 32%4 = 0 push(32) Stack 32 64 28 0 7%4 = 3 Sorry,the queue is empty 11%4 = 3 Sorry,the queue is empty 13%4 = 1 Stack Item = 32 Stack 64 28 0 30%4 = 2 queue.add(30) Queue : 30 -999 quitRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.