For my C++ programming course, the prof wants us to do an order queuing program, however he hasn't taught queues and stacks yet. Is there a way I could use arrays (with dynamic memory allocation) to act as a queue? This is the
code I have so far, but I don't know where to start with the queue.

``````#include <stdio.h>

{
printf("I: Insert an item\n");
printf("P: Remove items from queue and process order\n");
printf("Q: Quit program\n");
}

char GetChoice(void)
{
char t;

scanf("%c",&t);
return t;
}

main()
{

float n;
int i;
int d,f;
char c;
float p;

printf("Enter the letter for your choice:\n");
c = GetChoice();

while (c != 'Q')
{
if (c == 'I')
{
printf("Enter number of items to add into queue:\n");
scanf("%d",&i);
if(i<0)
{
printf("number is not valid");
return 0;
}
for(f=0;f<i;f++)
{printf("Enter the price of the item to insert into the queue:\n)";
scanf("%f", &n);

}
}
else if (c == 'P')
{
printf("How many items do you want to process");
scanf("%d",&i);
if(i<0)
{
printf("number is not valid");
return 0;
}
for(f=0;f<i;f++)
{

printf("Item cost is %f\n", n);
p += n;
}
printf("Total cost is %f\n",p);
}

}
else if (c != 'Q')
printf("Invalid Choice. You must use I, P, or Q.\n");

printf("Enter the letter for your choice: ");
c = GetChoice();
};

return 0;
}``````

Sure, you could build a queue around an array, but it may be easier to do it around a list. Any structure that allows you to remove the first item entered first work. The problem with arrays is that they are fixed size. So in order to not overflow and crash the program you have to declare significantly more memory than you really need or you can do a lot of shifting around of the contained elements. If the array isn't that large, that's doable in a reasonable timeframe. Lists are inherently of variable size. It's just a matter of always adding at the end and always removing from the beginning.

Sure, you could build a queue around an array, but it may be easier to do it around a list. Any structure that allows you to remove the first item entered first work. The problem with arrays is that they are fixed size. So in order to not overflow and crash the program you have to declare significantly more memory than you really need or you can do a lot of shifting around of the contained elements. If the array isn't that large, that's doable in a reasonable timeframe. Lists are inherently of variable size. It's just a matter of always adding at the end and always removing from the beginning.

There are two parts (sorry about forgetting this), one assuming there are no more than 20 entries and one assuming there's no limit. How would I set it up without going to a list? I appreciate your help with my question.

If you're doing this in C++ why are you using C syntax?

>>derangedone
ucan use arrays with dynamic memory allocation.

``````main()
{

float n;
int i;
int d,f;
char c;
float p;
int *Queue=NULL ; //declare a pointer
printf("Enter the letter for your choice:\n");
c = GetChoice();

while (c != 'Q')
{
if (c == 'I')
{
printf("Enter number of items to add into queue:\n");
scanf("%d",&i);
if(i<0)
{
printf("number is not valid");
return 0;
}
Queue = new int[i] ; //memory allocated now use it as array
if(!Queue ) {
printf("memory allocation failed");
return 0;

}
...................................
...................................``````

after this u try writing insert and delete funtions, aad at the end and remove from front, to achive u can maintain two index(int front and int tail) storing position of first and last item of Queue.

Whether you declare the array size embedded in the queue with static or dynamic memory doesn't alter what you do with it once the memory has been declared. You can increase the size of the array as needed using dynamic memory, thus in effect emulating an STL vector, but IMO it's easier using a list.

With an array of objects when you remove an object you can either ignore it or overwrite it. If you ignore it, then you need to keep track of which objects are valid and which aren't. If you overwrite an object when you delete it, then you just have to keep track of the last valid object. Either way, you increment the index of the last valid object every time you push something on the queue. If you ignore objects when poppint, you increment the first valid object index and keep the same last valid object index the same. If you overwrite when popping you always pop the object at index zero and then shift all remaining objects one index to the left thus overwriting all objects (but the object at the last valid index before the pop) and then after all the shifting/overwriting you decrease the last valid index, thus ignoring everything beyond the last valid index.

If you have an unchangeble array size and you don't overwrite, then the queue will be good for only the first 20 (or whatever) objects. If you overwrite, then the queue will be good as long as there are 20 (or whatever) or less objects at any time, but you could add and remove literally thousands of objects over the life of the queue.

With dynamic memory you can extend the size of the array in the queue, but it's a bit of a hassle. You need to copy the current contents of the current queue to a temporary holding queue, free the memory in current queue, redeclare more memory for the current queue, then write contents of the temporary queue back to the current queue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.