Queue
Queue linked list implementation
Queue data structures maintain two pointers, front, and rear. The front points to the first item of a queue and the rear points to the last item.
Operations of Linked Queue
There are two basic operations that can be implemented on the linked queues.
- enQueue()
- deQueue()
Enqueue
enQueue() adds a new node after rear and moves rear to the next node
c++ code
void Enqueue(int value)
{
Node* newnode = new Node();
newnode->data=value;
newnode->next=front;
front->next=newnode;
rear=newnode;
}
Dequeue
deQueue() removes the front node and moves the front to the next node.
c++ code
void Deqeue()
{
Node* temp;
if (front == NULL)
{
cout << "\nStack Underflow" << endl;
}
else
{
temp=front;
front = front->next;
temp->next=NULL;
free(temp);
}
}
Comments
Post a Comment
feel free to contact me in case of queries