-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from yuvi-mittal/master
queue
- Loading branch information
Showing
3 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// Queue class implemented using an array | ||
public class Queue { | ||
int[] queue; | ||
int front, rear, capacity, size; | ||
|
||
// Constructor to initialize a queue with a given capacity | ||
public Queue(int capacity) { | ||
this.capacity = capacity; | ||
queue = new int[capacity]; | ||
front = 0; | ||
rear = -1; | ||
size = 0; | ||
} | ||
|
||
// Enqueue: Add an element to the rear of the queue | ||
public void enqueue(int data) { | ||
if (isFull()) { | ||
System.out.println("Queue is full. Cannot enqueue " + data); | ||
return; | ||
} | ||
rear = (rear + 1) % capacity; // Circular queue behavior | ||
queue[rear] = data; | ||
size++; | ||
System.out.println("Enqueued: " + data); | ||
} | ||
|
||
// Dequeue: Remove an element from the front of the queue | ||
public int dequeue() { | ||
if (isEmpty()) { | ||
System.out.println("Queue is empty. Cannot dequeue."); | ||
return -1; // Return -1 if the queue is empty | ||
} | ||
int data = queue[front]; | ||
front = (front + 1) % capacity; // Circular queue behavior | ||
size--; | ||
System.out.println("Dequeued: " + data); | ||
return data; | ||
} | ||
|
||
// Peek: Get the front element without removing it | ||
public int peek() { | ||
if (isEmpty()) { | ||
System.out.println("Queue is empty."); | ||
return -1; | ||
} | ||
return queue[front]; | ||
} | ||
|
||
// Check if the queue is empty | ||
public boolean isEmpty() { | ||
return size == 0; | ||
} | ||
|
||
// Check if the queue is full | ||
public boolean isFull() { | ||
return size == capacity; | ||
} | ||
|
||
// Traverse the queue and print its elements | ||
public void traverse() { | ||
if (isEmpty()) { | ||
System.out.println("Queue is empty."); | ||
return; | ||
} | ||
|
||
System.out.print("Queue: "); | ||
for (int i = 0; i < size; i++) { | ||
System.out.print(queue[(front + i) % capacity] + " "); | ||
} | ||
System.out.println(); | ||
} | ||
|
||
// Driver method for testing the queue implementation | ||
public static void main(String[] args) { | ||
Queue queue = new Queue(5); // Create a queue with capacity of 5 | ||
|
||
// Enqueue elements | ||
queue.enqueue(10); | ||
queue.enqueue(20); | ||
queue.enqueue(30); | ||
queue.enqueue(40); | ||
queue.enqueue(50); | ||
|
||
// Traverse and print the queue | ||
System.out.println("Queue after enqueuing elements:"); | ||
queue.traverse(); // Expected output: 10 20 30 40 50 | ||
|
||
// Dequeue elements | ||
queue.dequeue(); // Expected output: Dequeued: 10 | ||
queue.dequeue(); // Expected output: Dequeued: 20 | ||
|
||
// Traverse the queue again | ||
System.out.println("\nQueue after dequeuing elements:"); | ||
queue.traverse(); // Expected output: 30 40 50 | ||
|
||
// Peek front element | ||
System.out.println("\nPeek: " + queue.peek()); // Output: 30 | ||
|
||
// Check if the queue is empty | ||
System.out.println("\nIs queue empty? " + queue.isEmpty()); // Output: false | ||
|
||
// Enqueue more elements | ||
queue.enqueue(60); // Circular behavior should kick in here | ||
queue.enqueue(70); | ||
|
||
// Traverse the queue again | ||
System.out.println("\nQueue after enqueuing more elements:"); | ||
queue.traverse(); // Expected output: 30 40 50 60 70 | ||
|
||
// Try to enqueue when the queue is full | ||
queue.enqueue(80); // Output: Queue is full | ||
|
||
// Dequeue all elements | ||
queue.dequeue(); | ||
queue.dequeue(); | ||
queue.dequeue(); | ||
queue.dequeue(); | ||
queue.dequeue(); // Emptying the queue | ||
|
||
// Check if the queue is empty after dequeuing all elements | ||
System.out.println("\nIs queue empty? " + queue.isEmpty()); // Output: true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle. This means that the first element added to the queue will be the first one to be removed. It's similar to a real-world queue, such as people standing in line: the person who gets in line first will be served first. | ||
|
||
Key Operations of a Queue: | ||
Enqueue: This operation adds an element to the end (rear) of the queue. | ||
Dequeue: This operation removes an element from the front of the queue. | ||
Peek: This operation allows you to look at the front element of the queue without removing it. | ||
IsEmpty: This checks if the queue is empty. | ||
IsFull: In cases where the queue has a fixed capacity, this checks if the queue is full. | ||
Traverse: This allows you to go through all the elements in the queue and display them in order. | ||
Queue Example: | ||
Imagine a line of people waiting at a coffee shop: | ||
|
||
Enqueue: When a person joins the line, they are added to the rear. | ||
Dequeue: When a person gets served, they leave from the front of the line. | ||
Peek: You can check who is at the front of the line without removing them. | ||
Types of Queues: | ||
Simple Queue (Linear Queue): | ||
|
||
The basic form of the queue where elements are added at the rear and removed from the front. | ||
Once an element is dequeued, the space it occupied cannot be reused, which may lead to inefficiency. | ||
Circular Queue: | ||
|
||
In a circular queue, the last position is connected back to the first, forming a circle. | ||
This allows for better space utilization, as the positions of dequeued elements can be reused. | ||
Priority Queue: | ||
|
||
In a priority queue, each element has a priority. Elements with higher priority are dequeued before elements with lower priority, regardless of their order of entry. | ||
Deque (Double-Ended Queue): | ||
|
||
This is a more advanced version where elements can be added or removed from both ends (front and rear). | ||
Queue Representation: | ||
Queues can be implemented using: | ||
|
||
Arrays: A fixed-size array is used to store elements. Circular queues are often implemented using arrays to reuse space efficiently. | ||
Linked Lists: In this implementation, each element (node) points to the next, and both front and rear pointers are maintained for efficient enqueue and dequeue operations. | ||
Queue Operations Complexity: | ||
Enqueue: O(1) (constant time) if there's space available. | ||
Dequeue: O(1) (constant time) since we directly remove from the front. | ||
Peek: O(1) (constant time) to look at the front element. | ||
Real-world Applications of Queues: | ||
Task Scheduling: Queues are used in operating systems to manage processes (e.g., in multi-tasking systems, the CPU schedules tasks using a queue). | ||
Print Queue: When multiple print jobs are sent to a printer, they are queued up and handled in the order they arrive. | ||
Breadth-First Search (BFS) in graph traversal: The queue is used to explore the graph level by level. | ||
Customer Service Systems: Queues are used in customer support systems where calls or requests are processed in the order they are received. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// There are n people in a line queuing to buy tickets, where the 0th person is at the front of the line and the (n - 1)th person is at the back of the line. | ||
|
||
// You are given a 0-indexed integer array tickets of length n where the number of tickets that the ith person would like to buy is tickets[i]. | ||
|
||
// Each person takes exactly 1 second to buy a ticket. A person can only buy 1 ticket at a time and has to go back to the end of the line (which happens instantaneously) in order to buy more tickets. If a person does not have any tickets left to buy, the person will leave the line. | ||
|
||
// Return the time taken for the person initially at position k (0-indexed) to finish buying tickets. | ||
|
||
public class TicketQueue { | ||
public int timeRequiredToBuy(int[] tickets, int k) { | ||
int time = 0; | ||
int n = tickets.length; | ||
|
||
for (int i = 0; i < n; i++) { | ||
if (i <= k) { | ||
time += Math.min(tickets[i], tickets[k]); | ||
} else { | ||
time += Math.min(tickets[i], tickets[k] - 1); | ||
} | ||
} | ||
|
||
return time; | ||
} | ||
|
||
public static void main(String[] args) { | ||
TicketQueue tq = new TicketQueue(); | ||
|
||
// Example usage: | ||
int[] tickets = {2, 3, 2}; | ||
int k = 2; | ||
|
||
System.out.println("Time required: " + tq.timeRequiredToBuy(tickets, k)); // Output: 6 | ||
} | ||
} |