Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Assignment No.1: Zeeshan Safdar

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Assignment no.

Name:

Zeeshan Safdar

Roll number:

S1F18BSCS0035

Subject:

Introduction to Data Structure And Algorithm

Submitted To:

Mam Attia
Queue:

Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is
open at both its ends. One end is always used to insert data (enqueue) and the other is used to
remove data (dequeue). Queue follows First-In-First-Out methodology, i.e., the data item
stored first will be accessed first.

A real-world example of queue can be a single-lane one-way road, where the vehicle enters
first, exits first. More real-world examples can be seen as queues at the ticket windows and
bus-stops.

Queue Representation:

As we now understand that in queue, we access both ends for different reasons. The
following diagram given below tries to explain queue representation as data structure −

As in stacks, a queue can also be implemented using Arrays, Linked-lists, Pointers and
Structures. For the sake of simplicity, we shall implement queues using one-dimensional
array.

Applications of Queue:

Queue, as the name suggests is used whenever we need to manage any group of objects in an
order in which the first one coming in, also gets out first while the others wait for their turn,
like in the following scenarios:
1. Serving requests on a single shared resource, like a printer, CPU task scheduling etc.
2. In real life scenario, Call Center phone systems uses Queues to hold people calling
them in an order, until a service representative is free.
3. Handling of interrupts in real-time systems. The interrupts are handled in the same
order as they arrive i.e First come first served.

Basic Operations:

Queue operations may involve initializing or defining the queue, utilizing it, and then
completely erasing it from the memory. Here we shall try to understand the basic operations
associated with queues −

 enqueue() − add (store) an item to the queue.

 dequeue() − remove (access) an item from the queue.

Few more functions are required to make the above-mentioned queue operation efficient.
These are −

 peek() − Gets the element at the front of the queue without removing it.

 isfull() − Checks if the queue is full.

 isempty() − Checks if the queue is empty.

In queue, we always dequeue (or access) data, pointed by front pointer and while enqueing
(or storing) data in the queue we take help of rear pointer.

Let's first learn about supportive functions of a queue −

Peek ():

This function helps to see the data at the front of the queue. Implementation of peek()
function in C programming language 

Example:

int peek() {
return queue[front];
}
Isfull ():

As we are using single dimension array to implement queue, we just check for the rear
pointer to reach at MAXSIZE to determine that the queue is full. 

Implementation of isfull() function in C programming language −

Example:

bool isfull() {
if(rear == MAXSIZE - 1)
return true;
else
return false;
}

Isempty ():

If the value of front is less than MIN or 0, it tells that the queue is not yet initialized, hence
empty.

Here's the C programming code −

Example:

bool isempty() {
if(front < 0 || front > rear)
return true;
else
return false;
}

Enqueue Operation:

Queues maintain two data pointers, front and rear. Therefore, its operations are


comparatively difficult to implement than that of stacks.

The following steps should be taken to enqueue (insert) data into a queue −

 Check if the queue is full.


 If the queue is full, produce overflow error and exit.

 If the queue is not full, increment rear pointer to point the next empty space.

 Add data element to the queue location, where the rear is pointing.

 return success.

Sometimes, we also check to see if a queue is initialized or not, to handle any unforeseen
situations. Implementation of enqueue() in C programming language −

Example:

int enqueue(int data)


if(isfull())
return 0;

rear = rear + 1;
queue[rear] = data;

return 1;
end procedure
Dequeue Operation:

Accessing data from the queue is a process of two tasks − access the data where front is
pointing and remove the data after access. The following steps are taken to
perform dequeue operation −

 Check if the queue is empty.

 If the queue is empty, produce underflow error and exit.

 If the queue is not empty, access the data where front is pointing.

 Increment front pointer to point to the next available data element.

 Return success.

Implementation of dequeue() in C programming language −

Example:

int dequeue() {
if(isempty())
return 0;
int data = queue[front];
front = front + 1;

return data;
}

You might also like