Data sturcture and algorithm week 5
Data sturcture and algorithm week 5
Semester – II Semester
EA2331201010152
1. Explain enqueue and dequeue operation performed in queue data
structure using array representation with algorithm.
Array Representation:
We use an array to store the elements of the queue. Two variables, front and rear, are used to track the
queue's state:
• front: Points to the index of the first element in the queue (or -1 if empty).
• rear: Points to the index of the last element in the queue (or -1 if empty).
Enqueue Operation:
Enqueue adds a new element to the back of the queue. Here's the algorithm:
1. Check for Overflow: If rear is equal to the array size minus 1, the queue is full. We can't add any
more elements. (This can be handled by throwing an exception or returning an error code.)
2. Increment Rear: If there's space, increment rear by 1 to point to the next available slot in the array.
3. Insert Element: Insert the new element at the array index pointed to by rear.
Enqueue Algorithm:
Python
def enqueue(self, data):
if self.rear == len(self.arr) - 1:
print("Overflow error: Queue is full")
else:
self.rear += 1
self.arr[self.rear] = data
Dequeue Operation:
Dequeue removes the element from the front of the queue and returns it. Here's the algorithm:
1. Check for Underflow: If front is equal to -1, the queue is empty. There's nothing to remove. (This
can be handled by throwing an exception or returning an error code.)
2. Store Removed Element: Store the element at the index pointed to by front in a temporary
variable.
3. Update Front: Increment front by 1 to point to the next element in the queue.
4. Return Element: Return the stored element (the one that was removed from the front).
Dequeue Algorithm:
Python
def dequeue(self):
if self.front == -1:
print("Underflow error: Queue is empty")
return None
else:
data = self.arr[self.front]
EA2331201010152
self.front += 1
return data
Important Notes:
• In this implementation, the enqueue operation happens at the end of the array (higher index), and
dequeue happens at the beginning (lower index). This maintains the FIFO order.
• This is a simple implementation. For better space utilization, we can explore circular queues where
the rear can wrap around to the beginning of the array when it reaches the end.
By using these operations, you can efficiently manage elements in a queue data structure using an array
representation.
Ans. Queue with Linked List Representation: Enqueue and Dequeue Operations
A queue implemented using a linked list offers dynamic memory allocation, making it more flexible than an
array-based representation. Here's how enqueue and dequeue operations work in this scenario:
The queue is represented as a linked list where each node holds the data and a pointer to the next node. Two
pointers, front and rear, are used to manage the queue:
• front: Points to the first node in the queue (or None if empty).
• rear: Points to the last node in the queue (or None if empty).
Enqueue Operation:
Enqueue adds a new element to the back of the queue. Here's the algorithm:
1. Create a New Node: Create a new node with the data to be enqueued.
2. Update Rear: If the queue is empty (rear is None), set both front and rear to point to the new
node. Otherwise, update the next pointer of the current rear node to point to the new node and then
set rear to point to the new node.
Python
def enqueue(self, data):
new_node = Node(data)
if self.rear is None:
self.front = self.rear = new_node
else:
self.rear.next = new_node
self.rear = new_node
Dequeue Operation:
Dequeue removes the element from the front of the queue and returns it. Here's the algorithm:
EA2331201010152
1. Check for Underflow: If front is None, the queue is empty. There's nothing to remove (return error
or None).
2. Store Removed Data: Store the data from the node pointed to by front.
3. Update Front: If there's only one element (front and rear point to the same node), set both front
and rear to None (empty queue). Otherwise, update front to point to the next node in the list.
4. Return Element: Return the stored data (the one that was removed from the front).
Python
def dequeue(self):
if self.front is None:
print("Underflow error: Queue is empty")
return None
else:
data = self.front.data
if self.front == self.rear: # Only one element
self.front = self.rear = None
else:
self.front = self.front.next
return data
• Dynamic Memory Allocation: No need to predefine the queue size. Memory is allocated as needed.
• Efficient Insertion and Deletion: Insertion and deletion at the beginning or end of a linked list take
constant time (O(1)).
Important Notes:
• This is a simple implementation. Variations like circular linked lists can be used for better memory
management.
• The Node class is assumed to have attributes like data and next to store the data and reference the
next node in the list.
EA2331201010152