0% found this document useful (0 votes)
3 views26 pages

W9L2 Linked Queue

This document provides an overview of implementing queues using linked lists, detailing the algorithms for inserting and deleting elements. It explains the roles of FRONT and REAR pointers in managing the queue's structure and includes step-by-step instructions for both operations. The document emphasizes memory management and the conditions for handling empty queues.

Uploaded by

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

W9L2 Linked Queue

This document provides an overview of implementing queues using linked lists, detailing the algorithms for inserting and deleting elements. It explains the roles of FRONT and REAR pointers in managing the queue's structure and includes step-by-step instructions for both operations. The document emphasizes memory management and the conditions for handling empty queues.

Uploaded by

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

Queues from Linked Lists

Week 9
Lecture 3
Objectives

In this session, you will learn to:

• Linked List implementation of Queues


Implementing a Queue Using a Linked List

To keep track of the rear and front positions, you need to


declare two variables/pointers, REAR and FRONT, that will
always point to the rear and front end of the queue
respectively.
If the queue is empty, REAR and FRONT point to NULL.

FRONT REAR

310 5 7 15
Inserting an Element in a Linked Queue

Write an algorithm to implement insert operation in a linked


queue.
Inserting an Element in a Linked Queue (Contd.)

Algorithm to insert an element in a linked queue.

1. Allocate memory for the new node.

1. Assign value to the data field of the new


Suppose initially, the queue node.
is empty. 1. Make the next field of the new node point to
NULL.

1. If the queue is empty, execute the following


Request number generated 3 steps:

a. Make FRONT point to the new node


b. Make REAR point to the new node
c. Exit
REAR = NULL
1. Make the next field of REAR point to the new
FRONT = NULL node.

1. Make REAR point to the new node.


Inserting an Element in a Linked Queue (Contd.)

1. Allocate memory for the new node.


Request number generated 3 1. Assign value to the data field of the new
node.

1. Make the next field of the new node point to


NULL.
REAR = NULL
1. If the queue is empty, execute the following
FRONT = NULL
steps:

a. Make FRONT point to the new node


b. Make REAR point to the new node
c. Exit

1. Make the next field of REAR point to the new


node.

1. Make REAR point to the new node.


Inserting an Element in a Linked Queue (Contd.)

1. Allocate memory for the new node.


Request number generated 3 1. Assign value to the data field of the new
node.

1. Make the next field of the new node point to


NULL.
REAR = NULL
1. If the queue is empty, execute the following
FRONT = NULL
steps:

a. Make FRONT point to the new node


b. Make REAR point to the new node
c. Exit

1. Make the next field of REAR point to the new


310 node.

1. Make REAR point to the new node.


Inserting an Element in a Linked Queue (Contd.)

1. Allocate memory for the new node.


Request number generated 3 1. Assign value to the data field of the new
node.

1. Make the next field of the new node point to


NULL.
REAR = NULL
1. If the queue is empty, execute the following
FRONT = NULL
steps:

a. Make FRONT point to the new node


b. Make REAR point to the new node
c. Exit

1. Make the next field of REAR point to the new


310 node.

1. Make REAR point to the new node.


Inserting an Element in a Linked Queue (Contd.)

1. Allocate memory for the new node.


Request number generated 3 1. Assign value to the data field of the new
node.

1. Make the next field of the new node point to


NULL.
REAR = NULL
1. If the queue is empty, execute the following
FRONT = NULL
steps:

a. Make FRONT point to the new node


b. Make REAR point to the new node
c. Exit

1. Make the next field of REAR point to the new


310 node.

1. Make REAR point to the new node.


Inserting an Element in a Linked Queue (Contd.)

1. Allocate memory for the new node.


Request number generated 3 1. Assign value to the data field of the new
node.

1. Make the next field of the new node point to


NULL.
REAR = NULL
1. If the queue is empty, execute the following
FRONT = NULL
steps:
FRONT
a. Make FRONT point to the new node
b. Make REAR point to the new node
c. Exit

1. Make the next field of REAR point to the new


310 node.

1. Make REAR point to the new node.


Inserting an Element in a Linked Queue (Contd.)

1. Allocate memory for the new node.


Request number generated 3 1. Assign value to the data field of the new
node.

1. Make the next field of the new node point to


NULL.
REAR = NULL
1. If the queue is empty, execute the following
steps:
FRONT REAR
a. Make FRONT point to the new node
b. Make REAR point to the new node
c. Exit

1. Make the next field of REAR point to the new


310 node.

1. Make REAR point to the new node.


Inserting an Element in a Linked Queue (Contd.)

1. Allocate memory for the new node.


Request number generated 3 1. Assign value to the data field of the new
node.

1. Make the next field of the new node point to


NULL.

1. If the queue is empty, execute the following


steps:
FRONT REAR
a. Make FRONT point to the new node
b. Make REAR point to the new node
c. Exit

1. Make the next field of REAR point to the new


310 node.

1. Make REAR point to the new node.

Insert operation complete


Inserting an Element in a Linked Queue (Contd.)

1. Allocate memory for the new node.


Request number generated 5 1. Assign value to the data field of the new
node.

1. Make the next field of the new node point to


NULL.

1. If the queue is empty, execute the following


