Last active
February 25, 2021 13:28
-
-
Save suvasish114/1062a17561f195be618973e6c863604f to your computer and use it in GitHub Desktop.
queue operation using array
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> | |
#define _size 5 | |
using namespace std; | |
class _queue{ | |
int front = -1; | |
int rear = -1; | |
int A[_size]; | |
public: | |
void enqueue(int); | |
int dequeue(); | |
void display(); | |
}; | |
void _queue :: enqueue(int item){ | |
if(rear == _size) | |
cout<<"Overflow"<<endl; | |
else{ | |
if(rear == -1 && front == -1) | |
front = 0; | |
rear += 1; | |
A[rear] = item; | |
} | |
} | |
int _queue :: dequeue(){ | |
int item; | |
if(front == -1){ | |
cout<<"Empty"<<endl; | |
exit(0); | |
} | |
else{ | |
item = A[front]; | |
if(front == rear){ | |
front = -1; | |
rear = -1; | |
} | |
else | |
front += 1; | |
} | |
return item; | |
} | |
void _queue :: display(){ | |
if(front == -1) | |
cout<<"Empty"<<endl; | |
else{ | |
cout<<"Items are -"<<endl; | |
for(int i=front ; i<=rear ; i++){ | |
cout<<A[i]<<" "; | |
} | |
cout<<endl; | |
} | |
} | |
int main(){ | |
_queue a; | |
a.display(); | |
a.enqueue(10); | |
a.enqueue(20); | |
a.enqueue(30); | |
a.display(); | |
cout<<"deleted item is: "<< a.dequeue()<<endl; | |
cout<<"deleted item is: "<< a.dequeue()<<endl; | |
cout<<"deleted item is: "<< a.dequeue()<<endl; | |
cout<<"deleted item is: "<< a.dequeue()<<endl; | |
a.display(); | |
return 0; | |
} | |
// this code is contribute by suvasish das. | |
// https://suvasish114.github.io/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment