Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Course # : CSCI528
Professor : Dr. Rajagopalan
Topic : Final Exam
Ex 6.4
Q3:
Algorithm to insert a node before the last of a linked list, whose first node is pointed to by
“first”
1. Traverse to the end of the list by maintaining two other “temporary” nodes which
remember the address of the pointer.
while (first->next != NULL)
2. One of the nodes should point to a previous address than the current node.
3. If the current node is the last node, we know that the other temporary node contains
the address of the node where the insertion needs to happen.
4. Once the point of insertion is found, set the newnode equal to the head of the old
node
newnode=temporary_node2->next;
Q4:
Algorithm to check if the data items in a linked list whose first node is pointed to by “first”
are in ascending order
int flag=1;
4. As per above example, if the value of the flag is 1, then the linked list is in ascending
order, otherwise it is not.
1
Ex 6.5
Q9: p4 = p1
Now p1 and p4 are both going to point to the first node (Cat)
2
Ex 6.6
while ( temp1 )
{
*List = temp1;
temp2= temp1->pNext;
temp1->pNext = temp3;
temp3 = temp1;
temp1 = temp2;
}
return *List;
}
Ex 7.2
Q5a:
Permutations to arrange rail road cars from right to left track (n=3)
Q5b:
Permutations to arrange rail road cars from right to left track (n=4)
3
7. Move 1, push 2, push 3, move 4, pop 3, pop 2
a. 1, 4, 3, 2
There may be several other combinations. Some are not possible for example:
1, 3, 2, 4 and 1, 4, 2, 3.
Ex 9.4
Q9:
Contents of vector:
Since v is not declared, it will always have a value less than number. Therefore, the while
loop will run until each of the elements of the number vector have been erased and we have
an empty vector with a NULL value.
Q10:
This will delete all values > 25 in the vector. Therefore the remaining vector will be:
22 11
Ex 10.1
Q3:
Output: 10
Q5:
Output: 67
Q9:
Output:10
Q11:
20 characters will be printed including the new line character
Q12:
This function calculates the sum of all integers from 1 to n. i.e. (1+2+3+….+n)
Ex 10.6
Q4:
Write a recursive function to calculate n!.
int factorial(n) {
if (n==1)
return n;
else
return (n*factorial(n-1));
}
Example:
Cout << factorial (4);
1st Recursion: 4 x …
2n Recursion: 3 x …
3rd Recursion: 2 x …
4th Recursion: 1
4
Result: 1 x 2 x 3 x 4
Ex 16.1
Q5:
A B C D E
A 0 1 0 1 0
B 1 1 1 0 0
C 0 0 0 0 1
D 0 1 0 0 1
E 0 0 0 0 0
Possible Edges
AB
AD
BC
CE
DB
DE
5
Ex 15.3
Q20:
A B C D E F G H I
65 66 67 68 69 70 71 72 73
Q21: