0% found this document useful (0 votes)
11 views

Lab7 04052021 012842pm

solved lab journal of data structures and algorithm

Uploaded by

ikhalil2718
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lab7 04052021 012842pm

solved lab journal of data structures and algorithm

Uploaded by

ikhalil2718
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Lab#07

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.

1.1 Queue as an Abstract Data Type


The definition of an abstract data type clearly states that for a data structure to be abstract, it
should have the following two characteristics – Firstly, there should be particular way in which
components are related to each other and secondly, a statement of the operations that can be
performed on an element of the abstract data type should be specified.

Thus, a queue, as an abstract data type, can be defined as follows

A queue of elements of type X is a finite sequence of elements of X together with the following
operation

 Initialize a queue to be empty


 Determine if a queue is empty or not
 Determine if a queue is full or not
 Insert a new element after the last element in the queue, if it is not full
 Retrieve the first element of a queue, if it is not empty
 Delete the first element of the queue, if it is not empty

1.2 Representation of Queues:


Queues, being a linear data structure, can be represented by using both arrays and linked
list (we will do Linked list in the later stages).

1.2.1 Representation of a Queue as an Array:


Array is a data structure that stores a fixed number of elements. One of the major
limitations of an array is that its size should be fixed prior to using it. But the size of the
queue keeps on changing as the elements are either removed from the front end or added
at the rear end. One of the solutions to this problem would be to declare an array with a
maximum size. The followings figure shows the representation of the queue as an array.
Front=-1: Rear=7

48 95 62 -8 1 2 4 6

X[0] X[1] X[2] X[3] X[4] X[5] X[6] X[7]

Let us now see a program that implements a queue as an array

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

Before Insertion After Insertion

Let us now see how the del_q () (i.e. Dequeue the queue) function works.

//remove an elements from the queue


Char queue :: del_q()
{
char data;
if(front==-1)
{
cout<<"\n Queue is empty";
return NULL;
}
data = arr[front];
arr[front]=0;
if(front==rear)
front= rear-1;
else
front++;
return data;
}

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.

Here is the main Function of the program.

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).

Figure 1: Circular Queue

Suppose an array x of n elements is used to implement a circular queue. If we go on adding


element to the queue we may reach x[n-1]. We cannot add any more elements to the queue
since the end of the array has been reached. Instead of reporting the queue is full, if some
elements in the queue have been deleted then there might be empty slots at the beginning of
the queue. In such cases these slots would be filled by new elements added to the queue. In
short, just because we have reached the end of the array, the queue would not be reported as
full. The queue would be reported full only when all the slots in the array are occupied.

Linear Vs Circular Queue

Figure 2: Linear Queue


Figure 3: Circular Queue

Program that Implements Circular Queue as an Array

const int QUEUE_CAPACITY = 128;


class Queue
{
private:

int myArray[QUEUE_CAPACITY];

int front, rear, count=0;

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.

void Queue::enqueue(int value)


{
if (! isFull())
{
myArray[rear] = value;
rear = (rear + 1) % QUEUE_CAPACITY;
count++;
}
}

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.

++++++++++++++++++++++++++++++++++++++++++++++++++++

You might also like