Skip to content

Commit c41f34b

Browse files
committed
fixed typos and formatting
1 parent 5d368a6 commit c41f34b

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

data_structures/linkedlist_queue.ts

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
/* The queue data strcture is sequential collection of elements that
2-
follows the priciple of FIFO */
3-
/*here we are using a singly linkedlist so we are only going to traverse to the next node */
1+
/**
2+
* This is an linkedlist-based implementation of a Queue.
3+
* A Queue is a data structure that follows the FIFO (First In First Out) principle.
4+
* It means that the first element that was added to the queue will be the first one to be removed.
5+
* The time complexity of the operations is O(n).
6+
7+
*/
8+
9+
/** USAGE
410
5-
/*USAGE
611
*Printers
712
*CPU task scheduling
813
*Callback queue in javascript
@@ -15,49 +20,52 @@ type Node<T> = {
1520
}
1621

1722

18-
export class LinkedlistQueue<T> {
23+
export class LinkedQueue<T> {
1924

2025
public length: number;
2126
public head?: Node<T>;
2227
private tail?: Node<T>;
2328

2429
constructor() {
25-
this.head = this.tail = undefined; //when a new linkedlist is created the head becomes equal to the tail .
30+
this.head = this.tail = undefined;
2631
this.length = 0;
2732
}
2833

29-
// adds elements to the rear/tail of the collection.
34+
// adds elements to the rear/tail of the Queue
3035
enqueue(item: T): void {
31-
const node = { value: item } as Node<T>; //creates a new node
32-
this.length++ //increase the length of the linkedlist
36+
const node = { value: item } as Node<T>; // Creates a new node
37+
this.length++ // Increase the length of the Queue
3338

3439

3540
if (!this.tail) {
3641
this.tail = this.head = node;
3742
return;
3843
}
39-
this.tail.next = node; //updates the next tail to the node created
40-
this.tail = node; //the tail of the linkedlist then becomes the node created!!
44+
this.tail.next = node; // Updates the next tail to the node created
45+
this.tail = node; // The tail of the Queue then becomes the node created!!
4146

4247
}
43-
//remove elements to the front/head of the collection
48+
// Remove elements to the front/head of the Queue
4449
deque(): T | undefined {
50+
51+
// If there is no head return undefined
4552
if (!this.head) {
46-
return undefined; //if there is no head return undefined
53+
return undefined;
4754
}
55+
4856
this.length--;
49-
let head = this.head; //we store the head in order not to lose track of it
50-
this.head = this.head.next; //update the the head to the next node
51-
return head.value; // return the value of the head
57+
let head = this.head; // We store the head in order not to lose track of it
58+
this.head = this.head.next; // Update the the head to the next node
59+
return head.value; // Return the value of the head
5260
}
5361

5462

55-
// returns the value of the head
63+
// Returns the value of the head
5664
peek(): T | undefined {
5765
return this.head?.value;
5866
}
5967

60-
// checks if the the linkedlist is empty
68+
// Returns true if the Queue is empty
6169
isEmpty(): boolean {
6270
return this.length == 0
6371
}

data_structures/test/linkedlist_queue.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11

2-
import { LinkedlistQueue } from './../linkedlist_queue';
2+
import { LinkedQueue } from './../linkedlist_queue';
33

44
describe("Testing Queue data structure", () => {
55
it("enqueue should add a new element to the queue", () => {
6-
const queue = new LinkedlistQueue<number>();
6+
const queue = new LinkedQueue<number>();
77
queue.enqueue(1);
88

99
expect(queue.length).toBe(1);
1010
});
1111

1212
it("isEmpty should return true on empty queue", () => {
13-
const queue = new LinkedlistQueue<number>();
13+
const queue = new LinkedQueue<number>();
1414
expect(queue.isEmpty()).toBeTruthy();
1515
});
1616

1717
it("isEmpty should return false on not empty queue", () => {
18-
const queue = new LinkedlistQueue<number>();
18+
const queue = new LinkedQueue<number>();
1919
queue.enqueue(1);
2020

2121
expect(queue.isEmpty()).toBeFalsy();
@@ -24,7 +24,7 @@ describe("Testing Queue data structure", () => {
2424

2525

2626
it("length should return the number of elements in the queue", () => {
27-
const queue = new LinkedlistQueue<number>();
27+
const queue = new LinkedQueue<number>();
2828
queue.enqueue(1);
2929
queue.enqueue(1);
3030
queue.enqueue(1);
@@ -33,7 +33,7 @@ describe("Testing Queue data structure", () => {
3333
});
3434

3535
it("dequeue should remove the first element", () => {
36-
const queue = new LinkedlistQueue<number>();
36+
const queue = new LinkedQueue<number>();
3737
queue.enqueue(1);
3838
queue.enqueue(2);
3939
queue.enqueue(3);

0 commit comments

Comments
 (0)