Unit-5 OOP - DS
Unit-5 OOP - DS
A queue is a linear list of elements in which deletion of an element can take place only
at one end called the front and insertion can take place on the other end which is
termed as the rear. The term front and rear are frequently used while describing
queues in a linked list. In this chapter, you will deal with the queue as arrays.
In the concept of a queue, the first element to be inserted in the queue will be the first
element to be deleted or removed from the list. So Queue is said to follow the FIFO
(First In First Out) structure. A real-life scenario in the form of example for queue will
be the queue of people waiting to accomplish a particular task where the first person
in the queue is the first person to be served first.
Other examples can also be noted within a computer system where the queue of tasks
arranged in the list to perform for the line printer, for accessing the disk storage, or
even in the time-sharing system for the use of CPU. So basically queue is used within
a single program where there are multiple programs kept in the queue or one task may
create other tasks which must have to be executed in turn by keeping them in the
queue.
The meaning of an abstract data type clearly says that for a data structure to be
abstract, it should have the below-mentioned characteristics:
First, there should be a particular way in which components are related to each
other
Second, a statement for the operation that can be performed on elements of
abstract data type must have to be specified
Thus for defining a Queue as an abstract data type, these are the following criteria:
Queue is a sub class of linear data structure in which elements can be inserted from
one end called rear and deleted from the other end called front. Since insertion and
deletion in Queue is performed from two different ends, its elements are removed
from queue in same order in which they were inserted. This means first item added in
the list is the first item to be removed. Because of this characteristic queue is called
FIFO (First In First Out) list.
Real life example of queue is people waiting in a line to purchase a movie ticket. The
first person in line will get ticket first. Another example is payment counter in
shopping mall. Time sharing system is also an example of queue.
According to data structure used for implementation queues can be static queue or
dynamic queue. Queues implemented using fixed length array is called static queue
and queues implemented using linked list is called dynamic queue. In this chapter only
static queue is discussed. Dynamic queue is discussed with linked list.
According to the way the queues are implemented they can be separated into simple
queue, circular queue and dequeue.
class queue
{
int q1[5];
int rear, front;
public:
queue()
{
rear = -1;
front = -1;
}
void insert(int x)
{
if (rear == 5) {
cout << "queue over flow";
rear=5;
return;
}
if(front==-1)
{
front=0;
rear=0;
}
q1[rear] = x;
rear++;
}
void delet()
{ cout<<"\n value of front is \n"<<front<<":"<<rear;
if (front == -1) {
cout << "queue under flow";
return;
}
else
{
cout << "deleted " << q1[front];
}
if(front==(rear-1))
{
front=-1;
rear=-1;
}
else
{
front++;
}
void display();
};
void queue::display()
{ int i;
if (front==-1)
{
cout << " queue empty";
return;
}
for (i = front ; i < rear; i++)
cout << q1[i] << " ";
}
int main()
{ clrscr();
int ch;
queue qu;
while (1)
{
cout << "\n1.insert 2.delet 3.display 4.exit\nenter ur choice: "; cin >> ch;
switch (ch)
{
case 1:
cout << "enter the element: "; cin >> ch;
qu.insert(ch);
break;
case 2:
qu.delet();
break;
case 3:
qu.display();
break;
case 4:
exit(0);
}
}
}
4.3 Circular Queue
In simple queue we can not utilize memory properly in some cases like if we have
inserted 5 elements in queue with capacity of 5, then the queue is full. Now, we have
deleted 2 elements from that queue then also we can not insert new element until
whole queue gets empty, though it has 2 free elements. But in the case of circular
queue the space of deleted elements can be used to insert new elements.
Fig. 4.9 Simple Queue With Overflow On Insert Though Having Space For 2
Elements
Fig. 4.11 1st sub part of queue overflow condition (F=1 and R=N)
Fig. 4.12 Rear At the End
public:
cir()
{
rear = -1;
front = -1;
}
void insert()
{ int no;
if((front==0 && rear==size-1)||(front==rear+1))
{
cout<<"\n overflow";
return;
}
else if(front==-1)
{
front=0;
rear=0;
}
else if(rear==size-1)
{
rear=0;
}
else
rear++;
cout<<"\n Enter element :";
cin>>no;
Q[rear]=no;
}
void del()
{
if(front==-1)
{
cout<<"\n underflow ";
return;
}
cout<<"\n Element deleted:"<<Q[front];
if(front==rear)
{
front=-1;
rear=-1;
}
else
{
if(front==size-1)
front=0;
else
front++;
}
}
void print()
{
int i;
if(front==-1)
{
cout<<"\n queue is empty ";
return;
}
if(front<=rear)
{
for(i=front;i<=rear;i++)
cout<<"\n"<<Q[i];
}
else
{
for(i=front;i<size;i++)
cout<<"\n"<<Q[i];
for(i=0;i<=rear;i++)
cout<<"\n"<<Q[i];
}
};
int main()
{
int ch;
cir qu;
while (1) {
cout << "\n1.insert 2.delet 3.display 4.exit\nenter ur choice: "; cin >> ch;
switch (ch) {
case 1:
qu.insert();
break;
case 2:
qu.del();
break;
case 3:
qu.print();
break;
case 4:
exit(0);
}
}
}
Double ended queue is a queue in which insertions and deletions are made to or from
either or both ends of the queue. There are two variations of double ended queue,
input restricted deque and output restricted deque. The restricted operation can only
be performed from one end. In input restricted deque insertion can be performed from
only one end. In output restricted deque delete operation can be performed from only
one end. In algorithms deque using circular queue is considered.
int deq::delete_frontend()
{
int item;
if( front == -1)
{
cout<<"\nqueue underflow\n";
exit(1);
}
item=deque_arr[front];
if(front==rear) /*queue has only one element */
{
front=-1;
rear=-1;
}
else
if(front==max-1)
front=0;
else
front=front+1;
return item;
}/*end of delete_frontend()*/
int deq::delete_rearend()
{
int item;
if( front == -1)
{
cout<<"\nqueue underflow\n";
exit(1);
}
item=deque_arr[rear];
void deq::display()
{
int i;
switch(choice)
{
case 1:
cout<<"\ninput the element for adding in queue : ";
cin>>item;
q1.insert_frontend(item);
break;
case 2:
cout<<"\ninput the element for adding in queue : ";
cin>>item;
q1.insert_rearend(item);
break;
case 3:
cout<<"\nelement deleted from front end is
:\n"<<q1.delete_frontend();
break;
case 4:
cout<<"\nelement deleted from rear end is :
\n"<<q1.delete_rearend();
break;
case 5:
q1.display();
break;
case 6:
exit(1);
default:
cout<<"\nwrong choice\n";
}/*end of switch*/
q1.display();
}/*end of while*/
}/*end of main()*/
CPU scheduling
Memory management
Traffic Management