0

when my code is run, I'm getting 3 errors:

  1. "expected expression before return"
  2. "expected expression before }"
  3. "expected ',' or ';' before ')' for(Node *node=q->head)
int queue_length(queue_t* q){
    if(q==NULL) {
        return -1;
    }
    int size = 0;
    for(Node *node=q->head)
  {
        size++;
    }
    return size;
 
}

2 Answers2

0

Your for loop isn't correct. Ideally, you want something similar to the following:

for (int countingVar = 0; countingVar < 10; countingVar++) { ... }

Maybe your goal is to count the number of elements within your queue. If so, maybe you want something similar to the following and this is a stretch given that I do not know what your queue_t struct looks like:

while (NULL != q->head) { ... }

Brandon
  • 1
  • 1
0
for(Node *node=q->head)
  {
        size++;
    }

This loop is syntactically wrong. Google syntax for For loops. It should be for(initialization;condition;increment/decrement){ //body } I can see you are coming from a higher level language. C doesn't have stuff like foreach or for..in.. etc. For your case, you should be doing it in while, something like

while(q!=NULL)
{
    size++;
    q=q->next;
}