Created
January 23, 2020 13:31
-
-
Save susanhsrestha/63f456039d47e22dbfa896539e6c6e09 to your computer and use it in GitHub Desktop.
This is an implementation of static queue ADT
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> | |
#define size 2 | |
using namespace std; | |
class Queue{ | |
int a[size],front,rear; | |
public: Queue(){ | |
front = rear = -1; | |
} | |
bool isEmpty(){ | |
if(front == -1 && rear == -1) | |
return true; | |
else | |
return false; | |
} | |
bool isFull(){ | |
if((rear+1)%size == front) | |
return true; | |
else | |
return false; | |
} | |
void enqueue(int x){ | |
if(isEmpty() == true){ | |
front = rear = 0; | |
a[rear] = x; | |
display(); | |
return; | |
} | |
if(isFull() == true){ | |
cout<<"Can't add more elements"<<endl; | |
display(); | |
return; | |
} | |
rear = (rear + 1) % size; | |
a[rear] = x; | |
display(); | |
} | |
void dequeue(){ | |
if(front != -1 && front == rear){ | |
cout<<"Deleted Element is: "<<a[front]<<endl; | |
front = rear = -1; | |
display(); | |
return; | |
} | |
if(isEmpty() == true){ | |
cout<<"No elements to delete"<<endl; | |
display(); | |
return; | |
} | |
cout<<"Deleted element is: "<<a[front]<<endl;; | |
front = (front + 1) % size; | |
display(); | |
} | |
void display(){ | |
int l = (rear+size-front)%size + 1; | |
cout<<"Queue is: "; | |
if(isEmpty() == true){ | |
cout<<"Empty"<<endl; | |
return; | |
} | |
for(int i=0; i<l; i++){ | |
cout<<a[(front+i)%size]<<" "; | |
} | |
cout<<endl; | |
} | |
}; | |
int main(){ | |
Queue q; | |
q.dequeue(); | |
q.enqueue(3); | |
q.enqueue(5); | |
q.dequeue(); | |
q.dequeue(); | |
q.dequeue(); | |
q.enqueue(5); | |
q.enqueue(4); | |
q.enqueue(6); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment