Skip to content

Commit c5ae48e

Browse files
authored
Merge pull request #1283 from dhairyagothi/main
Queue document added
2 parents 9a19241 + 988e58c commit c5ae48e

File tree

4 files changed

+256
-1
lines changed

4 files changed

+256
-1
lines changed

docs/dsa/Queue/_category_.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Queue",
3+
"position": 11,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "A queue is a data structure that follows the First-In-First-Out (FIFO) principle. It is similar to a real-life queue, where the first person to join the queue is the first one to be served. In programming, a queue allows you to add elements to the end and remove elements from the front. This makes it useful for implementing algorithms that require a FIFO ordering, such as breadth-first search. Queues are commonly used in operating systems, networking, and other areas of computer science."
7+
}
8+
}

docs/dsa/Queue/image.png

27 KB
Loading

docs/dsa/Queue/queue.md

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
---
2+
id: queue-in-dsa
3+
title: Queue Data Structure
4+
sidebar_label: Queue
5+
sidebar_position: 1
6+
description: "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. Queues are used in various applications such as process scheduling, breadth-first search, and more."
7+
tags: [dsa, data-structures, Queue]
8+
---
9+
10+
### Introduction to Queue
11+
12+
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. Queues are used in various applications such as process scheduling, breadth-first search, and more.
13+
14+
![alt text](image.png)
15+
16+
### Queue Operations
17+
18+
1. **Enqueue**: Add an element to the end of the queue.
19+
2. **Dequeue**: Remove the element from the front of the queue.
20+
3. **Peek**: Retrieve the element from the front of the queue without removing it.
21+
4. **isEmpty**: Check if the queue is empty.
22+
5. **Size**: Get the number of elements in the queue.
23+
24+
### Pseudocode
25+
26+
#### Basic Operations
27+
28+
1. **Enqueue**:
29+
30+
```text
31+
function enqueue(queue, element):
32+
queue.append(element)
33+
```
34+
35+
2. **Dequeue**:
36+
37+
```text
38+
function dequeue(queue):
39+
if isEmpty(queue):
40+
return "Queue Underflow"
41+
return queue.pop(0)
42+
```
43+
44+
3. **Peek**:
45+
46+
```text
47+
function peek(queue):
48+
if isEmpty(queue):
49+
return "Queue is empty"
50+
return queue[0]
51+
```
52+
53+
4. **isEmpty**:
54+
55+
```text
56+
function isEmpty(queue):
57+
return len(queue) == 0
58+
```
59+
60+
5. **Size**:
61+
```text
62+
function size(queue):
63+
return len(queue)
64+
```
65+
66+
### Implementation in Python, C++, and Java
67+
68+
#### Python Implementation
69+
70+
```python
71+
class Queue:
72+
def __init__(self):
73+
self.elements = []
74+
75+
def enqueue(self, element):
76+
self.elements.append(element)
77+
78+
def dequeue(self):
79+
if self.is_empty():
80+
return "Queue Underflow"
81+
return self.elements.pop(0)
82+
83+
def peek(self):
84+
if self.is_empty():
85+
return "Queue is empty"
86+
return self.elements[0]
87+
88+
def is_empty(self):
89+
return len(self.elements) == 0
90+
91+
def size(self):
92+
return len(self.elements)
93+
94+
# Example usage
95+
queue = Queue()
96+
queue.enqueue(10)
97+
queue.enqueue(20)
98+
print(queue.dequeue()) # Output: 10
99+
print(queue.peek()) # Output: 20
100+
print(queue.is_empty()) # Output: False
101+
print(queue.size()) # Output: 1
102+
```
103+
104+
#### C++ Implementation
105+
106+
```cpp
107+
#include <iostream>
108+
#include <vector>
109+
110+
class Queue {
111+
private:
112+
std::vector<int> elements;
113+
114+
public:
115+
void enqueue(int element) {
116+
elements.push_back(element);
117+
}
118+
119+
int dequeue() {
120+
if (is_empty()) {
121+
std::cerr << "Queue Underflow" << std::endl;
122+
return -1;
123+
}
124+
int front = elements.front();
125+
elements.erase(elements.begin());
126+
return front;
127+
}
128+
129+
int peek() {
130+
if (is_empty()) {
131+
std::cerr << "Queue is empty" << std::endl;
132+
return -1;
133+
}
134+
return elements.front();
135+
}
136+
137+
bool is_empty() {
138+
return elements.empty();
139+
}
140+
141+
int size() {
142+
return elements.size();
143+
}
144+
};
145+
146+
// Example usage
147+
int main() {
148+
Queue queue;
149+
queue.enqueue(10);
150+
queue.enqueue(20);
151+
std::cout << queue.dequeue() << std::endl; // Output: 10
152+
std::cout << queue.peek() << std::endl; // Output: 20
153+
std::cout << std::boolalpha << queue.is_empty() << std::endl; // Output: false
154+
std::cout << queue.size() << std::endl; // Output: 1
155+
return 0;
156+
}
157+
```
158+
159+
#### Java Implementation
160+
161+
```java
162+
import java.util.ArrayList;
163+
164+
public class Queue {
165+
private ArrayList<Integer> elements;
166+
167+
public Queue() {
168+
elements = new ArrayList<>();
169+
}
170+
171+
public void enqueue(int element) {
172+
elements.add(element);
173+
}
174+
175+
public int dequeue() {
176+
if (is_empty()) {
177+
System.out.println("Queue Underflow");
178+
return -1;
179+
}
180+
return elements.remove(0);
181+
}
182+
183+
public int peek() {
184+
if (is_empty()) {
185+
System.out.println("Queue is empty");
186+
return -1;
187+
}
188+
return elements.get(0);
189+
}
190+
191+
public boolean is_empty() {
192+
return elements.isEmpty();
193+
}
194+
195+
public int size() {
196+
return elements.size();
197+
}
198+
199+
// Example usage
200+
public static void main(String[] args) {
201+
Queue queue = new Queue();
202+
queue.enqueue(10);
203+
queue.enqueue(20);
204+
System.out.println(queue.dequeue()); // Output: 10
205+
System.out.println(queue.peek()); // Output: 20
206+
System.out.println(queue.is_empty()); // Output: false
207+
System.out.println(queue.size()); // Output: 1
208+
}
209+
}
210+
```
211+
212+
### Complexity
213+
214+
- **Time Complexity**:
215+
216+
- Enqueue: $O(1)$
217+
- Dequeue: $O(1)$
218+
- Peek: $O(1)$
219+
- isEmpty: $O(1)$
220+
- Size: $O(1)$
221+
222+
- **Space Complexity**: $O(n)$, where $n$ is the number of elements in the queue.
223+
224+
### Example
225+
226+
Consider a queue with the following operations:
227+
228+
1. Enqueue 10
229+
2. Enqueue 20
230+
3. Dequeue
231+
4. Peek
232+
5. Check if empty
233+
6. Get size
234+
235+
**Operations**:
236+
237+
- Enqueue 10: Queue becomes [10]
238+
- Enqueue 20: Queue becomes [10, 20]
239+
- Dequeue: Removes 10, Queue becomes [20]
240+
- Peek: Returns 20, Queue remains [20]
241+
- isEmpty: Returns false
242+
- Size: Returns 1
243+
244+
### Conclusion
245+
246+
A queue is a fundamental data structure used in computer science for various applications where the FIFO (First In First Out) principle is required. It is simple to implement and provides efficient operations for adding and removing elements. Understanding and using queues effectively can help solve many algorithmic problems.
247+

docs/dsa/stack/_category_.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"label": "Stack",
3-
"position": 9,
3+
"position": 12,
44
"link": {
55
"type": "generated-index",
66
"description": "Stack is a linear data structure that follows LIFO principle"

0 commit comments

Comments
 (0)