steps:
FRONT REAR
a. Make FRONT point to the new node
b. Make REAR point to the new node
c. Exit

1. Make the next field of REAR point to the new


310 node.

1. Make REAR point to the new node.


Inserting an Element in a Linked Queue (Contd.)

1. Allocate memory for the new node.


Request number generated 5 1. Assign value to the data field of the new
node.

1. Make the next field of the new node point to


NULL.

1. If the queue is empty, execute the following


steps:
FRONT REAR
a. Make FRONT point to the new node
b. Make REAR point to the new node
c. Exit

1. Make the next field of REAR point to the new


310 node.

1. Make REAR point to the new node.


Inserting an Element in a Linked Queue (Contd.)

1. Allocate memory for the new node.


Request number generated 5 1. Assign value to the data field of the new
node.

1. Make the next field of the new node point to


NULL.

1. If the queue is empty, execute the following


steps:
FRONT REAR
a. Make FRONT point to the new node
b. Make REAR point to the new node
c. Exit

1. Make the next field of REAR point to the new


310 510 node.

1. Make REAR point to the new node.


Inserting an Element in a Linked Queue (Contd.)

1. Allocate memory for the new node.


Request number generated 5 1. Assign value to the data field of the new
node.

1. Make the next field of the new node point to


NULL.

1. If the queue is empty, execute the following


steps:
FRONT REAR
a. Make FRONT point to the new node
b. Make REAR point to the new node
c. Exit

1. Make the next field of REAR point to the new


310 510 node.

1. Make REAR point to the new node.


Inserting an Element in a Linked Queue (Contd.)

1. Allocate memory for the new node.


Request number generated 5 1. Assign value to the data field of the new
node.

1. Make the next field of the new node point to


NULL.

1. If the queue is empty, execute the following


steps:
FRONT REAR
a. Make FRONT point to the new node
b. Make REAR point to the new node
c. Exit

1. Make the next field of REAR point to the new


310 510 node.

1. Make REAR point to the new node.


Inserting an Element in a Linked Queue (Contd.)

1. Allocate memory for the new node.


Request number generated 5 1. Assign value to the data field of the new
node.

1. Make the next field of the new node point to


NULL.

1. If the queue is empty, execute the following


steps:
FRONT REAR
a. Make FRONT point to the new node
b. Make REAR point to the new node
c. Exit

1. Make the next field of REAR point to the new


310 510 node.

1. Make REAR point to the new node.


Inserting an Element in a Linked Queue (Contd.)

1. Allocate memory for the new node.


Request number generated 5 1. Assign value to the data field of the new
node.

1. Make the next field of the new node point to


NULL.

1. If the queue is empty, execute the following


steps:
FRONT REAR REAR
a. Make FRONT point to the new node
b. Make REAR point to the new node
c. Exit

1. Make the next field of REAR point to the new


310 510 node.

1. Make REAR point to the new node.

Insert operation complete


ALGORITHM TO IMPLEMENT INSERT
OPERATION
FRONT= NULL
REAR = NULL
Algorithm Insert()
{
1. Create node [(new1 = (struct node*) malloc(sizeof(struct node))]
2. Enter data [new1 -> info =data]
3.if(FRONT == NULL)
3.1 FRONT = new1
3.2 REAR = new1
else
3.1 REAR -> next = new1
3.2 REAR = new1
4. REAR ->next = NULL

}
Deleting an Element from a Linked Queue

Write an algorithm to implement the delete operation on a


linked queue.
Deleting an Element from a Linked Queue (Contd.)

Algorithm to implement delete operation on a linked queue.

1. If the queue is empty: // FRONT = NULL


One request processed
a. Display “Queue empty”
b. Exit

1. Mark the node marked FRONT as current

1. Make FRONT point to the next node in its


sequence
FRONT REAR
1. Release the memory for the node marked
as current

310 5 7 10
Deleting an Element from a Linked Queue (Contd.)

One request processed 1. If the queue is empty: // FRONT = NULL

a. Display “Queue empty”


b. Exit

1. Mark the node marked FRONT as current

1. Make FRONT point to the next node in its


sequence
FRONT REAR
1. Release the memory for the node marked
as current

310 5 7 10
Deleting an Element from a Linked Queue (Contd.)

One request processed 1. If the queue is empty: // FRONT = NULL

a. Display “Queue empty”


b. Exit

1. Mark the node marked FRONT as current

1. Make FRONT point to the next node in its


sequence
FRONT REAR
1. Release the memory for the node marked
as current

310 5 7 10

current
Deleting an Element from a Linked Queue (Contd.)

One request processed 1. If the queue is empty: // FRONT = NULL

a. Display “Queue empty”


b. Exit

1. Mark the node marked FRONT as current

1. Make FRONT point to the next node in its


sequence
FRONT REAR
1. Release the memory for the node marked
as current

3 5 7 10

current
Delete operation complete

Memory released
ALGORITHM TO IMPLEMENT THE DELETE OPERATION
Algorithm Delete()
{
1. If (FRONT == 0)
1.1 Print "underflow“
else
1.1 Current = FRONT
1.2 FRONT = FRONT -> next
1.3 Current -> next = NULL
1.4 Release the memory [ free (Current) ]

You might also like