Exam 2 + Answers
Exam 2 + Answers
Question 1:
Show the contents of a stack for the following sequences of operations: push(A), push(B), push(C),
pop(), push(D), push(E), pop(), push(F), push(G). Assume an initial size of 6 for the array
implementation. You also need to indicate the value of top at the end.
Ans 1.
Initially stack is empty so top = -1.
First operation is push(A), this operation push element A at the top of the stack
as:
top = top + 1 = -1 + 1 = 0
stack[0] = A
Second operation is push(B), this operation push element B at the top of the
stack as:
top = top + 1 = 0 + 1 = 1
stack[1] = B
Third operation is push(C), this operation push element C at the top of the stack
as:
top = top + 1 = 1 + 1 = 2
stack[2] = C
C
B
Fourth operation is pop(), this operation pop the top element from the array as:
top = top - 1 = 2 - 1 = 1
Fifth operation is push(D), this operation push element D at the top of the stack
as:
top = top + 1 = 1 + 1 = 2
stack[2] = D
Sixth operation is push(E), this operation push element E at the top of the stack
as:
top = top + 1 = 2 + 1 = 3
stack[3] = E
D
B
Seventh operation is pop(), this operation pop the top element from the array as:
top = top - 1 = 3 - 1 = 2
Eighth operation is push(F), this operation push element F at the top of the stack
as:
top = top + 1 = 2 + 1 = 3
stack[3] = F
Nineth operation is push(G), this operation push element G at the top of the
stack as:
top = top + 1 = 3 + 1 = 4
stack[4] = G
G
F
Question 2:
Assume that stack s has stack type as int, and stack-capacity = 6. Give the
contents of top & all arrays. Data members of s after the code segment is
executed:
stack s;
s.push (123);
s.push (456);
s.pop();
s.push(789);
s.pop();
Question 3:
Assume that stack s has stack type as int, and stack-capacity = 6. Give the
contents of top & all arrays. Data members of s after the code segment is
executed:
stack s;
s.push (101);
int i = s.top();
s.push (218);
s.pop();
s.push (2*4+i);
Question 4:
Assume that stack s has stack type as int, and stack-capacity = 6. Give the
contents of top & all arrays. Data members of s after the code segment is
executed:
stack s;
s.push( 2*i+3);
s.pop();
s.pop();
s.push(i*3 + 3);
Question 5:
Convert the following arithmetic expressions from infix to postfix & prefix:
((a * b – c / d * e) + f % g * (h * k))
Question 6:
Show the contents of a circular queue for the following sequences of operations: enqueue(A),
enqueue(B), enqueue(C), dequeue(), enqueue(D), enqueue(E), dequeue(), enqueue(F), enqueue(G).
Assume an initial size of 6 for the array implementation.
Ans 2:
front = front + 1 = -1 + 1 = 0
rear = rear + 1 = -1 + 1 = 0
cqueue[0] = A
front = 0
rear = rear + 1 = 0 + 1 = 1
cqueue[1] = B
A B
Third operation is enqueue(C), this operation enqueue element C at the rear end
of the circular queue as:
front = 0
rear = rear + 1 = 1 + 1 = 2
cqueue[2] = C
A B C
Fourth operation is dequeue(), this operation dequeue element from the front end
of the circular queue as:
dequeued_element = cqueue[0] = A
front = front + 1 = 0 + 1 = 1
B C
Fifth operation is enqueue(D), this operation enqueue element D at the rear end
of the circular queue as:
front = 1
rear = rear + 1 = 2 + 1 = 3
cqueue[3] = D
B C D
Fifth operation is enqueue(E), this operation enqueue element E at the rear end
of the circular queue as:
front = 1
rear = rear + 1 = 3 + 1 = 4
cqueue[4] = E
B C D E
Sixth operation is dequeue(), this operation dequeue element from the front end
of the circular queue as:
dequeued_element = cqueue[1] = B
front = front + 1 = 1 + 1 = 2
C D E
front = 2
rear = rear + 1 = 4 + 1 = 5
cqueue[5] = E
C D E F
front = 2
rear = 0
cqueue[0] = G
G C D E F
Question 7:
Write a C++ code (not a full program) to reverse a linked list. For example, if the list is {4,5,6},then after
reversing, it becomes {6,5,4}. (Hint: you can use a stack)