Welcome! Log In Create A New Profile

Advanced

Assignment 2: Question 11

Posted by brettc 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
avatar Assignment 2: Question 11
April 30, 2009 02:49PM
Can we add our own member functions to the queueType class?

Would be great to have a getElement(int i) function that returns the element at position i.
avatar Re: Assignment 2: Question 11
May 03, 2009 06:36PM
Wouldn't that destroy the whole purpose of a queue?

Imagine you're a teacher and you've got your class standing in a line in front of you outside your class. How would you remove all the sick pupils from the line? Remember that you can only interact with the pupil in front.

Maybe that analogy will help you answer the question.
Re: Assignment 2: Question 11
May 03, 2009 07:13PM
Ah yeah, I suppose you're right, thanks smiling smiley
avatar Re: Assignment 2: Question 11
May 25, 2009 10:44PM
BTW, how do one know that you have processed all the elements in the queue? There is no member to get the size of the queue. Unless this function they want is a friend, I can't see how.

Current algorithm:

For the number of items in the queue left not yet processed

Get the front element
Delete from the queue

If not the same as the one to remove
Add to the back of the queue
End if

End


Unless one use a temporary queue in the removeX function, which maybe clumsy.
avatar Re: Assignment 2: Question 11
May 26, 2009 07:17AM
@Henry

If using a linked list, check for Null.
If an array, use a counter to track and not exceeding array size.
avatar Re: Assignment 2: Question 11
May 26, 2009 10:47AM
Not to worry. I did it the easy way. No friends required.

Language: C++
template <typename T> void removeX(queueType<T>& Q, const T& x) { queueType<T> newQ;   while (! Q.isEmptyQueue()) { T frontElement = Q.front(); Q.deleteQueue();   if (frontElement != x) { newQ.addQueue(frontElement); } }   Q = newQ; }
avatar Re: Assignment 2: Question 11
May 26, 2009 01:27PM
Your logic is reversed. Instead of deleting x, you're deleting everything except x.

[Edit]

Sorry, I misread the last if statement. You're deleting everything.
avatar Re: Assignment 2: Question 11
May 26, 2009 03:07PM
Yes, I am clearing out the Q given in the parameter, and then only copy elements not equal to x to the new Q. Then I assign the new Q to the old. I can do this because the parameter is not a constant reference. The method signature is as given by the assignment question, so I guess I am not breaking any rules.
Sorry, only registered users may post in this forum.

Click here to login