Name: ________________________________
Honor Code Acknowledgment: ___________________
Random Quiz # 7
CPS 100E, Fall 1996
Due: October 31 (boo)
Problem 1: Three's Company (2 points)
Using big O notation and as precise an expression as possible, what is
the complexity of the loop below (justify your answer briefly)
int n;
cout << "enter a number ";
cin >> n;
int sum = 0;
while (n > 0)
{
sum += n % 3;
n /= 3;
}
Problem 2: What's wrong with this picture (6 points)
Describe, in one or two sentences, why each of the functions below does
NOT work correctly.
Part A
template
void ListSplit(TNode * & list, TNode * & first,
TNode * & second)
//postcondition: splits a circular list of nodes into two separate
circular lists each containing k nodes
{
TNode * current = list;
int counter = 1;
while (current->next != list)
{
counter ++;
current = current->next;
}
first = list;
int k;
for (k=1;k<=counter/2;k++)
{
first = first->next;
if (k==counter/2)
{
first->next = list;
}
}
first = list;
for (k=1;k<=counter/2+1;k++)
{
list = list->next;
}
second = list;
for (k=1;k<=counter/2;k++)
{
second = second->next;
if (k==counter/2)
{
second->next = list;
}
}
list=NULL;
}
Part B
template
void IterativeRemove(TNode * & list, const Kind & deadValue)
//postcondition: all nodes containing deadValue have been removed
// from list
TNode * temp; // temporary variable;
TNode * tempTemp; // secondary temporary variable;
TNode * start; // top of list
{
while (list && (list->info==deadValue)) //remove nodes at beginning of
//list containing deadValue
{
temp=list;
delete list;
list=temp->next;
}
while (list)
{
temp=list->next;
while (temp==deadValue)
{
tempTemp=temp->next;
delete temp;
temp=Temptemp;
}
list->next=temp;
}
if (list) {list=start};
return;
}