Created
September 7, 2014 08:06
-
-
Save harish-r/67de62d7b8bf868111ea to your computer and use it in GitHub Desktop.
Queue Array Implementation in C++
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 MAX 5 | |
using namespace std; | |
class Queue | |
{ | |
public: | |
int front, rear; | |
int queue_array[MAX]; | |
bool isEmpty() | |
{ | |
return front == -1; | |
} | |
bool isFull() | |
{ | |
return front == MAX-1; | |
} | |
public: | |
Queue() | |
{ | |
front = -1; | |
rear = -1; | |
} | |
void enqueue(int x) | |
{ | |
if(!isFull()) | |
{ | |
queue_array[++front] = x; | |
if(rear == -1) | |
rear = 0; | |
} | |
else | |
cout << "Queue is Full!" << endl; | |
} | |
void dequeue() | |
{ | |
if(!isEmpty()) | |
{ | |
if(rear == front) | |
rear = front = -1; | |
else | |
rear++; | |
} | |
else | |
cout << "Queue is Empty!" << endl; | |
} | |
void display() | |
{ | |
if(!isEmpty()) | |
{ | |
for(int i=rear;i<=front;i++) | |
cout << queue_array[i] << " "; | |
cout << endl; | |
} | |
else | |
cout << "Queue is Empty!" << endl; | |
} | |
}; | |
int main() | |
{ | |
Queue q; | |
q.enqueue(15); | |
q.display(); | |
q.enqueue(25); | |
q.display(); | |
q.enqueue(35); | |
q.display(); | |
q.enqueue(45); | |
q.display(); | |
q.enqueue(55); | |
q.display(); | |
q.enqueue(55); | |
q.dequeue(); | |
q.display(); | |
q.dequeue(); | |
q.display(); | |
q.dequeue(); | |
q.display(); | |
q.dequeue(); | |
q.display(); | |
q.dequeue(); | |
q.display(); | |
q.dequeue(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment