
Re: Some hints needed to improve my code for queues
Aswin,
Looking at #define was a good idea. To understand how it works, just keep in mind that macros created by #define are pre-processor commands. That means they run before the compiler does. Essentially, when you have a #define, every place in the code that references the macro gets replaced before the compiler runs.
Here is an example:
In the code above, I've created a 'DTYPE' macro, which defines the data type I will be using. Then, any place I want to use the data type, I just use the macro. If you run this code as is, you'll notice the code correctly behaves as if the 'PiVal' variable was an integer.
Next, try switching the first line to: "#define DTYPE float". (You also need to change the display line to "%f" instead of "%d". You'll notice the code now works with the data being represented by a float.
The same thing can be done in your queue code:
That would allow you to switch the data type. As you mentioned, you would only be able to use one data type per program.
As a sidenote, is this code in progress? I notice that it doesn't compile due to references to a "queue.sum" member of the struct that isn't defined. Also, it seems unlikely that moving both the start and end indices in the 'add' routine is what you would want to be doing. Your 'get' routine is interesting as well. You allow people to access any element of the queue? Not just the one at the front?
Best of luck in your work.