Skip to content

Commit bbc0403

Browse files
Merge pull request #77 from madhusudhan1234/notes
Notes
2 parents 2dd5bc1 + 9812e5e commit bbc0403

File tree

4 files changed

+377
-0
lines changed

4 files changed

+377
-0
lines changed
7.07 MB
Binary file not shown.
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
---
2+
title: 'Data Structure: Circular Queue in JavaScript'
3+
description: 'A queue data structure that follows the FIFO (First-In-First-Out) principle. It is similar to a regular queue, but the last element is connected to the first element to form a circle.'
4+
pubDate: 'Jan 3 2026'
5+
tags: 'js-data-structure'
6+
---
7+
8+
## Overview
9+
10+
The size of the queue is fixed and a single block of memory is used as if the first element is connected to the last element to form a circle.
11+
12+
Also referes to the circular buffer or ring buffer and follows the FIFO (First-In-First-Out) principle.
13+
14+
A circular queue reuses the empty block created during the dequeue implementation.
15+
16+
When working with queue of fixed maximum size, a circular queue is a great implementation choice.
17+
18+
The circular Queue data structure supports two main operations.
19+
20+
- Enqueue: Adds an element to the end of the queue.
21+
- Dequeue: Removes the element from the front of the queue.
22+
23+
Circular Queue usage:
24+
- Clock
25+
- Streaming data
26+
- Traffic Lights
27+
28+
29+
### Circular Queue Implementaiton
30+
- enqueue(element) - add an element to the queue
31+
- dequeue() - remove an element from the queue
32+
- isFull() - check if the queue is full
33+
- isEmpty() - check if the queue is empty
34+
- peek() - get the value of the element at the front of the queue without removing it
35+
- size() - get the number of elements in the queue
36+
- print() - print the elements in the queue
37+
38+
39+
```
40+
class CircularQueue {
41+
constructor(capacity) {
42+
this.items = new Array(capacity)
43+
this.capacity = capacity
44+
this.currentLength = 0
45+
this.rear = -1
46+
this.front = -1
47+
}
48+
49+
isFull() {
50+
return this.currentLength === this.capacity
51+
}
52+
53+
isEmpty() {
54+
return this.currentLength === 0
55+
}
56+
57+
enqueue(element) {
58+
if (!this.isFull()) {
59+
this.rear = (this.rear+1) % this.capacity
60+
this.items[this.rear] = element
61+
this.currentLength += 1
62+
63+
if (this.front === -1) {
64+
this.front = this.rear
65+
}
66+
}
67+
}
68+
69+
dequeue() {
70+
if (this.isEmpty()) {
71+
return null
72+
}
73+
74+
const item = this.items[this.front]
75+
this.items[this.front] = null
76+
this.front = (this.front+1) % this.capacity
77+
this.currentLength -= 1
78+
if (this.isEmpty()) {
79+
this.front = -1
80+
this.rear = -1
81+
}
82+
return item
83+
}
84+
85+
86+
peek() {
87+
if (!this.isEmpty()) {
88+
return this.items[this.front]
89+
}
90+
91+
return null
92+
}
93+
94+
print() {
95+
if (this.isEmpty()) {
96+
console.log('Queue is empty')
97+
} else {
98+
let i
99+
let str = ''
100+
for (i = this.front; i !== this.rear; i = (i+1) % this.capacity) {
101+
str += this.items[i] + ' '
102+
}
103+
str += this.items[i]
104+
console.log(str)
105+
}
106+
}
107+
}
108+
```
109+
110+
This is the implementation for the circular queue data structure in JavaScript. Let's check if it workks correctly.
111+
112+
```
113+
const circularQueue = new CircularQueue(5)
114+
console.log(circularQueue.isEmpty()) // true
115+
116+
circularQueue.enqueue(10)
117+
circularQueue.enqueue(20)
118+
circularQueue.enqueue(30)
119+
console.log(circularQueue.size()) // 3
120+
circularQueue.print() // 10,20,30
121+
122+
circularQueue.enqueue(40)
123+
circularQueue.enqueue(50)
124+
console.log(circularQueue.size()) // 5
125+
circularQueue.print() // 10,20,30,40,50
126+
127+
console.log(circularQueue.isFull()) // true
128+
```
129+
130+
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
---
2+
title: 'Data Structure: Queue in JavaScript'
3+
description: 'FIFO structure, operations, array/class implementations, performance, and pitfalls.'
4+
pubDate: 'Jan 4 2026'
5+
tags: 'js-data-structure'
6+
---
7+
8+
## Overview
9+
10+
The Queue data structure is a sequential collection of elements that follows the principle of First In First Out (FIFO).
11+
12+
The first element inserted into the queue is the first element to be removed.
13+
14+
A queue of people. People enter the queue at one end (rear/tail) and leave the queue from the other end (front/head).
15+
16+
Queue is an abstract data structure. It is defined by behavior rather than being a mathematical model.
17+
18+
The queue data structure supports two main operations.
19+
20+
- Enqueue: Add an element to the end of the queue.
21+
- Dequeue: Remove the element from the front of the queue.
22+
23+
Queue is useful when we have to do some operations in a particular order.
24+
25+
For e.g:
26+
- Printer to print multiple document
27+
- CPU task scheduling
28+
- Callback Queue in JavaScript
29+
30+
31+
### Queue Implementation
32+
- enqueue(element): add an element to the queue
33+
- dequeue - remove the oldest element from queue
34+
- peek - get the value of the element at the front of the queue without removing it.
35+
- isEmpty - check is the queue is empty
36+
- size - get the number of the element in a queue
37+
- print - visualize the elements in the queue
38+
39+
```
40+
class Queue {
41+
constructor() {
42+
this.items = []
43+
}
44+
45+
enqueue(element) {
46+
this.items.push(element)
47+
}
48+
49+
dequeue() {
50+
return this.items.shift()
51+
}
52+
53+
peek() {
54+
if (this.isEmpty()) {
55+
return null
56+
}
57+
58+
return this.items[0]
59+
}
60+
61+
isEmpty() {
62+
return this.items.length === 0
63+
}
64+
65+
size() {
66+
return this.items.length
67+
}
68+
69+
print() {
70+
console.log(this.items.toString())
71+
}
72+
}
73+
```
74+
75+
Let's instantiate the Queue class.
76+
77+
```
78+
const queue = new Queue()
79+
80+
console.log(queue.isEmpty()) // true
81+
queue.enqueue(10)
82+
queue.enqueue(20)
83+
queue.enqueue(30)
84+
console.log(queue.size()) // 3
85+
queue.print() // 10,20,30
86+
87+
88+
console.log(queue.dequeue()) // 10
89+
console.log(queue.peek()) // 20
90+
```
91+
92+
Here if you see the implmentation return this.items.shift() it remove the first element from the queue. This is going to be complexity of O(n) because it re-index the array after removing the first element.
93+
94+
```
95+
class Queue {
96+
constructor() {
97+
this.items = {}
98+
this.rear = 0
99+
this.front = 0
100+
}
101+
102+
enqueue(element) {
103+
this.items[this.rear] = element
104+
this.rear++
105+
}
106+
107+
dequeue() {
108+
const item = this.items[this.front]
109+
delete this.items[this.front]
110+
this.front++
111+
return item
112+
}
113+
114+
isEmpty() {
115+
return this.rear - this.front === 0
116+
}
117+
118+
peek() {
119+
return this.items[this.front]
120+
}
121+
122+
size() {
123+
return this.rear - this.front
124+
}
125+
126+
print() {
127+
console.log(this.items.toString())
128+
}
129+
}
130+
```
131+
132+
That is the optimized version of the queue implementation. How to use this class is similar to the previous implementation.
133+
134+
```
135+
const queue = new Queue()
136+
137+
console.log(queue.isEmpty()) // true
138+
queue.enqueue(10)
139+
queue.enqueue(20)
140+
queue.enqueue(30)
141+
console.log(queue.size()) // 3
142+
queue.print() // 10,20,30
143+
144+
145+
console.log(queue.dequeue()) // 10
146+
console.log(queue.peek()) // 20
147+
```
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
---
2+
title: 'Data Structure: Stack in JavaScript'
3+
description: 'LIFO structure, operations, implementations, performance, and pitfalls using arrays and classes.'
4+
pubDate: 'Jan 4 2026'
5+
tags: 'js-data-structure'
6+
---
7+
8+
## Overview
9+
10+
The stack data structure is a sequential collection of elements that follows the principle of last in first out (LIFO).
11+
12+
The last element inserted into the stack is the first element to be removed.
13+
14+
A stack of plates, The last plate placed on top of the stack is also the first plate removed from the stack.
15+
16+
Stack is an abstract data type. It is defined by it's behavior rather than being a mathematical model.
17+
18+
The stack data structure supports two main operations:
19+
20+
- Push: Add an element to the top of the stack.
21+
- Pop: Remove the element from the top of the stack.
22+
23+
24+
Stack Usage:
25+
26+
- Browser history tracking
27+
- undo operations while typing
28+
- Expression conversation
29+
- Call Stack in Javascript runtime
30+
31+
```
32+
class Stack {
33+
constructor() {
34+
this.items = [];
35+
}
36+
37+
// Add element to top of stack
38+
push(element) {
39+
this.items.push(element);
40+
}
41+
42+
// Remove and return top element
43+
pop() {
44+
if (this.isEmpty()) {
45+
return null;
46+
}
47+
return this.items.pop();
48+
}
49+
50+
// View top element without removing
51+
peek() {
52+
if (this.isEmpty()) {
53+
return null;
54+
}
55+
return this.items[this.items.length - 1];
56+
}
57+
58+
// Check if stack is empty
59+
isEmpty() {
60+
return this.items.length === 0;
61+
}
62+
63+
// Get stack size
64+
size() {
65+
return this.items.length;
66+
}
67+
68+
// Clear all elements
69+
clear() {
70+
this.items = [];
71+
}
72+
73+
// Print stack contents
74+
print() {
75+
console.log(this.items.toString());
76+
}
77+
}
78+
79+
// Example usage
80+
const stack = new Stack();
81+
stack.push(10);
82+
stack.push(20);
83+
stack.push(30);
84+
console.log(stack.peek()); // 30
85+
console.log(stack.pop()); // 30
86+
console.log(stack.size()); // 2
87+
```
88+
89+
90+
## Big‑O Complexity
91+
92+
| Operation | Complexity |
93+
| --------- | ---------- |
94+
| push | O(1) |
95+
| pop | O(1) |
96+
| peek | O(1) |
97+
| size | O(1) |
98+
| iterate | O(n) |
99+
100+

0 commit comments

Comments
 (0)