Sei sulla pagina 1di 6

Course Name : Data Structures

Course # : CSCI528
Professor : Dr. Rajagopalan
Topic : Final Exam

Submitted By : Saahil Goel (sgoel3376@unva.edu)


Student ID : 110-00-3376
Date Submitted : September 16, 2010

Ex 6.4

Q3:
Algorithm to insert a node before the last of a linked list, whose first node is pointed to by
“first”

Let the node to be inserted called “newnode”

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;

5. Set newnode->next equal to the first temporary node (last node)


newnode->next = temporary_node1;

Q4:
Algorithm to check if the data items in a linked list whose first node is pointed to by “first”
are in ascending order

1. Traverse the linked list using a outer while loop.


2. Check if each element is greater than the previous element.
3. If anywhere this statement is not met, break the loop.

int flag=1;

while (first->next != NULL) {


temp = first;
first=first->next;
if (temp > first) {
break;
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

Q8: p1 = p2 -> next


In this case, p1 will point to the value currently being pointed to by p3 – i.e. p1 will now
point to Ewe.

Q9: p4 = p1
Now p1 and p4 are both going to point to the first node (Cat)

Q10: p4->data = p1-> data


The data contained in the node referenced by p4 will now contain Cat (instead of Rat)

Q11: p4->next->data = p1->data


There will be an error in this case since p4 is pointing to the last element in the linked
list. Since there is no next value to refer, this operation will fail.

Q14: p1->next = p3->next;


p1 = p3;
The first node (Cat) will now point to the last node (Rat). Then by the next statement,
the value of the first node (Cat) will be replaced by the value of the last node (Rat).
Therefore both nodes will point to a data value of Rat.

Q16: p4->next = p3->next;


P3->next = p2->next;
P2->next = p1->next;
This operation will reverse the linked list where the first node becomes p4 and the last
node becomes p1.

Q17: p4->next = p3;


p4->next->next = p2;
p4->next->next->next = p1;
p1 = 0;
First, p4 will point to p3 (Ewe). Then the last node (Rat) will point to p2. Then the Dog
value will point to Cat and Cat will point to NULL (or 0).

This is how the new linked list will look

Rat -> Ewe -> Dog -> Cat

2
Ex 6.6

Q9: Reverse a linked list called List

Node* ReverseList( Node ** List )


{

Node *temp1 = *List;


Node * temp2 = NULL;
Node * temp3 = NULL;

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)

1. Push 1, push 2, move 3, pop2, pop1


a. Order: 3, 2, 1
2. Move 1, Move 2, Move 3
a. Order: 1, 2, 3
3. Move 1, Push 2, Push 3, Pop 3, Pop 2
a. Order: 1, 3, 2
4. Push 1, Move 2, Push 3, Pop 3, Pop 1
a. Order: 2, 3, 1

Orders 3, 1, 2 and 2, 1, 3 are not possible

Q5b:
Permutations to arrange rail road cars from right to left track (n=4)

1. Push 1, push 2, push 3, move 4, pop 3, pop 2, pop 1


a. Order: 4, 3, 2, 1
2. Move 1, move 2, move 3, move 4
a. 1, 2, 3, 4
3. Move 1, move 2, push 3, push 4, pop 4, pop 3
a. 1, 2, 4, 3
4. Move 1, push 2, move 3, push 4, pop 4, pop 2,
a. 1, 3, 4, 2
5. Push 1, push 2, move 3, move 4, pop 2, pop 1
a. 3, 4, 2, 1
6. Push 1, move 2, move 3, move 4, pop 1
a. 2, 3, 4, 1

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:

(Please zoom document to see in detail)

Potrebbero piacerti anche