Created
January 24, 2020 17:41
-
-
Save susanhsrestha/740db53a54b8033a62e2aeb421d479bc to your computer and use it in GitHub Desktop.
This is an implementation of dynamic queue using linked list
This file contains 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<conio.h> | |
using namespace std; | |
struct Node{ | |
int data; | |
Node* next; | |
}; | |
Node* front = NULL; | |
Node* rear = NULL; | |
void display(){ | |
if( rear == NULL && front == NULL ){ | |
cout<<"No element in the queue display"<<endl; | |
return; | |
} | |
cout<<"Queue is: "; | |
Node *temp = front; | |
while(temp != NULL){ | |
cout<<temp->data<<" "; | |
temp = temp->next; | |
} | |
cout<<endl; | |
} | |
void enqueue(int x){ | |
Node *temp = new Node; | |
temp->data = x; | |
temp->next = NULL; | |
if( front == NULL && rear == NULL ){ | |
front = rear = temp; | |
display(); | |
return; | |
} | |
rear->next = temp; | |
rear = temp; | |
display(); | |
} | |
void dequeue(){ | |
Node *head = front; | |
if(front == rear){ | |
cout<<"Deleted Element: "<<front->data<<endl;; | |
rear = front = NULL; | |
delete head; | |
return; | |
} | |
cout<<"Deleted Element: "<<front->data<<endl;; | |
front = front->next; | |
delete(head); | |
display(); | |
} | |
int main(){ | |
display(); | |
enqueue(5); | |
enqueue(6); | |
dequeue(); | |
dequeue(); | |
display(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment