For a linked queue without head node,what should we do in dequeue operation.
时间: 2024-03-14 15:47:33 浏览: 36
In a linked queue without head node, when performing dequeue operation, we need to consider two cases:
1. If the queue is empty, we cannot perform dequeue operation. So we need to check if the queue is empty before dequeueing.
2. If the queue is not empty, we can remove the first element from the queue. We need to update the front pointer of the queue to point to the next element in the queue. We also need to free the memory occupied by the node that was removed from the queue.
Here is the sample code for dequeuing an element from a linked queue without head node in C++:
```
int dequeue(Node* &front, Node* &rear) {
if (front == NULL) {
cout << "Queue is empty";
return -1;
}
int data = front->data;
Node* temp = front;
front = front->next;
if (front == NULL) {
rear = NULL;
}
delete temp;
return data;
}
```
In this code, `front` and `rear` are pointers to the first and last nodes of the queue respectively. The `dequeue()` function removes the first node from the front of the queue and returns its data value. If the queue is empty, the function displays a message and returns -1.