1

I'm new to C and I'm trying to code a queue problem. At the moment, I'm coding something that will check if the queue is empty. This is what I have so far:

For Queue.h (this was provided by our instructor)

#include <stdio.h>
#include <stdlib.h>

struct queueNode {
    char data;
    struct queueNode *nextPtr;
};

typedef struct queueNode QueueNode;
typedef QueueNode* QueueNodePtr;

typedef struct Queue {
    QueueNodePtr head;
    QueueNodePtr tail;
} Queue;

void instructions();
int isEmpty(Queue);
void enqueue(Queue*, char);
char dequeue(Queue*);
void printQueue(Queue);
void freeQueue(Queue*);

For Queue.c

#include <stdio.h>
#include <stdlib.h>

#include "Queue.h"

int isEmpty(struct Queue queue)
 {
     if (Queue == NULL)
     {
         return 1;
     }
 }

The problem is in line 8 of Queue.c, the compiler says "error: expected expression before 'Queue'" How will I resolve this?

Edit: I tried to use queue == NULL instead of Queue == NULL and the compiler said: error: invalid operands to binary == (have 'struct Queue' and 'void*').

Thank you very much!

3
  • 1
    Queue is the name of a struct. The expression Queue == NULL is trying to compare a type name to a value. The parameter struct Queue queue passes your queue by value, which might not be what you want. All things considered, it's absolutely unclear how to answer this, as it probably depends on the semantics of your structure itself. I suggest you edit your question to show the contents of Queue.h. Commented Nov 13, 2020 at 2:32
  • Read Modern C, see this C reference website, read the documentation of your compiler (e.g. GCC...) and debugger (e.g. GDB). With GCC compile as gcc -Wall -Wextra -g (all warnings and debug info). Take inspiration from existing programs coded in C on github Commented Nov 13, 2020 at 3:14
  • Thank you for references! They are indeed useful! Commented Nov 13, 2020 at 3:30

1 Answer 1

1

To begin with, use a consistent way of passing your queue around. Note the differences in the interfaces:

int isEmpty(Queue);             //<-- by value
void enqueue(Queue*, char);     //<-- by reference
char dequeue(Queue*);           //<-- by reference
void printQueue(Queue);         //<-- by value
void freeQueue(Queue*);         //<-- by reference

Some of these are passing a Queue structure by value, and others by reference (pointer). It's likely that you want all your functions to operate on a pointer, i.e. Queue*.

Next, you should have some kind of operation that initializes the queue. You have freeQueue already, which does the inverse. So you probably want something like initQueue:

void initQueue(Queue* q) {
    q->head = NULL;
    q->tail = NULL;
}

Now, on to the actual question... As I've already suggested, you should change isEmpty (and printQueue) to accept a pointer to the queue. And then you use whatever logic should indicate the queue is empty. Since I've asserted above that on initialization, the head pointer should probably be NULL, then that would be an appropriate "empty" test as well:

int isEmpty(Queue* q) {
    return q->head == NULL;
}

And finally, since you're likely to ask how to actually use this:

int main(void)
{
    Queue q;

    initQueue(&q);
    printf("Queue empty: %d\n", isEmpty(&q));

    enqueue(&q, 'X');
    printf("Queue empty: %d\n", isEmpty(&q));

    enqueue(&q, 'Y');
    enqueue(&q, 'Z');
    printQueue(&q);

    printf("Removed %c\n", dequeue(&q));
    printQueue(&q);

    freeQueue(&q);        
    return 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! This is very helpful!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.