Lecture 5 Queue DSA
Lecture 5 Queue DSA
AND ALGORITHMS
QUEUE
• Its Static and Dynamic. But it cannot be both at the same time
• Via Array
• Enqueue
Means to insert a new item
• Dequeue
Means to delete an item
Queue Operations
• Key Operations:
// Dequeue elements
q.dequeue();
q.dequeue();
q.enqueue(60);
}
Queue Time Complexity via Array
• O(1) for insertion as we simply increment a pointer value
and insert value in array
• Set queue[rear] to x.
Queue Operations
Circular Array Queue Implementation
• Set x to queue[front].
• Return x.
import java.util.ArrayList;
class CircularQueue{
// Constructor
CircularQueue(int size)
{
this.size = size;
this.front = this.rear = -1;
}
//Enqueue else
public void enQueue(int data) {
{ rear = (rear + 1);
// Condition if queue is full.
// Adding a new element if
if((front == 0 && rear == size - 1) ||
if(front <= rear)
(rear == (front - 1) % (size - 1)))
{ {
System.out.print("Queue is queue.add(rear, data);
Full");
}
}
// Else updating old value
// condition for empty queue.
else if(front == -1) else
{ {
front = 0; queue.set(rear, data);
rear = 0; }
queue.add(rear, data);
}
}
}
else if(rear == size - 1 && front !=
0)
{
rear = 0;
queue.set(rear, data);
}
//Dequeue else if(front == size - 1)
public int deQueue() {
{ front = 0;
int temp;
}
temp = queue.get(front);
q.displayQueue();
q.enQueue(9);
q.enQueue(20);
int x = q.deQueue();
q.enQueue(5);
• 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
Queue Operations
Linked List Queue Implementation
• Dequeue Operation:
• Initialize QNode temp with front and set front to its next
Node(int new_data) {
this.data = new_data;
this.next = null;
}
}
// Class to implement queue operations // Function to add an element to the queue
using a linked list
void enqueue(int new_data) {
class Queue {
boolean isEmpty() {
// Add the new node at the end of the queue and
change rear
// If the front and rear are null, then the
queue is empty, otherwise it's not rear.next = new_node;
rear = new_node;
}
// Function to remove an element from the // Function to get the front element of the queue
queue
int getFront() {
void dequeue() {
// Checking if the queue is empty
if (isEmpty()) {
// If queue is empty, return
System.out.println("Queue is empty");
if (isEmpty()) { return Integer.MIN_VALUE;
System.out.println("Queue Underflow"); }
return; return front.data;
} }
// Store previous front and move front one // Function to get the rear element of the queue
node ahead
int getRear() {
Node temp = front;
front = front.next;
// Checking if the queue is empty
if (isEmpty()) {
If front becomes null, then change rear also to
null System.out.println("Queue is empty");
if (front == null) { return Integer.MIN_VALUE;
rear = null; }
} return rear.data;
} }
}
// Driver code to test the queue implementation // Dequeue an element from the queue
public class Main { q.dequeue();
public static void main(String[] args) {
Queue q = new Queue();
System.out.println("Queue Front: " +
q.getFront());
// Enqueue elements into the queue System.out.println("Queue Rear: " +
q.enqueue(10); q.getRear());
q.enqueue(20); }
}
System.out.println("Queue Front: " +
q.getFront());
System.out.println("Queue Rear: " +
q.getRear());
• Data structure
• Array
• linked list
• Queue
• Assignment 1 announcement
• CHAT-GPT
• Prep insta
THANK YOU