Jump to content

Help needed with queue searchValue (basic)

- - - - -

  • Please log in to reply
3 replies to this topic

#1
Cutter

Cutter

    Newbie

  • Members
  • Pip
  • 2 posts
Hi all, i'm glad i found this forum, i'm doing some revision and i'm stuck on a question in a past exam, it goes like this:

Posted Image

Q. Sketch the code for searching the queue for value held in the variable searchValue. Indicate what would happen if this value occurred twice in the queue.

I've gone over a ton of things, but worried what i'm doing is way off, thank you for any help.

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
What's your current best guess as to the right answer? If it's a homework assignment, I'd much rather guide you through discovering the correct answer for yourself rather than merely provide the correct answer. Does the question say which search algorithm you're supposed to be using?
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
Cutter

Cutter

    Newbie

  • Members
  • Pip
  • 2 posts
Thanks greg, sounds good to me, basically the following code:

if (length == 0) then
{
front = 0 ;
back = 0 ;
}
else
{
back = back + 1 ;
if (back == limit) back = 0 ;
}
buffer[back] = value to add into the queue ;
length = length + 1 ;

Which from what i’ve worked out, if for example, the numbers 1, 2, 3 are at buffer 2 3 and position 4

If an element numbered 8 is added to the queue, then it would be added to position 5 of the buffer, 1, 2 and three would remain at position 2, 3 and 4.

If the element 1 is removed, all of the other numbers would remain in the same place.

So back to the first post question, i’m assuming it’s asking if i search for a value it will say what buffer position the number is, and if its been repeated in another queue position.

This is where i’m a little stuck, i’m guessing i will need to use a loop to go through each buffer position to find a value?

#4
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
It's still a little vague what algorithm you're trying to implement. Can you elaborate on what buffer, queue, back, front, limit are supposed to represent?

Are you trying to keep the array sorted every time you add a value to it? From the example image in your original post, the values are not sorted, so, yes you'd have to sequentially search the entire array to determine whether a value exists and whether it is repeated somewhere.

EDIT: Am I understanding correctly that you're trying to implement a queue using the array data structure above? If that's the case, your line "if (back == limit) back = 0 ;" is going to overwrite values at the beginning of the array if you try to add more values to the queue than the array can hold. I would assume you would rather want your queue to expand the array to accommodate more values instead of overwriting them.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users