Stack and Queue

Can someone explain to me about this coding? I'm confused when user input 24, 64, 22, 7, 13.

The correct output I should get is:

Output:

Queue Element=0
Queue Element=14
Queue Element=22
Sorry, the queue is empty
Sorry, the queue is empty
Stack Element=32
Stack Elements=64 28 0
Queue Elements=30

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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 written by the following segment of code:

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 Element=”<< stack.top()
			cout<<endl;
			stack.pop();}
else 
			cout<<”Sorry, the stack is empty.”
			cout<<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;}}
		cin>>x;
		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;
Last edited on
[quoteCan someone explain to me about this coding? I'm confused when user input 24, 64, 22, 7, 13.

The correct output I should get is:

Output:

Queue Element=0
Queue Element=14
Queue Element=22
Sorry, the queue is empty
Sorry, the queue is empty
Stack Element=32
Stack Elements=64 28 0
Queue Elements=30][/quote]
Why would you expect that output with the input you specified? The only numbers I see the same are 22, and 64 you are not showing any of the other numbers you inputted in your output.

What output do you actually get with the input you provided?
Topic archived. No new replies allowed.