0% found this document useful (0 votes)
63 views49 pages

Data Structures Unit 1-Linear Structures

The document discusses the queue abstract data type (ADT). It defines a queue as a first-in, first-out (FIFO) linear structure similar to a stack. The key operations of a queue include enqueue to add an item, dequeue to remove an item, peek to view the front item without removing it, and functions to check if the queue is full or empty. The document describes implementing a queue using either an array or linked list and provides pseudocode for the enqueue and dequeue operations in an array-based queue.

Uploaded by

Tom Jones
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views49 pages

Data Structures Unit 1-Linear Structures

The document discusses the queue abstract data type (ADT). It defines a queue as a first-in, first-out (FIFO) linear structure similar to a stack. The key operations of a queue include enqueue to add an item, dequeue to remove an item, peek to view the front item without removing it, and functions to check if the queue is full or empty. The document describes implementing a queue using either an array or linked list and provides pseudocode for the enqueue and dequeue operations in an array-based queue.

Uploaded by

Tom Jones
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

Department of

Computer Science and Engineering

DATA STRUCTURES
Unit 1-Linear structures
Year/Sem : II/III
Subject Code: 1151CS102
Topic : Queue ADT
Faculty Name : Mrs.S.Vijitha
Date : 17.08.2020
School of Computing
Vel Tech Rangarajan Dr. Sagunthala R&D Institute of
Science and Technology
Contents

 Queue ADT - Definition


 Queue ADT- Examples
 Queue ADT- Operations
 Queue ADT- Implementation
 Array implementation
 Linked list implementation

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Queue ADT - Definition
Definition:

 Queue is referred to be as First In First Out list.

 Queue is an abstract data structure, somewhat


similar to Stacks.

 Unlike stacks, a queue is open at both its ends.

and Project
 For example, people waiting in line for a rail ticket
Management
(SEPM)
form a queue.
07/09/2021 Department of Computer Science and Engineering
Queue - Examples
Examples of queue  in "real life":

07/09/2021
Department of Computer Science and Engineering
Queue - Operations
Operations:

Enqueue: Adds an item to the queue. If the queue is


full, then it is said to be an Overflow condition.

Dequeue: Removes an item from the queue. The items


are popped in the same order in which they are pushed.
If the queue is empty, then it is said to be an Underflow
condition.
and Project
Front: Get the front item from queue.
Management
(SEPM)

Rear: Get the last item from queue.


07/09/2021 Department of Computer Science and Engineering
Queue - Operations
Operations:

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.


and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Queue - operations

07/09/2021
Department of Computer Science and Engineering
Queue - operations

07/09/2021
Department of Computer Science and Engineering
Queue - operations
Enqueue Operation:

Queues maintain two data pointers, front and rear.

Step 1 − Check if the queue is full.

Step 2 − If the queue is full, produce overflow error.

Step 3 − If the queue is not full, increment rear pointer


to point the next empty space.

Step 4 − Add data element to the queue location,


where the rear is pointing.
07/09/2021
Department of Computer Science and Engineering
Queue - operations
Enqueue Operation:

07/09/2021
Department of Computer Science and Engineering
Queue - operations
Algorithm:
procedure enqueue(data)

if queue is full
return overflow
endif

rear ← rear + 1
queue[rear] ← data
return true

end procedure
07/09/2021
Department of Computer Science and Engineering
Queue - operations
Code:

int enqueue(int data)


if(isfull())
return 0;

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

return 1;

07/09/2021
Department of Computer Science and Engineering
Queue - operations
Dequeue Operation:

Step 1  Check if the queue is empty.

Step 2  If the queue is empty, produce underflow error


and exit.

Step 3  If the queue is not empty, access the data


where front is pointing.

Step 4  Increment front pointer to point to the next


available data element.
07/09/2021
Department of Computer Science and Engineering
Queue - operations
Dequeue Operation:

07/09/2021
Department of Computer Science and Engineering
Queue - operations
Algorithm:

procedure dequeue
if queue is empty
return underflow
end if
data = queue[front]
front ← front + 1
return true
end procedure

07/09/2021
Department of Computer Science and Engineering
Queue - operations
Code:

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

07/09/2021
Department of Computer Science and Engineering
Queue - operations

Peek():

This function helps to see the data at the front of the


queue.

Algorithm

begin procedure peek


return queue[front]
end procedure

07/09/2021
Department of Computer Science and Engineering
Queue - operations
Code:

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

07/09/2021
Department of Computer Science and Engineering
Queue - operations
Isfull():

To determine that the queue is full, the rear pointer


should reach at MAXSIZE.

Algorithm
begin procedure isfull
if rear equals to MAXSIZE
return true
else
return false
endif
end procedure
07/09/2021
Department of Computer Science and Engineering
Queue - operations

Code:

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

07/09/2021
Department of Computer Science and Engineering
Queue - operations

Isfull():

07/09/2021
Department of Computer Science and Engineering
Queue - operations
Isempty():

If the value of front is less than MIN or 0, it tells that the


queue is not yet initialized, hence empty.

Algorithm:
begin procedure isempty
if front is less than min or zero
return true
else
return false
endif
end procedure
07/09/2021
Department of Computer Science and Engineering
Queue - operations
Code:

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

07/09/2021
Department of Computer Science and Engineering
Queue - operations
Isempty():

07/09/2021
Department of Computer Science and Engineering
Queue - Implementation
Implementation:

