



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
The solutions to problem set 2 of cs335, which covers stack and queue data structures. The problems involve analyzing the state of a stack after executing certain lines of pseudocode, printing out positive and negative integers from a queue in a specific order, finding a path between cities using the hpair algorithm, and identifying strings in a given grammar. The document also includes c++ code for implementing stack functions push and isempty.
Typology: Exams
1 / 6
This page cannot be seen from the preview
Don't miss anything!
Answers
(1) (10 pts) What does the initially empty stack aStack “look like” after each line of the following pseudocode? aStack.push(9) /Line 1/ aStack.push(8) /Line 2/ aStack.push(9) /Line 3/ aStack.pop() /Line 4/ if (!aStack.isEmpty()) aStack.push(7) /Line 5/ aStack.pop(y) /Line 6/ aStack.push(10)/Line 7/ aStack.push(y) /Line 8/ Write the state of the stack after the given line has executed under the label for that line in the space below. Solution:
Line 1
Line 2
8 9
Line 3 9 8 9
Line 4
8 9
Line 5
7 8 9
Line 6
Line 7
10 8 9
Line 8 7 10 8 9
1
(2) (10 pts) Given a queue of integers iQueue, and using only the methods of the ADT Queue (enqueue, dequeue, isEmpty, etc.), write code which prints out the all the positive integers in the queue, first, in order. Then the code should print out all the negative (and 0) integers, again in the order in which they ap- peared in the original queue. All items should be dequeued from iQueue by the end of your code. So if iQueue initially contains (from front to back) 4 , 5 , − 9 , 9 , 9 , 0 , − 7 , 1 , the output to the screen should be 4 5 9 9 1 -9 0 - Hint: You need a loop to go through and dequeue all of iQueue first. The positive integers should be printed out immediately, and the nonpositive ones stored for later printout. How should the negative (and 0) integers be stored (in a list, an array, a stack, or a queue)? Your answer may be in C++ code or (pre- cise) pseudocode. Solution: int x; Queue
Answer for Problem 3: Solution: The path found is E,C,I,B,G. Here is the value of the stack at each step: G B B D I I I C C C C C C E E E E E E E (4) (10 pts) Consider the language the following grammar defines:
=
SLAB A 2 2A1BB B22 DSS 1BA
Solution: Only A, 2A1BB, and 1BA are in the language:
(5) (10 pts) Recall the data members for the pointer-based imple- mentation of class Stack class Stack{ /* public members here ... / private: struct StackNode{ StackItemType item; StackNode next; }; // end struct StackNode StackNode* topPtr; }; // end class Stack (a) Write a C++ implementation of the member function push, which pushes an item onto the top of the stack (you don’t have to worry about error handling). Here is the first line of the function definition: void Stack::push(StackItemType newItem) Solution: void Stack::push(StackItemType newItem){ StackNode* newPtr = new StackNode; newPtr->item = newItem; newPtr->next = topPtr; topPtr = newPtr; } (b) Write a C++ implementation of the member function isEmpty of class Stack. Here is the first line: bool Stack::isEmpty() const Solution: bool Stack::isEmpty() const{ return topPtr == NULL; }