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 af019f0

Browse files
Implemented LL-based Stack (#3 IP)
Implemented linked list style Stack. Created reusable Linked List Node.
1 parent da6fe2d commit af019f0

File tree

5 files changed

+132
-1
lines changed

5 files changed

+132
-1
lines changed

‎Algorithms/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@
1515
- [ ] Breadth First Search (BFS)
1616
- [ ] Depth First Search (DFS)
1717

18-
## Resources
18+
## Resources
19+
- [Visualizing Data Structures & Algorithms](https://visualgo.net/en)

‎Data-Structures/Linked-Lists/NodeLL.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export default class LinkedListNode<T> {
2+
private value: T;
3+
private next: LinkedListNode<T> | null;
4+
5+
constructor(value: T) {
6+
this.value = value;
7+
this.next = null;
8+
}
9+
10+
public setValue(value: T) {
11+
this.value = value;
12+
}
13+
14+
public setNext(nextNode: LinkedListNode<T>) {
15+
this.next = nextNode;
16+
}
17+
18+
public getValue(): T {
19+
return this.value;
20+
}
21+
22+
public getNext(): LinkedListNode<T> | any {
23+
return this.next;
24+
}
25+
}

‎Data-Structures/Sequential/Queue.ts

Whitespace-only changes.

‎Data-Structures/Sequential/Stack.ts

Whitespace-only changes.

‎Data-Structures/Sequential/StackLL.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import Node from '../Linked-Lists/NodeLL.ts';
2+
3+
4+
export default class StackLL<T> {
5+
private top: Node<T> | null;
6+
private bottom: Node<T> | null;
7+
private length: number;
8+
9+
constructor() {
10+
this.top = null;
11+
this.bottom = null;
12+
this.length = 0;
13+
}
14+
15+
public peek(): T | null {
16+
return this.top?.getValue() || null;
17+
}
18+
19+
public push(value: T): boolean {
20+
const newNode = new Node<T>(value);
21+
22+
if (!this.top) { // If stack is empty, initialize
23+
this.top = newNode;
24+
this.bottom = newNode;
25+
}
26+
else { // Else manipulate pointers
27+
newNode.setNext(this.top);
28+
this.top = newNode;
29+
}
30+
/* Challenge (Alternate Else Clause):
31+
* 1. How does the code below behave and why is it incorrect?
32+
* 2. What are the consequences to time complexity?
33+
*/
34+
// else {
35+
// this.top.setNext(newNode); // Else, set next node of LL to new value
36+
// this.top = this.top.getNext(); // And move 'top' pointer forward
37+
// }
38+
39+
++this.length; // Increment length
40+
return true;
41+
}
42+
43+
public pop(): T | null {
44+
if (!this.top) return null; // If empty stack, return null
45+
46+
if (this.length === 1) {
47+
this.bottom = null;
48+
}
49+
50+
const value = this.top.getValue(); // Retrieve top value of stack
51+
this.top = this.top.getNext(); // Move top pointer to next node
52+
53+
// On line 46, the reference to the top most node of the stack is lost
54+
// The node is now floating in memory, waiting patiently for the garbage collector
55+
56+
--this.length;
57+
return value;
58+
}
59+
}
60+
61+
function printStack(stack: StackLL<any>) {
62+
console.log(JSON.stringify(stack));
63+
}
64+
65+
function printPopStack(stack: StackLL<any>) {
66+
console.log('Popped:', stack.pop());
67+
}
68+
69+
function printPeekStack(stack: StackLL<any>) {
70+
console.log('Peeking:', stack.peek());
71+
}
72+
73+
//---------------------------------------------------------------------
74+
// ---------- MAIN PROGRAM ----------
75+
//---------------------------------------------------------------------
76+
if (import.meta.main) {
77+
78+
const ATLA = new StackLL<string>();
79+
80+
printPeekStack(ATLA);
81+
ATLA.push('Sokka');
82+
ATLA.push('Katara');
83+
printPeekStack(ATLA);
84+
ATLA.push('Aang');
85+
ATLA.push('Appa');
86+
87+
printStack(ATLA);
88+
89+
printPopStack(ATLA);
90+
91+
printStack(ATLA);
92+
93+
printPopStack(ATLA);
94+
printPopStack(ATLA);
95+
printPopStack(ATLA);
96+
97+
printStack(ATLA);
98+
99+
ATLA.push('Zuko');
100+
ATLA.push('Iroh');
101+
102+
printStack(ATLA);
103+
104+
// RUN: deno run Data-Structures/Sequential/StackLL.ts
105+
}

0 commit comments

Comments
(0)

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