I have a queue with items of both type int and char. When I dequeue these how can I check if the item is an int or a char? Should I dequeue them generically, using a pointer to the item and then check? How can I go about doing this?
I have a queue with items of both type int and char. When I dequeue these how can I check if the item is an int or a char? Should I dequeue them generically, using a pointer to the item and then check? How can I go about doing this?
You queue these
SayCode:struct queueElement { enum { IS_CHAR, IS_INT } type; union { char c; int i; } v; };
var.type = IS_CHAR;
var.v.c = 'a';
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
so when i originally make my queue i enqueue the items using that code? then, when dequeuing, i check if var.type = IS_CHAR ?
Yes.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
thank you!
okay i need a little bit more help here...
i don't really understand how to store the var.type as part of the queue...
how do i create the queue so that the structure is part of each element? so that when dequeuing i am able to check the var.type component?
You have
struct queueElement aQueue[10];
And say
int insertPoint = 0, removePoint = 0, numEntries = 0;
An insert function would be
aQueue[insertPoint++] = item; numEntries++;
A remove function would be
item = aQueue[removePoint++]; numEntries--;
The rest is just error checking.
If you want to use a linked list for the queue, that's your choice, but the fundamentals don't change.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Post your code, otherwise we're going to have to guess.
You'd probably have to change the type of the items stored in the queue from 'int' to 'struct queueElement'