Uploaded by SNDLATHI

COS2611 ASSIGNMENT 2

advertisement
C MOYENI
68732759
68732759
COS2611
ASSIGNMENT 2
UNIQUE NUMBER : 630015 S2
COS2611 ASSIGNMENT 2
C MOYENI
68732759
COS2611 ASSIGNMENT 2
QUESTION 1
Oderedlinkedlisttype
template<class>
void deleteOc (orderedLinkedList<Type> & L1, const orderedLinkedList<Type> & L2)
{
nodeType<Type> * current1 = L1.first;
nodeType<Type> * current2 = L2.first;
bool check = false;
while(current1 != NULL && current2 != NULL){
while(current2 !=NULL && check){
if(current1->info < current2->info)
current2 = current2->link;
else
if(current1->info >= current2->info){
if(current1->info == current2->info)
L1.deleteNode(current1->info);
check=true;
}
}
current1 = current1->link;
}
}
QUESTION 2
RECURSION
template <class Type>
bool identicalS (stackType<Type> S1, stackType< Type > S2)
{
C MOYENI
68732759
if(S1.isEmptyStack() && S2.isEmptyStack())
return true;
else if (S1.isEmptyStack() || S2.isEmptyStack())
return false;
else if (S1.top() != S2.top())
return false;
else
{
S1.pop();
S2.pop();
return identicalS(S1, S2);
}
}
QUESTION 3
queueType
template <class Type>
void queueType<Type>::removeFirst(queueType<Type>& Q, const Type& x)
{
assert(!Q.isEmptyQueue());
int m = Q.queueFront;
bool found = false;
while (m<=Q.queueFront+Q.count-1 && !found)
{
if (Q.list[m%Q.maxQueueSize] == x)
{
found = true;
COS2611 ASSIGNMENT 2
C MOYENI
68732759
COS2611 ASSIGNMENT 2
if (m%Q.maxQueueSize != Q.queueRear)
{
for (int n=m%Q.maxQueueSize; n<Q.queueFront+Q.count-1; n++)
{
Q.list[n%Q.maxQueueSize] = Q.list[(n+1)%Q.maxQueueSize];
}
}
Q.count--; // you have deleted an item
Q.queueRear--; // so update count and queueRear
if (Q.queueRear<0)
{
Q.queueRear = Q.maxQueueSize-1;
}
}
m++; // move to next item in the queue
}
}
QUESTION 4
SORTING
a) Selection sort
5
1
1
1
1
1
1
1
18
18
2
2
2
2
2
2
3
3
3
2
2
2
2
2
2
2
18
18
3
3
3
3
14
14
14
14
14
5
5
5
10
10
10
10
10
10
6
6
1
5
5
5
5
14
14
10
6
6
6
6
6
6
10
14
2
2
2
3
18
18
18
18
C MOYENI
68732759
COS2611 ASSIGNMENT 2
b) Insertion sort
5
5
3
2
2
2
1
1
1
18
18
5
3
3
3
2
2
2
3
3
18
5
5
5
3
3
2
2
2
2
18
14
10
5
5
3
14
14
14
14
18
14
10
6
5
10
10
10
10
10
18
14
10
6
1
1
1
1
1
1
18
14
10
6
6
6
6
6
6
6
18
14
2
2
2
2
2
2
2
2
18
c) Quick sort
5
1
1
1
1
18
2
2
2
2
3
3
2
2
2
2
5
3
3
3
14
14
5
6
5
10
10
14
5
6
1
18
10
10
10
6
6
6
14
14
2
2
18
18
18
Download