Lab7 04052021 012842pm
Lab7 04052021 012842pm
Objectives:
This lab is intended to introduce students to queues and their applications.
The students will implement the queues and employ it in solving the given problems.
1 Queue
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 only at the other end, called the rear. The
term ‘front’ and ‘rear’ are used while describing queues in a linked list.
Front= Rear = -1
Initially both iterator front and rear contains the value -1 which means queue is empty. Now if
we insert value ‘5’ in the queue then the new illustration will be
Front=-1 Rear=0
The first element in a queue will be the first one to be removed from the list. Therefore, queues
are also called FIFO (First In First out) lists. The common example for Queues can be a queue of
people waiting to purchase tickets, where the first person in a queue is the first one to be
saved.
Many Examples of queues can be noticed within a computer system – there may be queue of
task waiting for the line print, for access to disk storage or even, in time sharing system, for use
of the CPU. Within a single Program there may be multiple requests to be kept in a queue, or
one task may create other task which must be executed in turn by keeping them in a queue.
A queue of elements of type X is a finite sequence of elements of X together with the following
operation
48 95 62 -8 1 2 4 6
Next, we will learn how to initialize the data members of queue class.
queue :: queue()
{
front=-1;
rear=-1;
}
Here we have designed a class queue. We have also declared two data members front and rear
to monitor the two ends of the queue. The Initial values of front and rear are set to -1, which
indicate that the queue is empty. To carry out the Addition and deletion operations on the
queue is empty. To carry out the insertion and deletion operations on the queue we have
Implemented two function within the queue class, namely, ins_q () (used to enqueue
elements) and del_q () (used to dequeue elements).
Class queue
{
private :
char arr[MAX];
int front,rear;
public:
queue();
void ins_q (char item);
char del_q();};
Void queue :: ins_q (char item)
{
if(rear==(MAX-1))
{
cout<<"\n Queue is full";
return;
}
rear++;
arr[rear]=item;
While inserting a new element to the queue, first it would be ascertained whether such an
addition is possible or not. Since the array indexing begins with 0 the maximum number of
elements that can be stored in the queue are MAX-1. If these many elements are already
present in the queue then it is reported to be full. This situation is also called “Overflow”. If the
element can be added to the queue then the value of the variable rear is incremented and the
new item is stored in the array. The insertion of an element to the queue has been illustrated in
the figure.
Front=-1 Rear=3 Front=-1
Rear=4
A B C D A B C D E
Let us now see how the del_q () (i.e. Dequeue the queue) function works.
Before deleting an element from the queue it is first ascertained whether there are any
elements available for deletion. If not, then the queue is reported as empty. Otherwise, an
element is deleted from arr [front]. The deletion of an element is illustrated in figure.
Front=-1 Rear=4 Front=0
Rear=4
A B C D E B C D E
Before Deletion After Deletion
Imagine a case where we add 5 elements to the queue. Value of the rear would now be 4,
suppose we have not deleted any elements from the queue, then at this stage the value of the
front would be -1. Now suppose we go on deleting elements from the queue. When the fifth
element is deleted the queue would fall empty. To make sure that another attempt to delete
should be met with an ‘empty queue’ message, in such a case front and rear both are reset to -
1 to indicate emptiness of the queue or underflow condition.
void main()
{
queue a;
a. Ins_q('A');
a. Ins_q('B');
a. Ins_q('C');
a. Ins_q('D');
a. Ins_q('E');
a. Ins_q('F');
a. Ins_q('G');
a. Ins_q('H');
a. Ins_q('I');
a. Ins_q('J');
a. Ins_q('K');
char i=a. del_q();
cout<<"item deleted"<<endl<<i;
i=a.del_q();
cout<<"item deleted"<<endl<<i;
i=a.del_q();
cout<<"item deleted"<<endl<<i;
getch();
};
But this program has got some limitation. Suppose we go on adding elements to the queue till
the entire array gets filled. At this stage the value of the rear would be MAX-1. Now if we delete
5 elements from the queue at the end of these deletions the value of the front would be 5. If
now we attempt to add new element to the queue then it would be reported as full even
though in reality the first five slots of the queue are empty. To overcome this situation we can
implement a queue as a circular queue, it is discussed in next topic.
1.3 Circular Queue
Circular queues are the queues implemented in circular form rather than in a straight line.
Circular queue overcome the problem of unutilized space in linear queue implemented as an
array. In the array implementation there is a possibility that the queue is reported full even
though slots of the queue are empty (since rear has reached the end of the array).
int myArray[QUEUE_CAPACITY];
public:
Queue();
bool isEmpty();
bool isFull();
void enqueue(int value);
int getFront();
void dequeue();
}
Next, we will learn how to initialize the data members of Stack class. The constructor of the
class is used to initialize the data member of the class here we set the front and rear of the
circular queue to 0.
Here the class queue contains a myArray to store elements of the circular queue. The member
function enqueue () and getFront () are used to insert and delete elements from the circular
queue. Furthermore the functions dequeue () is used to extract the index which is further used
in deletion of elements in the circular queue. The isEmpty () and isFull () functions are used to
check whether the queue is empty or full and the return type of both functions are Boolean.
bool Queue::isEmpty()
{
if (rear == front)
return true;
else
return false;
}
As we Described earlier that the isEmpty () function of the class is used to check whether the
queue is empty or not. This function returns true if the rear part of the circular queue is equal
to front part of the queue otherwise return false if the queue is not empty.
bool Queue::isFull()
{
if (count == QUEUE_CAPACITY)
return true;
else
return false;
}
As we Described earlier that the isFull () function of the class is used to check whether the
queue is Full or not. This function returns true if the counter (the purpose of counter is to
ensure how much element is inserted in the queue) of the circular queue is equal to
QUEUE_CAPACITY of the queue otherwise return false if the queue is not Full.
void Queue::dequeue()
{
front = (front + 1) % QUEUE_CAPACITY;
count--;
}
This function is used to delete element from the circular queue. Firstly, it will check whether the
queue is empty or not. If the queue is not empty then it will use the index (location)/front value
which is already returns from the function dequeue () to delete the elements from the circular
queue otherwise it will return -1 if the queue is empty. The getFront () function delete the
elements randomly based on index (location)/front value.
As we described earlier that the enqueue () function is used to add elements in the circular
queue. The elements is inserted randomly in the circular queue based on rear value which is
extracted as same as that of front value. The Counter (count++) is used to ensure that how
much elements are inserted in the queue which is further used to identify whether the circular
queue is full or not.
++++++++++++++++++++++++++++++++++++++++++++++++++++