You are on page 1of 5

Quiz I Calculator NOT allowed

Misr University for Sc & Tech (MUST) Term: Spring 2019 Department: Computer Science
Faculty of Information Technology Duration: 60 min Course: Data Structures, CS 201

Student Name: ………………………………… …………………………… Ideal ……………………………………………………………………. ID: ……………………………………………

1) [5 pts: 3+2] We wrote this stack-based program to check parentheses matching. The program uses three
functions: isMatchingPair which takes two parentheses and returns 1 if they match (e.g. {}) or 0 if
they don’t (e.g. {]), push which pushes a parenthesis on the stack, and pop which returns the parenthesis
on top of the stack, if any, or -1, if the stack is empty. Then we removed 2 lines from the code as shown.
void main(void)
{ char exp[100] = "({()}[]))";
int i = 0, mismatch_flag=0; char ch;
while (exp[i])

{ if ………….……………….……………….…….……………….……………….…………………….………………………….……………….……………….………………;

else if (exp[i] == '}' || exp[i] == ')' || exp[i] == ']')


{ ch = pop();
if ….…….……………….……………….…………………….

{ cout << "\nImbalance: Stack ended, while expression not";


mismatch_flag = 1;
break;}
else if (!isMatchingPair(ch, exp[i]))
{ cout << "\nImbalance: ending parenthesis without starting";
mismatch_flag = 1;
break;}
}
…………….……………….……………….………………;

}
if (mismatch_flag == 0)
if (ToS != -1) cout << "\n Imbalance: expression ended while stack not";
else cout << "\n Balanced parenthses";
}
a. Fill in the blanks to make the program complete again.
b. What does the program, after completed, print?
…………….……………….……………….………………

2) [6 pts=3x2] We wrote this function, Dequeue, to remove a node, if invoked, from a LL-based queue.
The queue is pointed to by a global pointer Tail, where new nodes can be inserted. We removed 3 lines
from the code, as shown, which you are required to write back so that the function becomes complete.
void Dequeue(void)
{ if (Tail == NULL) cout << "\n\nSorry, Queue empty\n";
else if (Tail->Next == NULL)
{
……….……………….…………………….………………………….;

cout << " \n This is 1-node Q & has been dequeued\n";


}
else
{ node * current = Tail;
while (……….……………….…………………….…………………….………………………….…………….)
current = current->Next;
delete (current->Next);
……….……………….…………………….…………………………. ;
cout << "\n Queue > 2 nodes & node has been dequeued\n"; }}
p. 1 of 2, ds_xs19q1&a.docx, nassar
3) [4 pts] An array-based circular queue of size 4 is implemented and operates as in the PPT presentation
and code you have handed out. The initial sketch of the queue is shown below. The following operations
are carried out in sequence: Enque 6, Enque 7, Enque 4, Deque, Enque 7, Deque, Deque, Enque 2, Deque,
Enque 9, Deque, Enque 6, Enque 1, Deque. Sketch the queue after each of these operations, showing
clearly the current contents and pointers (Head and Tail) position.

Good luck, Prof. H. Nassar


p. 2 of 2, ds_xs19q1&a.docx, nassar
Model Answer

p. 3 of 2, ds_xs19q1&a.docx, nassar
1) [5 pts: 3+2] We wrote this stack-based program to check parentheses matching. The program uses three
functions: isMatchingPair which takes two parentheses and returns 1 if they match (e.g. {}) or 0 if
they don’t (e.g. {]), push which pushes a parenthesis on the stack, and pop which returns the parenthesis
on top of the stack, if any, or -1, if the stack is empty. Then we removed 2 lines from the code as shown.
void main(void)
{ char exp[100] = "({()}[]))";
int i = 0, mismatch_flag=0; char ch;
i = 0;
while (exp[i])
{ if (exp[i] == '{' || exp[i] == '(' || exp[i] == '[') push(exp[i]);
else if (exp[i] == '}' || exp[i] == ')' || exp[i] == ']')
{ ch = pop();
if (ch == -1)
{ cout << "\nImbalance: Stack ended, while expression not";
mismatch_flag = 1;
break;}
else if (!isMatchingPair(ch, exp[i]))
{ cout << "\nImbalance: ending parenthesis without starting";
mismatch_flag = 1;
break;}
}
i++;
}
if (mismatch_flag == 0)
if (ToS != -1) cout << "\n Imbalance: expression ended while stack not";
else cout << "\n Balanced parenthses";
}
a. Fill in the blanks to make the program complete again.
b. What does the program, after completed, print?
Imbalance: Stack ended, while expression not
Explanation:
Stack contents are as follows, where – denotes an empty stack.
( -> ( { -> ( { ( -> ( { -> ( -> ( [ -> ( -> –
Then the last parenthesis in the input string, i.e. ), will have nothing in the stack to match with.

4) [5 pts] We wrote this function, Dequeue, to remove a node, if invoked, from a LL-based queue. The
queue is pointed to by a global pointer Tail, where new nodes are inserted. We removed 3 lines from
the code, as shown, which you are required to write back so that the function becomes complete.
void Dequeue(void)
{ if (Tail == NULL) cout << "\n\nSorry, Queue empty\n";
else if (Tail->Next == NULL)
{ Tail = NULL;
cout << " \n This is 1-node Q & has been dequeued\n";
}
else
{ node * current = Tail;
while (current->Next->Next != NULL)
current = current->Next;
delete (current->Next);
current->Next = NULL;
cout << "\n Queue has more than 2 nodes and head has been dequeued\n";
}
}

p. 4 of 2, ds_xs19q1&a.docx, nassar
5) [5 pts] An array-based circular queue of size 4 is implemented and operates as in the PPT presentation
and code you have handed out. The initial sketch of the queue is shown below. The following operations
are carried out in sequence: Enque 6, Enque 7, Enque 4, Deque, Enque 7, Deque, Deque, Enque 2, Deque,
Enque 9, Deque, Enque 6, Enque 1, Deque. Sketch the queue after each of these operations, showing
clearly the current contents and pointers (Head and Tail) position.

Recall from the PPT presentation you were handed out that:
 We insert at ‘Tail’ then increment it modulo ‘Size’, and remove from ‘Head’ then
increment it modulo ‘Size’, where here Size = 4.
 That is, ‘Tail’ is always pointing at an empty location, whereas ‘Head’ is always pointing
at an item (unless Queue is empty).
Accordingly, we show below the queue status following each Enque/Dequeue operation.

0) Initial status 4) After Dequeue 8) After: Enque 2 12) After: Enqueue 6


Head Tail Head Tail Tail Head Head
Tail

7 4 2 7 9 6

1) After: Enque 6 5) After: Enque 7 9) After: Dequeue 13) After: Enque 1


Head Tail Tail Head Head Tail
Tail Head

6 7 4 7 2 9 6 1

2) After: Enque 7 6) After: Dequeue 10) After: Enque 9 14) After: Dequeue
Head Tail Head Tail
Tail Head Tail Head

6 7 4 7 2 9 6 1

3) After: Enque 4 7) After: Deque 11) After: Dequeue


Head Tail Tail Head Head Tail

6 7 4 7 9

Good luck, Prof. H. Nassar


16 March 2019

p. 5 of 2, ds_xs19q1&a.docx, nassar

You might also like