What should I return when trying to dequeue an empty queue?
My textbook tells be to return a 'key' that signals an error.
I tried :
Element queueEmpty()
{
Element elem = {'!'};
printf("Queue is empty");
return elem;
}
but figured that it's not a good design since '!' is not a unique representation of an error.
(Assuming that any char data can be a valid input)
My full code for Queue ADT & dequeue function :
typedef struct
{
char data;
} Element;
typedef struct Node *nodePtr;
typedef struct Node
{
Element elem;
nodePtr next;
} Node;
nodePtr front[MAX_QUEUES];
nodePtr rear[MAX_QUEUES];
Element dequeue(int i)
{
Element elem;
nodePtr temp = front[i];
if(!front[i])
{
return queueEmpty();
}
elem = temp->elem;
front[i] = temp->next;
free(temp);
return elem;
}
FYI : My textbook is : "Fundamentals of Data Structures in C" by Horowitz, Sahni, Anderson-Freed
!was unacceptable until you said so?