• C

Enqueue in C (algorithm)

I have the following algorithm:  

if(queue->rear == queue-maxsize)
   queue->rear = 0;
   queue-queueAry[queue->rear] = itemPtr;

All that this is doing is checking to see if the queue is full and then inserting the item at the beginning of the queue if it is.  The thing is, I need to do this without using an if statement, any suggestions??



LVL 2
wiggs1006Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenData Warehouse Architect / DBACommented:

Hmmm....  You want to insert the item at the front of the queue if the queue is full?  Seems like a VERY strange thing to do...

However, you don't need an if() statement to insert an item to the front of a list.

  Item = malloc (sizeof (Item_t));
  Item->next = HeadOfList;
  HeadOfList = Item;


Kent
0
wiggs1006Author Commented:
The queue is not full it is just at the end and there is empty space in the front so that is where we need to insert the new item.  Without the if statement.  Does this make more sense?
0
IndrawatiCommented:
You can use the modulus operator for this. E.g.:

queue->rear = (queue->rear + 1) % (queue->maxsize + 1);
queue->queueAry[queue->rear] = itemPtr;

thus queue->rear will never be greater than queue->maxsize
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aib_42Commented:
Side note:

Please don't let yourself get used to solving problems without basic tools (such as the if statement). Real life problems are hardly ever solved this way; this is merely academic nonsense.
0
wiggs1006Author Commented:
I am holding a lab for undergraduates and the teacher is having them come up with another way to do enqueue without the if statement.  I totally agree with you here, and it stumped me when they came to me with questions.  I did tell them that his seemed odd to me, and at this point they are very discouraged.  It is their first introduction to data structures.  Anyways, thanks for the help, have a good Thanksgiving.


Corey
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.