Share
Explore BrainMass

Queues C++

Consider the following statements:

stackType<int> stack;
queueType<int> queue;
int x;

Suppose the input is:
15 28 14 22 64 35 19 32 7 11 13 30 -999

Show what is output by the following segment of code.

stack.initializeStack();
queue.initializeQueue();
stack.push(0);
queue.addQueue(0);
cin>>x;

while(x != -999)
{
switch(x % 4)
{
case 0: stack.push(x);
break;
case 1: if(!stack.isEmptyStack())
{
cout<<"Stack Elment = "<<stack.top()<<endl;
stack.pop();
}
else
cout<<"Sorry, the stack is empty."<<endl;
break;
case 2: queue.addQueue(x);
break;
case 3: if(!queue.isEmptyQueue())
{
cout<<"Queue Element = "<<queue.front()<<endl;
queue.deleteQueue();
}
else
cout<<"Sorry, the queue is empty."<<endl;
break;
}//end switch

cin>>x;
}//end while

cout<<"Stack Elements: ";
while(!stack.isEmptyStack())
{
cout<<stack.top()<<" ";
stack.pop();
}

cout<<endl;
cout<<"Queue Elements: ";
while(!queue.isEmptyQueue();
{
cout<<queue.front()<<" ";
queue.deleteQueue();
}
cout<<endl;

See attached file for full problem description.

Attachments

Solution Summary

Queues C++ are highlighted.

$2.19