Skip to content

Commit 7cf39ce

Browse files
committed
Improve queue ds & algorithms
1 parent 76ee2b9 commit 7cf39ce

File tree

11 files changed

+692
-142
lines changed

11 files changed

+692
-142
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ The implementations are primarily in JavaScript, with some examples in other lan
2323

2424
### Queue
2525

26-
| No. | Name | Source | Live | Documentation | Level | Pattern |
27-
| :-: | :--------------------- | :----: | :--: | :-----------: | :---: | :-----: |
28-
| 1 | Queue using array | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/datastructures/queue/queue_array.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/datastructures/queue/queue_array.js) | - | Easy | Using Array operations |
29-
| 2 | Queue using linkedlist | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/datastructures/queue/queue_with_linkedlist.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/datastructures/queue/queue_with_linkedlist.js) | - | Easy | Using LinkedList operations |
26+
| No. | Name | Source | Live | Documentation | Level | Pattern |
27+
| :-: | :--------------------- |:----------------------------------------------------------------------------------------------------------------------------------------------------------------:|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:| :---: | :-----: |
28+
| 1 | Queue using array | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/datastructures/queue/1.queue_with_array/queue_with_array.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/datastructures/queue/1.queue_with_array/queue_with_array.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/datastructures/queue/1.queue_with_array/queue_with_array.md) | Easy | Using Array operations |
29+
| 2 | Queue using linkedlist | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/datastructures/queue/2.queue_with_linkedlist/queue_with_linkedlist.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/datastructures/queue/2.queue_with_array/queue_with_linkedlist.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/datastructures/queue/2.queue_with_linkedlist/queue_with_linkedlist.md) - | Easy | Using LinkedList operations |
3030

3131
### SinglyLinkedList
3232

