Data Structures - CS301 Spring 2004 Mid Term Paper Session 1-Edited
Data Structures - CS301 Spring 2004 Mid Term Paper Session 1-Edited
com
WWW.VUTUBE.EDU.PK
http://forum.vupages.com
Largest Online Community of VU Students
H
MIDTERM EXAMINATION
Total Marks:75
SEMESTER SPRING 2004
CS301-DATA STRUCTURE Duration: 60min
Instructions
Please read the following instructions carefully before attempting any question:
Answer:
6.2+5/3
6.2+1 (As 5 & 3 are integers so result will be in integer, fraction portion will be discard)
7.2
Question No: 2 Marks: 5
Answer:
Input op1 op2 value stack
8 8
6 6
8
4 4
6
8
+ 6 4 10 10
8
3 3
10
8
* 10 3 30 30
8
+ 8 30 38 38
5 5
38
+ 38 5 43 43
6+ (5 - 3 ) * 8
Answer: 6 5 3 - 8 * +
class Node
{
public:
Node(const string& s, Node* ptr)
: info(s), next(ptr)
{}
private:
string info;
Node* next;
};
Write a function that changes every ’t’ that occurs as the first letter of a word to a ’b’. No other letters
should change. For example, ("tin", "tile", "ant", "saint", "tot") should be changed to ("bin", "bile",
"ant", "saint", "bot")
Answer:
b) Add nodes containing “black” and “panda” so that the tree remains a search tree. Add “black”
first. Draw the nodes attached to the tree diagram above.
c) Draw a search tree in which “teddy” is at the root of the tree, and the root’s left child is “polar”
(include all other nodes from the tree in the diagram above, these other nodes can occur in any
order in the tree you draw.)
Answer
Draw a box and pointer diagram (i.e., a linked list) of what list points to after executing the following code
fragment:
string* name;
Stack<string*> s;
Node* list = NULL;
s.push(new string("Naveed"));
s.push(new string("Mahmood"));
s.push(new string("Mohsin"));
s.push(new string("Aijaz"));
s.push(new string("Pervaiz"));
while (!s.empty())
{
name = s.pop(name);
list = new Node(name, list);
}
Answer:
Answer:
4
( ::: )
[ ::: ]
{::: }
In a properly formed program, these characters will be properly nested and matched. To determine
whether this condition holds for a particular program, you can ignore all the other characters and look
simply at the pattern formed by the parentheses, brackets, and braces. In a legal configuration, all the
operators match up correctly, as shown in the following example:
{ x = ( s = v[ 1 ] + 2 ); y = 4 * ( v [ v.size() - 1 ] + x ); }
bool isBalanced(string s)
that takes a string s with all characters except the bracketing operators removed. For example, for the
program statements
{ x = ( s = v [ 1 ] + 2 ); y = 4 * ( v [ v.size() - 1 ] + x ); }
The method should return true if the bracketing operators in s are balanced, which means they are
correctly nested and aligned, otherwise it should return false. You must either use a Stack or recursion in
your solution. Assume you have the following helper functions.
boolean isBalanced(String s)
{
Answer:
bool is Balanced(string s)
{
char ch, stch;
int i =0;
while(s[i]) // untill the end of the string
{
ch = s[i]; // pick the character
if( boolisOpener(ch); // if it is opener then push on the stack
st.push(ch);
else if( boolisCloser(ch) // if it is closer then pop the last char. from stack
{ stch = st.pop();