0

Iam trying to build a queue data structure that contains Nodes each node have integer value and a pointer to the next node there is something wrong with he deQueue(Queue q); function , it is returning elements like a stack (LIFO) here is my code and the output

deQueue();

int deQueue(Queue* q){
     if(q->head)
    {
        int result;
        Node*temp=q->head;
        result=temp->value;
        q->head=temp->next;
        free(temp);
        if(q->head==NULL)
            q->tail=NULL;
        return result;
    }
}

enQueue():

void enQueue(Queue* q ,Node* node){

    if(isEmpty(q)){


        q->head = q->tail = node;
        q->head->next = q->tail->next = NULL;
        q->size++;
    }
    else{

        q->tail->next = node;
        q->tail = node;
        q->tail->next = NULL;
        q->size++;
    }
}

main():

int main()
{
    Queue* q = initializerQueue();
    Node* n1 = newNode(10);
    Node* n2 = newNode(20);
    Node* n3 = newNode(30);
    Node* n4 = newNode(40);

    enQueue(q , n1);
    enQueue(q , n2);
    enQueue(q , n3);
    enQueue(q , n4);

    printQueue(q);
    printf("\n%d - %d - %d - %d " , deQueue(q) , deQueue(q) , deQueue(q) , deQueue(q));
    return 0;
}

Output:

  • C or C++? Please pick one; rarely do both tags apply. – Jonathon Reinhart May 07 '21 at 02:36
  • You might be adding new element at head of the linked list, adding new element at end of linked list might help can you show us complete code. – Agent_A May 07 '21 at 02:38
  • Fyi your dequeue function invokes undefined behavior when the queue is empty. Look at the code and ask yourself, "What does this return if the queue is *empty* ? The answer is... *nothing*. As in, there is no `return `, which breaks the promise that you will return a definite `int` value. Regarding your dilema, remove function invoke eval concerns (order of invokes when used as arguments) and actually *loop* your dequeue's one at a time. that should tell you one way or another whether your order is correct (and I suspect it is). – WhozCraig May 07 '21 at 02:45
  • @WhozCraig I guess it won't solve his problem but still good corner case. if(q==NULL){return -1;} – Agent_A May 07 '21 at 02:47
  • 1
    If what I just said is confusing, consider this. Given `foo( a(), b(), c() )` , forget what you think you know and ask yourself: is `a()` called *before* `b()` or `c()` ? You sure about that? [Now read this](https://stackoverflow.com/a/367663/1322972). It makes a *huge* difference when you're expecting an order of execution, and in reality none is guaranteed. – WhozCraig May 07 '21 at 02:51
  • @WhozCraig actually i did have an empty queue condition and it didnt help – HosnyyHanyy May 07 '21 at 02:52
  • 1
    Completely unrelated, you really should decrement your queue `size` member on dequeue'ing. – WhozCraig May 07 '21 at 02:55
  • @WhozCraig thanks very much this was really the problem , man this c language is getting on my nerves :D thanks anyways – HosnyyHanyy May 07 '21 at 03:00
  • 1
    @HosnyyHanyy it's really not that complicated. In fact, it's a surprisingly simple language (few reserved words, basic constructs, etc). But is carries an incredible level *responsibility*. Second only to assembly, no language lets your shoot yourself in the foot faster, or with more ease, than C. Used properly, it's amazing (though I still prefer C++), but it is *very* easy to use *improperly* so with great power comes... yeah. Anyway, glad you understood. – WhozCraig May 07 '21 at 03:02

2 Answers2

0

You're probably inserting at the start of your list, and in your function deQueue you are getting the first element of your list causing it to behave like a stack. One way you could solve this is either adding every new element at the end of the list or deQueue getting the last element.

  • @Agent_A no im pretty sure im adding to the end of the list , actually the first set of output is a function that prints the list from head to tail , i think the list is fine – HosnyyHanyy May 07 '21 at 02:41
  • @HosnyyHanyy Share the EnQueue Function Please – Agent_A May 07 '21 at 02:45
0

I think that error comes from enqueue function not the dequeue function

the enqueue function should be

void enqueue(Queue *q, Node *node){
   if(q->head == NULL){
     q->head = q->tail = node;
     return;
   }

   q->tail->next = node;
   q->tail = node;
}