An operation that displays the contents of a queue can be us
Solution
Problem 5:
a.
Since the queue can be implemented in any way as mentioned in the question, I made an assumption that the queue implements some method say get(i)
that gets the elements from the queue.
void display(){
if(queue.head==NULL){ //If queue\'s head == null, then it is empty. Then printout empy queue\'
cout <<\"Queue empty\";
}
else{ //else get each element of queue using get() method
while (queue.get(i) !=NULL){
cout<<queue.get(i);
}
}
}
b.
Assuming display uses pointer based implementation of the ADT, then we iterate by accessing the next pointer after next pointer till we get a null.
void display(){
node *temp;
temp=head;
if (temp == NULL){
cout <<\"Queue empty\";
}
else{
while(temp != NULL){
cout <<temp->data<< \" \";
temp=temp->next;
}
}
}
Problem 6:
Looking at the two for loops we find that for the first for loop, it goes from 0 to n. As j goes from 0 to n, i goes from 0 to i. Therefore, for jth inner for loop, there
are total j comparisons. Hence, the sum of these = 1 + 2 + 3 + ... + n
= n*(n+1)/2
