Skip to main content
Engineering LibreTexts

10.5: Look at the code (continued)

  • Page ID
    34691
  • The other functionality necessary is to remove an item from the front of the queue.

    void qRemove() 
    {

    // If queue is empty, return NULL.
    if (front == NULL)
    return;

    // Store previous front and
    // move front one node ahead
    QNode* temp = front;
    front = front->next;

    // If front becomes NULL, then
    // change rear also as NULL
    if (front == NULL)
    rear = NULL;

    delete (temp);
    }
    }; //this is the end of the class Queue
    If front is NULL - then we know that the queue is empty and we simply return to main(). 
    OTHERWISE
    Create a temp item, set it equal to the front item, and then reset front to the current front->next. After this we again check front to see if it is NULL, if so then we set rear to NULL as well. It is required to delete the temp space we allocated earlier.
    Adapted from: "Queue – Linked List Implementation" by Gaurav Kumar is licensed under CC BY-SA 4.0
    • Was this article helpful?