src/javascript/algorithms/queue/enqueueDequeueWithStacks.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,45 @@
1-
class Stack {
1+
class MyStack {
22
constructor() {
3-
this.array = [];
3+
this.items = [];
44
}
55

6-
isEmpty() {
7-
return this.array.length === 0;
6+
// Core stack operations
7+
push(value) {
8+
this.items.push(value);
9+
}
10+
11+
pop() {
12+
if (this.isEmpty()) {
13+
throw new Error("Stack Underflow: Cannot pop from an empty stack.");
14+
}
15+
return this.items.pop();
816
}
917

1018
peek() {
1119
if (this.isEmpty()) {
12-
return null;
13-
} else {
14-
return this.array[this.array.length - 1];
20+
throw new Error("Stack Underflow: Cannot peek from an empty stack.");
1521
}
22+
return this.items[this.items.length - 1];
1623
}
1724

18-
push(value) {
19-
this.array.push(value);
25+
// Helper functions
26+
isEmpty() {
27+
return this.items.length === 0;
2028
}
2129

22-
pop() {
23-
if (this.isEmpty()) return null;
24-
return this.array.pop();
30+
size() {
31+
return this.items.length;
32+
}
33+
34+
printStack() {
35+
return this.items.join(' ');
2536
}
26-
2737
}
2838

2939
class MyQueue {
3040
constructor() {
31-
this.stack1 = new Stack();
32-
this.stack2 = new Stack();
41+
this.stack1 = new MyStack();
42+
this.stack2 = new MyStack();
3343
}
3444

3545
peek() {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class MyQueue {
2+
constructor() {
3+
this.items = []; // Array is used to implement a Queue
4+
}
5+
6+
// Core queue operations
7+
enqueue(value) {
8+
this.items.push(value); // adds an item to the queue
9+
}
10+
11+
dequeue() {
12+
if (this.isEmpty()) {
13+
throw new Error("Queue Underflow: Cannot dequeue from an empty queue.");
14+
}
15+
return this.items.shift(); // Removes an item from the front of a queue and returns the removed item
16+
}
17+
18+
peek() {
19+
if (this.isEmpty()) {
20+
throw new Error("Queue Underflow: Cannot peek from an empty queue.");
21+
}
22+
return this.items[0]; // returns the front item of the queue without removing it
23+
}
24+
25+
// Helper functions
26+
isEmpty() {
27+
return this.items.length === 0; // returns true if the queue is empty
28+
}
29+
30+
size() {
31+
return this.items.length; // returns the number of elements in the queue
32+
}
33+
34+
printQueue() {
35+
return this.items.join(" ");
36+
}
37+
}
38+
39+
// Usage example
40+
function useQueue() {
41+
const queue = new MyQueue();
42+
43+
console.log("Is empty?", queue.isEmpty()); // true
44+
45+
try {
46+
queue.dequeue(); // Should throw
47+
} catch (e) {
48+
console.error(e.message);
49+
}
50+
51+
queue.enqueue(1);
52+
queue.enqueue(2);
53+
queue.enqueue(3);
54+
55+
console.log("Queue contents:", queue.printQueue()); // 1 2 3
56+
console.log("Front element:", queue.peek()); // 1
57+
console.log("Dequeued element:", queue.dequeue()); // 1
58+
console.log("Queue after dequeue:", queue.printQueue()); // 2 3
59+
}
60+
61+
useQueue();
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Queue Implementation Using Array
2+
3+
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. A queue can be visualized as a line of people waiting for a service, where new people join at the back and are served from the front.
4+
5+
## Overview
6+
7+
This implementation uses a JavaScript array to represent a queue. While arrays in JavaScript provide methods that make queue operations straightforward, it's important to understand the underlying concepts.
8+
9+
## Operations
10+
11+
### Core Queue Operations
12+
13+
1. **Enqueue**: Adds an element to the back of the queue.
14+
- Time Complexity: O(1) amortized
15+
- Implementation: Uses the array's `push()` method
16+
17+
2. **Dequeue**: Removes and returns the element from the front of the queue.
18+
- Time Complexity: O(n) - because shifting all elements is required
19+
- Implementation: Uses the array's `shift()` method
20+
21+
3. **Peek**: Returns the element at the front of the queue without removing it.
22+
- Time Complexity: O(1)
23+
- Implementation: Accesses the first element of the array
24+
25+
### Helper Operations
26+
27+
1. **isEmpty**: Checks if the queue is empty.
28+
- Time Complexity: O(1)
29+
- Implementation: Checks if the array's length is 0
30+
31+
2. **Size**: Returns the number of elements in the queue.
32+
- Time Complexity: O(1)
33+
- Implementation: Returns the array's length
34+
35+
3. **PrintQueue**: Returns a string representation of the queue.
36+
- Time Complexity: O(n)
37+
- Implementation: Joins all elements of the array with spaces
38+
39+
## Implementation Details
40+
41+
```javascript
42+
class MyQueue {
43+
constructor() {
44+
this.items = []; // Array is used to implement a Queue
45+
}
46+
47+
// Core queue operations
48+
enqueue(value) {
49+
this.items.push(value); // adds an item to the queue
50+
}
51+
52+
dequeue() {
53+
if (this.isEmpty()) {
54+
throw new Error("Queue Underflow: Cannot dequeue from an empty queue.");
55+
}
56+
return this.items.shift(); // Removes an item from the front of a queue
57+
}
58+
59+
peek() {
60+
if (this.isEmpty()) {
61+
throw new Error("Queue Underflow: Cannot peek from an empty queue.");
62+
}
63+
return this.items[0]; // returns the front item without removing it
64+
}
65+
66+
// Helper functions
67+
isEmpty() {
68+
return this.items.length === 0;
69+
}
70+
71+
size() {
72+
return this.items.length;
73+
}
74+
75+
printQueue() {
76+
return this.items.join(" ");
77+
}
78+
}
79+
```
80+
81+
## Advantages and Disadvantages
82+
83+
### Advantages
84+
- Simple implementation
85+
- Dynamic size (no need to specify capacity)
86+
- Easy to understand and use
87+
88+
### Disadvantages
89+
- The `dequeue` operation has O(n) time complexity because it uses `shift()`, which requires moving all remaining elements
90+
- Not as efficient for large queues compared to linked list implementation
91+
92+
## Complexity Analysis
93+
94+
- **Enqueue**: O(1) amortized time complexity
95+
- **Dequeue**: O(n) time complexity
96+
- **Peek**: O(1) time complexity
97+
- **Space Complexity**: O(n) where n is the number of elements in the queue
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
class Node {
2+
constructor(value) {
3+
this.value = value;
4+
this.next = null;
5+
}
6+
}
7+
8+
class MyQueue {
9+
constructor() {
10+
this.first = null;
11+
this.last = null;
12+
this.length = 0;
13+
}
14+
15+
// Core queue operations
16+
enqueue(value) {
17+
const newNode = new Node(value);
18+
if (this.isEmpty()) {
19+
this.first = newNode;
20+
this.last = newNode;
21+
} else {
22+
this.last.next = newNode;
23+
this.last = newNode;
24+
}
25+
this.length++;
26+
}
27+
28+
dequeue() {
29+
if (this.isEmpty()) {
30+
throw new Error("Queue Underflow: Cannot dequeue from an empty queue.");
31+
}
32+
33+
const temp = this.first;
34+
if (this.length === 1) {
35+
this.first = null;
36+
this.last = null;
37+
} else {
38+
this.first = this.first.next;
39+
temp.next = null;
40+
}
41+
42+
this.length--;
43+
return temp.value;
44+
}
45+
46+
peek() {
47+
if (this.isEmpty()) {
48+
throw new Error("Queue Underflow: Cannot peek from an empty queue.");
49+
}
50+
return this.first.value;
51+
}
52+
53+
// Helper functions
54+
isEmpty() {
55+
return this.length === 0;
56+
}
57+
58+
size() {
59+
return this.length;
60+
}
61+
62+
printQueue() {
63+
let current = this.first;
64+
const values = [];
65+
while (current) {
66+
values.push(current.value);
67+
current = current.next;
68+
}
69+
return values.join(' ');
70+
}
71+
}
72+
73+
// Usage example
74+
function useQueue() {
75+
const queue = new MyQueue();
76+
77+
console.log("Is empty?", queue.isEmpty()); // true
78+
79+
try {
80+
queue.dequeue(); // Should throw
81+
} catch (e) {
82+
console.error(e.message);
83+
}
84+
85+
queue.enqueue(1);
86+
queue.enqueue(2);
87+
queue.enqueue(3);
88+
89+
console.log("Queue contents:", queue.printQueue()); // 1 2 3
90+
console.log("Front element:", queue.peek()); // 1
91+
console.log("Dequeued element:", queue.dequeue()); // 1
92+
console.log("Queue after dequeue:", queue.printQueue()); // 2 3
93+
}
94+
95+
useQueue();

0 commit comments

Comments
 (0)