Download Stack of Integers - Data Structures - Exam and more Exams Data Structures and Algorithms in PDF only on Docsity!
Data Structures, Sample Test 2, with Answers
- (a) Consider the grammar which defines the language of all validwords: = | RS | Z = A | B | C Circle all the following strings which are in the language of all validwords: AABC RS ARS ABACCRS RSZ ACCRZ ZZ Z CABZ (b) If a = 10, b = 3, c = โ2, evaluate the postfix expression aa+bc-* (c) Convert the following fully parenthesized infix expression to postfix. (x-((x+y)*((z/r)+p)))
- Recall the array-based implementation of class template Stack has the following data members:
template class Stack{ // public function declarations here ... private: static const int MAX_STACK = 10; T items[MAX_STACK]; int top; /* array index of top element of the stack; is -1 when stack is empty */ };
(a) Write a member function template void Stack::push(T newItem) throw(StackException) which pushes newItem onto the stack and throws an exception if the stack is full. (b) Write a member function template bool Stack::operator== (const Stack& rhsStack) const which checks to see if two stacks are equal (in other words, they have the same number of items, and all these items, from the top to the bottom, are equal).
(c) A stack of integers aStack has the following private data: top: 4 items: 8 0 0 4 7 10 -34323 0 67823 - What is the output of the following code? int x; while (!aStack.isEmpty()){ aStack.pop(x); cout << x << " "; } Hint: Recall that top is the array index of the top of the stack.
- A queue aQueue of strings contains the following words (separated by commas, front to back): car, house, pig, car, hill What is the status of the queue each of the following actions is taken in succession?
(a) aQueue.dequeue(st) Answer: (b) aQueue.enqueue("nail") Answer: (c) aQueue.getFront(st) Answer: (d) aQueue.enqueue(st) Answer: (e) aQueue.dequeue() Answer:
- A stack bStack contains the following items
7 8 โ 3 14 5
What is the output to the screen of the following code?
int x; while (!bStack.isEmpty()){ bStack.pop(x); if (x>0 && !bStack.isEmpty()) bStack.pop(); cout << x << endl;
Answers
- (a) Only ARS, ABACCRS, Z, and CABZ are in the language:
- AABC is not in the language since all string in the language must end in RS or Z.
- RS is not in the language, since RS must be preceded by a letter.
- ARS is in the language, as we may calculate = RS = ARS
- ABACCRS is in the language, as we may calculate = = A = A = AB = AB = ABA = ABA = ABAC = ABACRS = ABACCRS
- RSZ is not in the language, since Z must be preceded only by a letter.
- ACCRZ is not in the language for the same reason.
- ZZ is not in the language for the same reason.
- Z is in the language, since we have = Z
- CABZ is in the language, since we have = = C = C = CA = CA = CAB = CABZ (b) aa+bc-* is (20)bc-* is (20)5* is 100 , since aa+ is 10 + 10 = 20, bc- is 3 โ (โ2) = 5 and (20)5* is 20 โ 5 = 100. (c) xxy+zr/p+*-
- (a) template void Stack::push(T newItem) throw(StackException){ if (top == MAX_STACK-1) throw StackException("StackException: stack is full in push"); else{ ++top; items[top] = newItem; }
(b) template bool Stack::operator== (const Stack& rhsStack) const{ if (top != rhsStack.top) return false; // must have same number of elements // (which is top+1) for (int i=0; i<=top; ++i) // only check items up to top, // garbage values beyond top are // irrelevant if (items[i] != rhsStack.items[i]) return false; // end if, end for return true; } (c) 7 4 0 0 8 (Recall that the array index top starts counting from 0, not 1. So a stack whose top is 4 will have 5 items, not 4.)
- (a) aQueue.dequeue(st) Answer: house, pig, car, hill
(b) aQueue.enqueue("nail") Answer: house, pig, car, hill, nail (c) aQueue.getFront(st) Answer: house, pig, car, hill, nail (d) aQueue.enqueue(st) Answer: house, pig, car, hill, nail, house (e) aQueue.dequeue() Answer: pig, car, hill, nail, house
7
14 - (a) int sum = 0; while (!cStack.isEmpty()){ int x; cStack.pop(x); sum += x;