There are two ways to implement a queue:

 Using array

 Using linked list

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Array Implementation of queue
Array implementation of queue:

 We can easily represent queue by using linear


arrays.

 There are two variables i.e. front and rear.

 Front and rear variables point to the position from


where insertions and deletions are performed in a
queue.
and Project
Management
 Initially, the value of front and queue is -1 which
(SEPM)

represents an empty queue.


07/09/2021 Department of Computer Science and Engineering
Array implementation of queue

 Array representation of a queue containing 5


elements along with the respective values of front
and rear.

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Array implementation of queue
The figure shows the queue of characters forming the
English word "HELLO".

Since, No deletion is performed in the queue till now,


therefore the value of front remains -1 .

However, the value of rear increases by one every time


an insertion is performed in the queue.

After inserting an element into the queue, the value of


and Project
rear will become Management
5 while the value of front remains
(SEPM)
same.
07/09/2021 Department of Computer Science and Engineering
Array implementation of queue

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Array implementation of queue

After deleting an element, the value of front will


increase from -1 to 0. 

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Array implementation of Queue

Insertion:

 Check if the queue is already full by comparing rear


to max - 1.

 if so, then return an overflow error.

 If the item is to be inserted as the first element in the


list, in that case set the value of front and rear to 0
and insert the element
and Project
Management
at the rear end.
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Array implementation of queue
Algorithm:

Step 1: If rear = max - 1


write overflow

Step 2: if front = -1 and rear = -1


set front = rear = 0
else
set rear = rear + 1

Step 3: set queue[rear]


and Project
Management
= num
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Array implementation of queue
Code:

void insert (int queue[], int max, int front, int rear, int item)
{
if (rear == max - 1)
{
printf("overflow");
}
else
{
if(front == -1 && rear == -1)
{ and Project

front = 0; Management
(SEPM)
rear = 0;
}
07/09/2021 Department of Computer Science and Engineering
Array implementation of queue
Code:

else
{
rear = rear + 1;
}
queue[rear]=item;
}
}

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Array implementation of queue
Deletion:

If, the value of front is -1 or value of front is greater than


rear , an underflow message will occur.

Algorithm

if front = -1 or front > rear


write underflow
else
set val = queue[front]
and Project
Management

set front = front + 1(SEPM)


07/09/2021 Department of Computer Science and Engineering
Array implementation of queue
Code:

int delete (int queue[], int max, int front, int rear)
{
int y;
if (front == -1 || front > rear)

{
printf("underflow");
} and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Array implementation of queue
Code:
else
{
y = queue[front];
if(front == rear)
{
front = rear = -1;
else
front = front + 1;
}
return y; and Project
Management

} (SEPM)

}
07/09/2021 Department of Computer Science and Engineering
Linked list implementation of queue
 In the linked queue, there are two pointers
maintained in the memory i.e. front pointer and
rear pointer.

 The front pointer contains the address of the


starting element of the queue while the rear pointer
contains the address of last element of the queue.

 Insertion and deletions are performed at rear and


front end respectively.
and Project
Management
(SEPM)
 If front and rear both are NULL, it indicates that the
queue is empty.
07/09/2021 Department of Computer Science and Engineering
Linked list implementation of queue

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Linked list implementation of queue
Operation on Linked Queue:

 There are two basic operations which can be


implemented on the linked queues.

 The operations are Insertion and Deletion.

Insert operation:

 The insert operation append the queue by adding an


and Project
element to theManagement
end of the queue.
(SEPM)
 The new element will be the last element of the
queue.
07/09/2021 Department of Computer Science and Engineering
Linked list implementation of queue
Algorithm:

Step 1: Allocate the space for the new node ptr


Step 2: Set ptr -> data = val
Step 3: if front = null
set front = rear = ptr
set front -> next = rear -> next = null
else
set rear -> next = ptr
set rear = ptr
and Project
set rear -> next =Management
null
(SEPM)
[end of if]
07/09/2021 Department of Computer Science and Engineering
Linked list implementation of queue
Code:

void insert(struct node *ptr, int item; )


{
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
and Project
else Management
(SEPM)
{
07/09/2021 Department of Computer Science and Engineering
Linked list implementation of queue
Code:

ptr -> data = item;


if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Linked list implementation of queue
Code:

else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Linked list implementation of queue
Deletion:

 Deletion operation removes the element that is first


inserted among all the queue elements.

 First, we need to check either the list is empty or


not.

 The condition front == NULL becomes true if the list


is empty, in thisandcase
Project , underflow error will occur.
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Linked list implementation of queue
Algorithm:

Step 1: if front = null


write " underflow "
go to step 5
[end of if]
Step 2: set ptr = front
Step 3: set front = front -> next
Step 4: free ptr
Step 5: end and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Linked list implementation of queue
Code:
void delete (struct node *ptr)
{
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
and Project
ptr = front; Management
(SEPM)
front = front -> next;
free(ptr);
Department of Computer Science and Engineering
}}
07/09/2021
Queue - Applications
 Queues are widely used as waiting lists for a single
shared resource like printer, disk, CPU.

 Queues are used in asynchronous transfer of data.


eg. pipes, file IO, sockets.

 Queues are used as buffers in most of the


applications like MP3 media player, CD player, etc.

 Queue are used to maintain the play list in media


players in order to add and remove the songs from
the play-list.
07/09/2021
Department of Computer Science and Engineering
Thank You

Department of Computer Science and Engineering

You might also like