Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Enqueue in C (algorithm)

Posted on 2004-11-22
5
Medium Priority
?
727 Views
Last Modified: 2008-03-10
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??



0
Comment
Question by:wiggs1006
5 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 12650038

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
 
LVL 2

Author Comment

by:wiggs1006
ID: 12650244
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
 
LVL 3

Accepted Solution

by:
Indrawati earned 2000 total points
ID: 12650845
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
 
LVL 7

Expert Comment

by:aib_42
ID: 12650899
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
 
LVL 2

Author Comment

by:wiggs1006
ID: 12651333
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question