Skip to content

Instantly share code, notes, and snippets.

@miladabc
Created July 7, 2018 16:34
Show Gist options
  • Save miladabc/1d7d11760bc5f760b2142c82599dd498 to your computer and use it in GitHub Desktop.
Save miladabc/1d7d11760bc5f760b2142c82599dd498 to your computer and use it in GitHub Desktop.
Queue implementation with Array
//Milad Abbasi
//06-07-2018
//Queue implementation with Array
#include<iostream>
using namespace std;
#define MAX_SIZE 5
class Queue
{
int A[MAX_SIZE];
int front, rear;
public:
Queue()
{
front = -1;
rear = -1;
}
bool isEmpty()
{
return (front == -1 && rear == -1);
}
bool isFull()
{
return (rear+1) % MAX_SIZE == front ? true : false;
}
void enqueue(int x)
{
if(isFull())
{
cout<<"Error: Queue is Full\n";
return;
}
if (isEmpty())
front = rear = 0;
else
rear = (rear+1) % MAX_SIZE;
A[rear] = x;
}
void dequeue()
{
if(isEmpty())
{
cout<<"Error: Queue is Empty\n";
return;
}
else if(front == rear )
rear = front = -1;
else
front = (front+1) % MAX_SIZE;
}
int peek()
{
if(front == -1)
{
cout<<"Error: cannot return front from empty queue\n";
return -1;
}
return A[front];
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment