0% found this document useful (0 votes)
2 views2 pages

Queue Using Linked List

Uploaded by

mntsr80awdail
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Queue Using Linked List

Uploaded by

mntsr80awdail
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include<iostream>

using namespace std;

struct node{
int data;
node* next;
};
node* front=NULL;
node* rear=NULL;

void enqueue(int value)


{
node* new_node=new node;
new_node-> data=value;
new_node->next=NULL;
if(front==NULL&&rear==NULL)
{
front=rear=new_node;
}
else
{
rear->next=new_node;
rear=new_node;
}
}

int dequeue()
{
int value;
if(rear=NULL)
{
cout << "Queue us empty " ;
}
else if(front==rear)
{
node* first_node=front;
front=rear=NULL;
delete first_node;
return value;
}
else
{
node* first_node=front;
front =front->next;
value =first_node->data;
delete first_node;
return value;
}
}

void display ()
{
node* current;
if(front==NULL)
{
cout << "Queue is empty " ;
}
else
{
current =front;
while(current!=NULL)
{
cout << current->data<<"\t";
current=current->next;
}
}
}
int main()
{
enqueue(5);
enqueue(10);
enqueue(15);
enqueue(20);
enqueue(25);

display();
cout << endl;
cout <<dequeue();
cout << endl;
display();

return 0;
}

You might also like