Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Stack of Integers - Data Structures - Exam, Exams of Data Structures and Algorithms

Main points of this exam paper are: Stack of Integers, Implementation, Same Number, Stack Methods, Unchanged, Public Methods, Array Index

Typology: Exams

2012/2013

Uploaded on 04/07/2013

sethuraman_h34rt
sethuraman_h34rt ๐Ÿ‡ฎ๐Ÿ‡ณ

4.3

(8)

159 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures, Sample Test 2, with Answers
1. (a) Consider the grammar which defines the language of all validwords:
<validword> = <letter><validword> | <letter>RS | Z
<letter> = 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)))
2. Recall the array-based implementation of class template Stack has the
following data members:
template <class T>
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 <class T>
void Stack<T>::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 <class T>
bool Stack<T>::operator== (const Stack<T>& 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).
pf3
pf4
pf5

Partial preview of the text

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

  1. (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)))
  2. 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.

  1. 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:

  1. 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

  1. (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+*-
  2. (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.)

  1. (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

  1. 7

    14
  2. (a) int sum = 0; while (!cStack.isEmpty()){ int x; cStack.pop(x); sum += x;