Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following statements: linkedQueueType queue; linkedStackType stack;

ID: 3693227 • Letter: C

Question

Consider the following statements: linkedQueueType queue; linkedStackType stack; where the definitions of class linkedQueueType and class UnkedStackType are on Pagexs 1257 and 1230 of the textbook. Show what is output by the following segment of code, if the input is 837298651020706 cin >> nun; nua = abs(nua); uhile (nun > 0) stack.push(num X 10); num = mm/10; > while (!stack.isEmptyStack()) queue.addQueue(stack.top()); stack.pop(); > if (!queue.isEaptyQueue()) digitl = queue.front (); queue. deleteQueueO; >while (!queue.isEmptyQueue()) digit2 = queue.front(); queue.delete QueueO; cout

Explanation / Answer

Here is the output, along with the explanation as comments:

cin >> num;       //Reads the value into num. Given num = 837298651020706
num = abs(num);   //If number is negative, converts to positive.
while(num > 0)   //Repeatedly removes the right most digit from num, and stores in stack.
{
stack.push(num % 10);
num /= 10;
}
//So, by end of this block, the elements in the stack are: 6, 0, 7, 0, 2, 0, 1, 5, 6, 8, 9, 2, 7, 3, 8.
while(!stack.isEmptyStack()) //Repeatedly removes the element from the stack, and will add to the queue, till stack becomes empty.
{
queue.addQueue(stack.top());
stack.pop();
}
//So, by the end of this block, stack will be empty, and the elements in the queue are: 6, 0, 7, 0, 2, 0, 1, 5, 6, 8, 9, 2, 7, 3, 8.
if(!queue.isEmptyQueue())   //The queue is not empty.
{
digit1 = queue.front();   //So, copies the element in the front of the queue, i.e., 6 into digit1.
queue.deleteQueue();       //Deletes the element from the front of the queue, so queue will now hold: 0, 7, 0, 2, 0, 1, 5, 6, 8, 9, 2, 7, 3, 8.
}
while(!queue.isEmptyQueue())   //Till the queue becomes empty.
{
digit2 = queue.front();       //Assign the value in the front of the queue, to digit2.
queue.deleteQueue();           //Removes the element in the front of the queue.
cout<<digit1 - digit2 << " ";   //Print digit1 - digit2, then a space.
digit1 = digit2;       //Assigns digit2 to digit1.
}
cout<<endl;    //Moves onto the next line.
//So, by the end of the loop, it will just print the difference between every consecutive elements in the queue.
//The output is: 6 -7 7 -2 2 -1 -4 -1 -2 -1 7 -5 4 -5.