An operation that displays the contents of a queue can be useful during program
ID: 3930793 • Letter: A
Question
An operation that displays the contents of a queue can be useful during program debugging. Add a display operation to the ADT queue such that display uses only ADT queue operation, so it is independent of the queue's implementation; display assumes and uses the pointer-based implementation of the ADT queue. Consider the following C++ function f, which calls the function swap. Assume that swap exists and simply swaps the contents of its two arguments. Do not be concerned with f s purpose. How many comparisons does f perform? void f(int theArray[], int){for (int j = 0; jExplanation / Answer
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.