Data Structures and Algorithms-DSA_Linkedlist_class 7.pdf
1. Dr. CHETHAN CHANDRA S BASAVARADDI
B.E., B.Ed., M.Tech., Ph.D., D.Litt., KEA-KSET.,
Associate Professor,
Dept. CSE, School of CS&T,
Faculty of Engineering Technology,
G M University,
Post Box No-4, PB Road,
Davanagere-577006,
2. A Queue Data Structure is a fundamental concept in
computer science used for storing and managing
data in a specific order.
It follows the principle of "First in, First out" (FIFO),
where the first element added to the queue is the
first one to be removed.
It is used as a buffer in computer systems where we
have speed mismatch between two devices that
communicate with each other. For example, CPU
and keyboard and two devices in a network
Queue is also used in Operating System algorithms
like CPU Scheduling and Memory Management,
and many standard algorithms like Breadth First
Search of Graph, Level Order Traversal of a Tree.
4. Queue – Linked List Implementation
Linked List implementation of the queue data structure is
discussed and implemented. Print ‘-1’ if the queue is
empty.
we maintain two pointers, front and rear. The front points
to the first item of the queue and rear points to the last
item.
enQueue(): This operation adds a new node after the
rear and moves the rear to the next node.
deQueue(): This operation removes the front node and
moves the front to the next node.
6. Follow the below steps to solve the problem:
Create a class Node with data members integer data and Node*
next
A parameterized constructor that takes an integer x value as a
parameter and sets data equal to x and next as NULL
Create a class Queue with data members Node front and rear
Enqueue Operation with parameter x:
Initialize Node* temp with data = x
If the rear is set to NULL then set the front and rear to temp and
return(Base Case)
Else set rear next to temp and then move rear to temp
7. Dequeue Operation:
If the front is set to NULL return(Base Case)
Initialize Node temp with front and set front to
its next
If the front is equal to NULL then set the rear to
NULL
Delete temp from the memory
15. Time Complexity: O(1), The time complexity of
both operations enqueue() and dequeue() is
O(1) as it only changes a few pointers in both
operations
Auxiliary Space: O(1), The auxiliary Space of
both operations enqueue() and dequeue() is
O(1) as constant extra space is required