Created
June 21, 2016 18:09
-
-
Save jayaprasad37/47006c423dd7112d209a51624c0047de to your computer and use it in GitHub Desktop.
Queue implementation using linked list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string.h> | |
#include <typeinfo> | |
using namespace std; | |
struct Node //declaring node | |
{ | |
int data; //data field in the node | |
Node* next; //pointer to next node | |
}; | |
Node* front=NULL; //initializing front and rear to NULL | |
Node* rear=NULL; | |
int isempty() //function to check if the node is empty or not | |
{ | |
if(front==NULL) | |
return 1; | |
else | |
return 0; | |
} | |
void enqueue(int x) //function to insert element into the queue | |
{ | |
Node* temp = new Node; //creating a temp node to insert data and later use | |
cout<<"Inserting "<<x<<" into the queue"<<endl; | |
temp->data = x; //putting the element into the node's data field | |
if(isempty()) | |
{ | |
temp->next = NULL; | |
front = temp; | |
rear = temp; | |
} | |
else | |
{ | |
rear->next = temp; | |
temp->next = NULL; | |
rear = temp; | |
} | |
} | |
void display() //function to display the elements of the queue | |
{ | |
if(isempty()) | |
{ | |
cout<<"The queue is empty"<<endl; | |
} | |
else | |
{ | |
cout<<"The elements in the queue are: "; | |
Node* temp = front; //creating temp node to traverse through the list | |
while(temp!=NULL) | |
{ | |
cout<<temp->data<<" "; | |
temp = temp->next; | |
} | |
cout<<endl; | |
} | |
} | |
void dequeue() //function to delete element in the queue | |
{ | |
if(isempty()) | |
{ | |
cout<<"Queue is already empty"<<endl; | |
} | |
else | |
{ | |
Node* temp = front; //temp node so that memory can be freed after traversing one node ahead | |
cout<<"Deleting "<<front->data<<" from the queue"<<endl; | |
front = front->next; | |
delete(temp); | |
} | |
} | |
void frontelement() //function to display the element in the front | |
{ | |
if(isempty()) | |
{ | |
cout<<"No elements in the queue"<<endl; | |
} | |
else | |
{ | |
cout<<"The element in the front is: "<<front->data<<endl; | |
} | |
} | |
int main() | |
{ | |
enqueue(5); | |
enqueue(10); | |
enqueue(12); | |
display(); //5 10 12 | |
enqueue(1); | |
enqueue(3); | |
display(); //5 10 12 1 3 | |
dequeue(); | |
dequeue(); | |
display(); //12 1 3 | |
enqueue(50); | |
enqueue(30); | |
display(); //12 1 3 50 30 | |
dequeue(); | |
dequeue(); | |
dequeue(); | |
dequeue(); | |
display(); //30 | |
enqueue(1445); | |
enqueue(125); | |
frontelement(); //30 | |
display(); //30 1445 125 | |
dequeue(); | |
dequeue(); | |
dequeue(); | |
dequeue(); //Queue is already empty | |
enqueue(1445); | |
enqueue(125); | |
display(); //1445 125 | |
frontelement();//1445 | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment