Skip to content

Commit 77219d3

Browse files
committed
chapter 05: [LinkedLists]
1 parent 438f96e commit 77219d3

File tree

9 files changed

+406
-6
lines changed

9 files changed

+406
-6
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ Work in Progress.
1919
### Third Edition Updates
2020

2121
* Algorithms using ES2015+
22+
* All chapters reviewed and rewritten
23+
* New data structures and algorithms
24+
* Three (3) new chapters
2225
* Creation of a Data Structures and Algorithms library that can be used in the browser or with Node.js
2326
* Algorithms tested with Mocha + Chai (test code available in `test` directory)
2427
* TypeScript version of the source code included

examples/PacktDataStructuresAlgorithms.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import DoublyLinkedList from './doubly-linked-list';
2+
3+
export default class StackLinkedList {
4+
constructor() {
5+
this.items = new DoublyLinkedList();
6+
}
7+
push(element) {
8+
this.items.push(element);
9+
}
10+
pop() {
11+
if (this.isEmpty()) {
12+
return undefined;
13+
}
14+
const result = this.items.removeAt(this.size() - 1);
15+
return result;
16+
}
17+
peek() {
18+
if (this.isEmpty()) {
19+
return undefined;
20+
}
21+
return this.items.getElementAt(this.size() - 1).element;
22+
}
23+
isEmpty() {
24+
return this.items.isEmpty();
25+
}
26+
size() {
27+
return this.items.size();
28+
}
29+
clear() {
30+
this.items.clear();
31+
}
32+
toString() {
33+
return this.items.toString();
34+
}
35+
}

src/js/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import { parenthesesChecker } from './others/balanced-symbols';
1010

1111
import * as _util from './util';
1212

13-
export { default as CircularLinkedList } from './data-structures/circular-linked-list';
14-
export { default as DoublyLinkedList } from './data-structures/doubly-linked-list';
1513
export { default as LinkedList } from './data-structures/linked-list';
14+
export { default as DoublyLinkedList } from './data-structures/doubly-linked-list';
15+
export { default as CircularLinkedList } from './data-structures/circular-linked-list';
1616
export { default as SortedLinkedList } from './data-structures/sorted-linked-list';
17+
export { default as StackLinkedList } from './data-structures/stack-linked-list';
1718

1819
export const util = _util;
1920

src/ts/data-structures/doubly-linked-list.ts

-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ export default class DoublyLinkedList<T> extends LinkedList<T> {
4242
}
4343
} else if (index === this.count) {
4444
// last item // NEW
45-
4645
current = this.tail; // {2}
4746
current.next = node;
4847
node.prev = current;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import DoublyLinkedList from './doubly-linked-list';
2+
3+
export default class StackLinkedList<T> {
4+
private items: DoublyLinkedList<T>;
5+
6+
constructor() {
7+
this.items = new DoublyLinkedList<T>();
8+
}
9+
10+
push(element: T) {
11+
this.items.push(element);
12+
}
13+
14+
pop() {
15+
if (this.isEmpty()) {
16+
return undefined;
17+
}
18+
const result = this.items.removeAt(this.size() - 1);
19+
return result;
20+
}
21+
22+
peek() {
23+
if (this.isEmpty()) {
24+
return undefined;
25+
}
26+
return this.items.getElementAt(this.size() - 1).element;
27+
}
28+
29+
isEmpty() {
30+
return this.items.isEmpty();
31+
}
32+
33+
size() {
34+
return this.items.size();
35+
}
36+
37+
clear() {
38+
this.items.clear();
39+
}
40+
41+
toString() {
42+
return this.items.toString();
43+
}
44+
}

src/ts/index.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import * as _util from './util';
22

3-
export { default as CircularLinkedList } from './data-structures/circular-linked-list';
4-
export { default as DoublyLinkedList } from './data-structures/doubly-linked-list';
53
export { default as LinkedList } from './data-structures/linked-list';
4+
export { default as DoublyLinkedList } from './data-structures/doubly-linked-list';
5+
export { default as CircularLinkedList } from './data-structures/circular-linked-list';
66
export { default as SortedLinkedList } from './data-structures/sorted-linked-list';
7+
export { default as StackLinkedList } from './data-structures/stack-linked-list';
78

89
export const util = _util;
910

11+
1012
/* import { hotPotato } from './others/hot-potato';
1113
import { palindromeChecker } from './others/palindrome-checker';
1214
import Deque from './data-structures/deque';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { StackLinkedList } from '../../../src/js/index';
4+
import MyObj from './my-obj';
5+
6+
describe('StackLinkedList', () => {
7+
let stack;
8+
9+
beforeEach(() => {
10+
stack = new StackLinkedList();
11+
});
12+
13+
it('starts empty', () => {
14+
expect(stack.size()).to.equal(0);
15+
expect(stack.isEmpty()).to.equal(true);
16+
});
17+
18+
it('pushes elements', () => {
19+
stack.push(1);
20+
expect(stack.size()).to.equal(1);
21+
stack.push(2);
22+
expect(stack.size()).to.equal(2);
23+
stack.push(3);
24+
expect(stack.size()).to.equal(3);
25+
26+
expect(stack.isEmpty()).to.equal(false);
27+
});
28+
29+
it('pops elements', () => {
30+
stack.push(1);
31+
stack.push(2);
32+
stack.push(3);
33+
34+
expect(stack.pop()).to.equal(3);
35+
expect(stack.pop()).to.equal(2);
36+
expect(stack.pop()).to.equal(1);
37+
expect(stack.pop()).to.equal(undefined);
38+
});
39+
40+
it('implements LIFO logic', () => {
41+
stack.push(1);
42+
stack.push(2);
43+
stack.push(3);
44+
45+
expect(stack.pop()).to.equal(3);
46+
expect(stack.pop()).to.equal(2);
47+
expect(stack.pop()).to.equal(1);
48+
expect(stack.pop()).to.equal(undefined);
49+
});
50+
51+
it('allows to peek at the top element in he stack without popping it', () => {
52+
expect(stack.peek()).to.equal(undefined);
53+
54+
stack.push(1);
55+
expect(stack.peek()).to.equal(1);
56+
57+
stack.push(2);
58+
expect(stack.peek()).to.equal(2);
59+
60+
stack.pop();
61+
expect(stack.peek()).to.equal(1);
62+
});
63+
64+
it('returns the correct size', () => {
65+
expect(stack.size()).to.equal(0);
66+
stack.push(1);
67+
expect(stack.size()).to.equal(1);
68+
stack.push(2);
69+
expect(stack.size()).to.equal(2);
70+
stack.push(3);
71+
expect(stack.size()).to.equal(3);
72+
73+
stack.clear();
74+
expect(stack.isEmpty()).to.equal(true);
75+
76+
stack.push(1);
77+
stack.push(2);
78+
stack.push(3);
79+
80+
stack.pop();
81+
expect(stack.size()).to.equal(2);
82+
stack.pop();
83+
expect(stack.size()).to.equal(1);
84+
stack.pop();
85+
expect(stack.size()).to.equal(0);
86+
stack.pop();
87+
expect(stack.size()).to.equal(0);
88+
});
89+
90+
it('returns if it is empty', () => {
91+
expect(stack.isEmpty()).to.equal(true);
92+
stack.push(1);
93+
expect(stack.isEmpty()).to.equal(false);
94+
stack.push(2);
95+
expect(stack.isEmpty()).to.equal(false);
96+
stack.push(3);
97+
expect(stack.isEmpty()).to.equal(false);
98+
99+
stack.clear();
100+
expect(stack.isEmpty()).to.equal(true);
101+
102+
stack.push(1);
103+
stack.push(2);
104+
stack.push(3);
105+
106+
stack.pop();
107+
expect(stack.isEmpty()).to.equal(false);
108+
stack.pop();
109+
expect(stack.isEmpty()).to.equal(false);
110+
stack.pop();
111+
expect(stack.isEmpty()).to.equal(true);
112+
stack.pop();
113+
expect(stack.isEmpty()).to.equal(true);
114+
});
115+
116+
it('clears the stack', () => {
117+
stack.clear();
118+
expect(stack.isEmpty()).to.equal(true);
119+
120+
stack.push(1);
121+
stack.push(2);
122+
123+
stack.clear();
124+
expect(stack.isEmpty()).to.equal(true);
125+
});
126+
127+
it('returns toString primitive types', () => {
128+
expect(stack.toString()).to.equal('');
129+
130+
stack.push(1);
131+
expect(stack.toString()).to.equal('1');
132+
133+
stack.push(2);
134+
expect(stack.toString()).to.equal('1,2');
135+
136+
stack.clear();
137+
expect(stack.toString()).to.equal('');
138+
139+
const stackString = new StackLinkedList();
140+
stackString.push('el1');
141+
expect(stackString.toString()).to.equal('el1');
142+
143+
stackString.push('el2');
144+
expect(stackString.toString()).to.equal('el1,el2');
145+
});
146+
147+
it('returns toString objects', () => {
148+
const stackMyObj = new StackLinkedList();
149+
expect(stackMyObj.toString()).to.equal('');
150+
151+
stackMyObj.push(new MyObj(1, 2));
152+
expect(stackMyObj.toString()).to.equal('1|2');
153+
154+
stackMyObj.push(new MyObj(3, 4));
155+
expect(stackMyObj.toString()).to.equal('1|2,3|4');
156+
});
157+
});

0 commit comments

Comments
 (0)