Sheet1,2 Stack Queue
Sheet1,2 Stack Queue
1
6- Evaluate the following expressions:
123+*45*6++
123+*45*+6+
12+3*45*6++
7- Trace the following code. (You may refer to the header file of the queue
implementation.) User input is the following sequence of numbers: 4 5 67 89 21 3 0
76.
int main(){
Queue q;
int i;
cin>> i;
while (i != 0)
{ if (i < 35)
q.enQueue(i);
cin>>i; }
while (!q.isEmpty())
{ q.deQueue();
cout<<i<<endl;}}
2
11- Suppose you have a stack in which the values 1 through 5 must be pushed on the
stack in that order, but that an item on the stack can be popped at any time. Give a
sequence of push and pop operations such that the values are popped in the
following order:
(a) 2, 4, 5, 3, 1
(b) 1, 5, 4, 2, 3
(c) 1, 3, 5, 4, 2 It might not be possible in each case.
12- (a) Suppose you have three stacks s1, s2, s2 with starting configuration shown on
the left, and finishing condition shown on the right. Give a sequence of push and
pop operations that take you from start to finish. For example, to pop the top
element of s1 and push it onto s3, you would write s3.push ( s1.pop()).
(b) Same question, but now suppose the finish configuration on s3 is BDAC (with B
on top) ?