Skip to content

Commit 5b46b78

Browse files
committed
Add day 41
1 parent 276a4ee commit 5b46b78

File tree

8 files changed

+132
-0
lines changed

8 files changed

+132
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Motivate yourself to code daily till 60 days, and see the magic! Coding will bec
6868
| [Day 38](./day38) | [Implement Stack Data Structure](./day38) | [http://codetoexpress.tech/dc/day38/](http://codetoexpress.tech/dc/day38/) | **Beginner** |
6969
| [Day 39](./day39) | [Maximum Element and Reverse Stack](./day39) | [http://codetoexpress.tech/dc/day39/](http://codetoexpress.tech/dc/day39/) | **Intermediate** |
7070
| [Day 40](./day40) | [Prefix, Infix, Postfix Conversion](./day40) | [http://codetoexpress.tech/dc/day40/](http://codetoexpress.tech/dc/day40/) | **Intermediate** |
71+
| [Day 41](./day41) | [Implement Queue Data Structure](./day41) | [http://codetoexpress.tech/dc/day41/](http://codetoexpress.tech/dc/day41/) | **Beginner** |
7172

7273
## [More Problems](./BONUS/README.md)
7374

day39/ques.png

778 KB
Loading

day40/JavaScript/queue.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Implementation of queue usinig JS
3+
* @author MadhavBahlMD
4+
* @date 14/02/2019
5+
*/
6+
7+
class Queue {
8+
constructor (size) {
9+
this.capacty = size;
10+
this.data = [];
11+
this.frontIndex = 0;
12+
this.rearIndex = 0;
13+
}
14+
15+
front () {
16+
return data[this.frontIndex];
17+
}
18+
19+
rear () {
20+
return data[this.rearIndex];
21+
}
22+
23+
enqueue (element) {
24+
if (this.rearIndex >= this.capacty) {
25+
console.log ("Overflow!");
26+
return -1;
27+
}
28+
this.data.unshift (element);
29+
console.log (element + ' added to the queue');
30+
this.rearIndex++;
31+
}
32+
33+
dequeue (element) {
34+
if (this.rearIndex === 0) {
35+
console.log ("Underflow!");
36+
return -1;
37+
}
38+
console.log (this.data[this.rearIndex -1] + ' has been removed from the queue');
39+
this.rearIndex--;
40+
return this.data.pop ();
41+
}
42+
}
43+
44+
const myQ = new Queue (4);
45+
46+
myQ.dequeue ( );
47+
myQ.enqueue (1);
48+
myQ.enqueue (2);
49+
myQ.enqueue (3);
50+
myQ.enqueue (4);
51+
myQ.enqueue (5);
52+
myQ.dequeue ( );
File renamed without changes.
File renamed without changes.
File renamed without changes.

day41/README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
![cover](./cover.png)
2+
3+
# Day 41 - Implement a Queue
4+
5+
Just like a stack, queue is also a linear data structure which follows a particular order in which the operations are performed. The order is called FIFO (First In First Out) which means the first element to enter the queue will be the last one to exit
6+
7+
There are many real-life examples of a queue, and the most common one of them is the real queue. It can be a queue of people waiting at billing counter, or a queue of people waiting at a ticket counter etc.
8+
9+
A Queue has various methods -
10+
11+
1. `Enqueue()` - **Adds an item to the queue**
12+
2. `Dequeue()` - **Removes an item from the queue**
13+
3. `Front()` - **Get the front item from queue**
14+
4. `Rear()` - **Get the last item from queue**
15+
16+
Try to implement a queue 😁
17+
18+
Read more about queues here: https://www.geeksforgeeks.org/queue-set-1introduction-and-array-implementation/
19+
20+
## Solution
21+
22+
## JavaScript Implementation
23+
24+
### [Solution](./JavaScript/queue.js)
25+
26+
```js
27+
/**
28+
* Implementation of queue usinig JS
29+
* @author MadhavBahlMD
30+
* @date 14/02/2019
31+
*/
32+
33+
class Queue {
34+
constructor (size) {
35+
this.capacty = size;
36+
this.data = [];
37+
this.frontIndex = 0;
38+
this.rearIndex = 0;
39+
}
40+
41+
front () {
42+
return data[this.frontIndex];
43+
}
44+
45+
rear () {
46+
return data[this.rearIndex];
47+
}
48+
49+
enqueue (element) {
50+
if (this.rearIndex >= this.capacty) {
51+
console.log ("Overflow!");
52+
return -1;
53+
}
54+
this.data.unshift (element);
55+
console.log (element + ' added to the queue');
56+
this.rearIndex++;
57+
}
58+
59+
dequeue (element) {
60+
if (this.rearIndex === 0) {
61+
console.log ("Underflow!");
62+
return -1;
63+
}
64+
console.log (this.data[this.rearIndex -1] + ' has been removed from the queue');
65+
this.rearIndex--;
66+
return this.data.pop ();
67+
}
68+
}
69+
70+
const myQ = new Queue (4);
71+
72+
myQ.dequeue ( );
73+
myQ.enqueue (1);
74+
myQ.enqueue (2);
75+
myQ.enqueue (3);
76+
myQ.enqueue (4);
77+
myQ.enqueue (5);
78+
myQ.dequeue ( );
79+
```

day41/cover.png

136 KB
Loading

0 commit comments

Comments
 (0)