Sunday, 30 November 2014

Circular Queue of Integers implemented in C++ [Turbo C++ version.]


Turbo C++

Circular Queue


In computer science, a queue (/ˈkjuː/ kew) is a particular kind of abstract data type or collection in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position, known as enqueue, and removal of entities from the front terminal position, known as dequeue. This makes the queue a First-In-First-Out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to the requirement that once a new element is added, all elements that were added before have to be removed before the new element can be removed. Often a peek or front operation is also entered, returning the value of the front element without dequeuing it. A queue is an example of a linear data structure, or more abstractly a sequential collection. Queues provide services in computer science, transport, and operations research where various entities such as data, objects, persons, or events are stored and held to be processed later. In these contexts, the queue performs the function of a buffer. Queues are common in computer programs, where they are implemented as data structures coupled with access routines, as an abstract data structure or in object-oriented languages as classes. Common implementations are circular buffers and linked lists. More information can be achieved by clicking this link.
Code implementation.

#include "iostream.h"
#include "conio.h"
const int size = 5;
class Queue
{
int Q[size];
int front;
int rear;
int values;
public:
Queue()
{
front = rear = values = 0;
}
void Enqueue(int n)
{
if (!isFull())
{
Q[rear] = n;
rear = (rear + 1) % size;
values++;
}
}
int Dequeue()
{
if (!isEmpty())
{
front=(front+1)%size;
values--;
return Q[front-1];
}
}
int isFull()
{
if (values == size)
return 1;
return 0;
}
int isEmpty()
{
if (values == 0)
return 1;
return 0;
}
int peek()
{
if (!isEmpty())
return Q[front];
}
void display()
{
cout << "Total values inside the Queue:\n";
int i = values;
int j = front;
while (i)
{
cout << Q[j] << " ";
j = (j + 1) % size;
i--;
}
}
void display(int n)
{
if (n<5 || n>-1)
cout << Q[n] << " ";
}
};

void main()
{
clrscr();
Queue q;
q.Enqueue(1);
q.Enqueue(1);
q.Enqueue(1);
q.Enqueue(1);
q.Dequeue();
q.Dequeue();
q.Dequeue();
q.Enqueue(2);
q.Enqueue(3);
q.Enqueue(4);
q.Enqueue(5);
q.display();
getch();
}
Unknown Web Developer

Morbi aliquam fringilla nisl. Pellentesque eleifend condimentum tellus, vel vulputate tortor malesuada sit amet. Aliquam vel vestibulum metus. Aenean ut mi aucto.

1 comment :