Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9a6c460

Browse files
Implemented queue (Closes #4)
The following data structures have been implemented: Array, Hash Table, Stack, Queue, Binary Search Tree. All are available for import.
1 parent 3a10f9f commit 9a6c460

File tree

5 files changed

+144
-6
lines changed

5 files changed

+144
-6
lines changed

‎Data-Structures/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
## Core
44
- [X] Arrays
55
- [X] Hash Tables
6-
- [] Stacks
7-
- [] Queues
6+
- [X] Stacks
7+
- [X] Queues
88
- [ ] Linked Lists
99
- [X] Trees
1010
- [X] Binary Search Tree (BST)

‎Data-Structures/Sequential/Queue.ts

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import Node from '../Linked-Lists/NodeLL.ts';
2+
3+
4+
export default class Queue<T> {
5+
private last: Node<T> | null;
6+
private first: Node<T>| null;
7+
private length: number;
8+
9+
constructor() {
10+
this.last = null;
11+
this.first = null;
12+
this.length = 0;
13+
}
14+
15+
public getLength(): number {
16+
return this.length;
17+
}
18+
19+
public peek(): T | null {
20+
return this.first?.getValue() || null;
21+
}
22+
23+
public enqueue(value: T): boolean {
24+
const newNode = new Node(value);
25+
26+
if (!this.last) { // If empty queue, initialize
27+
this.first = newNode;
28+
this.last = newNode;
29+
}
30+
else {
31+
this.last.setNext(newNode);
32+
this.last = this.last.getNext();
33+
}
34+
35+
++this.length;
36+
return true;
37+
}
38+
39+
public dequeue(): T | null {
40+
if (!this.first) return null; // Edge case: Empty queue
41+
42+
if (this.length === 1) { // Edge case: Queue has 1 element, so a dequeue should reset the queue's state
43+
this.last = null; // Challenge: What is the state of each 'class field' after a dequeue when 1 element was remaining?
44+
}
45+
46+
const value = this.first.getValue();
47+
this.first = this.first.getNext();
48+
49+
--this.length;
50+
return value;
51+
}
52+
}
53+
54+
function printQueue(queue: Queue<any>) {
55+
console.log(JSON.stringify(queue));
56+
}
57+
58+
function printPeekQueue(queue: Queue<any>) {
59+
console.log('Peeking... Found', JSON.stringify(queue.peek()));
60+
}
61+
62+
function printDequeue(queue: Queue<any>) {
63+
console.log('Dequeued:', JSON.stringify(queue.dequeue()));
64+
}
65+
66+
67+
//---------------------------------------------------------------------
68+
// ---------- MAIN PROGRAM ----------
69+
//---------------------------------------------------------------------
70+
if (import.meta.main) {
71+
72+
const ATLA = new Queue<string>();
73+
74+
printPeekQueue(ATLA);
75+
ATLA.enqueue('Sokka');
76+
ATLA.enqueue('Katara');
77+
printPeekQueue(ATLA);
78+
ATLA.enqueue('Aang');
79+
ATLA.enqueue('Appa');
80+
81+
printQueue(ATLA);
82+
83+
printDequeue(ATLA);
84+
printDequeue(ATLA);
85+
printDequeue(ATLA);
86+
87+
printQueue(ATLA);
88+
89+
printDequeue(ATLA);
90+
91+
printQueue(ATLA);
92+
93+
ATLA.enqueue('Zuko');
94+
ATLA.enqueue('Iroh');
95+
96+
printQueue(ATLA);
97+
98+
// RUN: deno run Data-Structures/Sequential/Queue.ts
99+
}
100+
101+
102+
// --------------------------- Terminal Output: ---------------------------
103+
// Peeking... Found null
104+
// Peeking... Found "Sokka"
105+
// {"last":{"value":"Appa","next":null},"first":{"value":"Sokka","next":{"value":"Katara","next":{"value":"Aang","next":{"value":"Appa","next":null}}}},"length":4}
106+
// Dequeued: "Sokka"
107+
// Dequeued: "Katara"
108+
// Dequeued: "Aang"
109+
// {"last":{"value":"Appa","next":null},"first":{"value":"Appa","next":null},"length":1}
110+
// Dequeued: "Appa"
111+
// {"last":null,"first":null,"length":0}
112+
// {"last":{"value":"Iroh","next":null},"first":{"value":"Zuko","next":{"value":"Iroh","next":null}},"length":2}

‎Data-Structures/Sequential/Stack.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,17 @@ if (import.meta.main) {
7373
printStack(ATLA);
7474

7575
// RUN: deno run Data-Structures/Sequential/Stack.ts
76-
}
76+
}
77+
78+
79+
// --------------------------- Terminal Output: ---------------------------
80+
// Peeking... Found null
81+
// Peeking... Found Katara
82+
// {"length":4,"values":["Sokka","Katara","Aang","Appa"]}
83+
// Popped: Appa
84+
// {"length":3,"values":["Sokka","Katara","Aang",null]}
85+
// Popped: Aang
86+
// Popped: Katara
87+
// Popped: Sokka
88+
// {"length":0,"values":[null,null,null,null]}
89+
// {"length":2,"values":["Zuko","Iroh",null,null]}

‎Data-Structures/Sequential/StackLL.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,17 @@ if (import.meta.main) {
106106
printStack(ATLA);
107107

108108
// RUN: deno run Data-Structures/Sequential/StackLL.ts
109-
}
109+
}
110+
111+
112+
// --------------------------- Terminal Output: ---------------------------
113+
// Peeking: null
114+
// Peeking: Katara
115+
// {"top":{"value":"Appa","next":{"value":"Aang","next":{"value":"Katara","next":{"value":"Sokka","next":null}}}},"bottom":{"value":"Sokka","next":null},"length":4}
116+
// Popped: Appa
117+
// {"top":{"value":"Aang","next":{"value":"Katara","next":{"value":"Sokka","next":null}}},"bottom":{"value":"Sokka","next":null},"length":3}
118+
// Popped: Aang
119+
// Popped: Katara
120+
// Popped: Sokka
121+
// {"top":null,"bottom":null,"length":0}
122+
// {"top":{"value":"Iroh","next":{"value":"Zuko","next":null}},"bottom":{"value":"Zuko","next":null},"length":2}

‎README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
### Data Structures
1111
- [X] Arrays
1212
- [X] Hash Tables
13-
- [] Stacks
14-
- [] Queues
13+
- [X] Stacks
14+
- [X] Queues
1515
- [ ] Linked Lists
1616
- [X] Trees
1717
- [ ] Graphs

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /