Queue using Linked List in C++
Queue: It is a linear data structure that is open at both ends and works on the principle of FIFO. Queue can handle multiple data it is fast and flexible.
FIFO means first in first out imagine it as a line for the movie the first one in the line will leave the line first.
Implementing the queue using a linked list.
#include <iostream>
using namespace std;
struct Node{
int data;
Node *next;
};
Node *front = NULL,*rear = NULL;
void enq(int val){ //function to insert element in queue
Node *new_node = new Node;
new_node -> data = val;
new_node -> next = NULL;
if(front==NULL){
front=rear=new_node;
}
else{
rear->next=new_node; // element will be inserted from the rear of side of the queue.
rear=new_node;
}
}
void deq(){
if(front==NULL){
cout<<"queue is empty"<<endl;
return;
}
else{
Node *temp = front;
front = front->next; // elements will be deleted from the front side of the queue
free(temp);
}
}
void display(){
if(front==NULL){
cout<<"Empty";
}
else{
Node *temp = front;
while(temp != NULL){
cout<<temp->data<<"->";
temp=temp->next;
}
}
}
void qeue_size(){
int count=0;
Node*temp=front;
while(temp->next!=NULL){
count++;
temp=temp->next;
}
cout<<"count is: "<<count+1;
}
int main()
{
enq(90);
enq(91);
enq(92);
display();
// deq();
//display();
qeue_size();
return 0;
}