Implementation of Queue using Array
_________________________Main.cpp_____________________________________
#include"OwnQueue.h"
int main()
{
std::cout << "MY.. QUEUE!\n" << endl;
OwnQueue queue;
queue.dequeue();
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(0);
queue.enqueue(40);
cout << queue.peek() << endl;
queue.enqueue(50);
queue.enqueue(70);
cout << queue.isempty() << endl;
cout << queue.isfull() << endl;
queue.dequeue();
queue.dequeue();
queue.dequeue();
queue.dequeue();
cout << queue.peek() << endl;
queue.dequeue();
queue.dequeue();
cout << queue.isempty() << endl;
cout << queue.isfull() << endl;
queue.printqueue();
return 0;
}
_____________________________Header file content____________________________________
#pragma once
#include<iostream>
using namespace std;
#define MAXSIZE 5
class OwnQueue
{
int front, rear;
int queue[MAXSIZE];
public :
OwnQueue();
void enqueue(int data);
void dequeue();
int peek();
bool isempty();
bool isfull();
void printqueue();
};
_________________________.cpp file content __________________________________
#include "OwnQueue.h"
OwnQueue::OwnQueue() :front(-1),
rear(-1)
{
}
void OwnQueue::enqueue(int data)
{
if (rear == MAXSIZE-1)
{
cout << "Queue size is full" <<endl;
}
else if ((front == -1))
{
front = rear = 0;
queue[rear] = data;
}
else
{
rear = rear+1;
queue[rear] = data;
}
}
void OwnQueue::dequeue()
{
if ((rear == -1))
{
cout << "Queue size is empty" << endl;
}
else
{
constexpr int length = sizeof(queue) / sizeof(queue[0]) - 1;
int temp[length];
int j = 0;
int deldata = queue[front];
for (int i = front+1; i<= rear; i++)
{
temp[j] = this->queue[i];
j++;
}
for (int i = 0; i <= rear; i++)
{
this->queue[i] = temp[i];
}
rear = rear - 1;
cout << "deque data " << deldata << endl;
}
}
int OwnQueue::peek()
{
return queue[front];
}
bool OwnQueue::isempty()
{
if (rear == -1)
{
return true;
}
return false;
}
bool OwnQueue::isfull()
{
if (rear == MAXSIZE-1)
{
return true;
}
return false;
}
void OwnQueue::printqueue()
{
for (int i =0 ; i <= rear; i++)
{
cout << "data in queue " << queue[i] << endl;
}
}
Comments
Post a Comment