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 174bed9

Browse files
committed
chapter 6: linked lists
1 parent 2633b83 commit 174bed9

File tree

10 files changed

+1534
-0
lines changed

10 files changed

+1534
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import {describe, expect, test, beforeEach} from '@jest/globals';
2+
import DoublyLinkedList from '../doubly-linked-list';
3+
4+
describe('DoublyLinkedList', () => {
5+
let doublyLinkedList: DoublyLinkedList<number>;
6+
7+
beforeEach(() => {
8+
doublyLinkedList = new DoublyLinkedList<number>();
9+
});
10+
11+
test('should create an empty doubly linked list', () => {
12+
expect(doublyLinkedList.toString()).toBe('');
13+
});
14+
15+
test('should append node to doubly linked list', () => {
16+
doublyLinkedList.append(1);
17+
doublyLinkedList.append(2);
18+
doublyLinkedList.append(3);
19+
expect(doublyLinkedList.toString()).toBe('1, 2, 3');
20+
});
21+
22+
test('should prepend node to doubly linked list', () => {
23+
doublyLinkedList.prepend(2);
24+
expect(doublyLinkedList.toString()).toBe('2');
25+
doublyLinkedList.append(1);
26+
doublyLinkedList.prepend(3);
27+
expect(doublyLinkedList.toString()).toBe('3, 2, 1');
28+
});
29+
30+
test('should insert node at position 0', () => {
31+
doublyLinkedList.append(1);
32+
doublyLinkedList.insert(0, 2);
33+
expect(doublyLinkedList.toString()).toBe('2, 1');
34+
});
35+
36+
test('should insert node at given position', () => {
37+
doublyLinkedList.append(1);
38+
doublyLinkedList.append(3);
39+
doublyLinkedList.insert(1, 2);
40+
expect(doublyLinkedList.toString()).toBe('1, 2, 3');
41+
});
42+
43+
test('should insert node at invalid position', () => {
44+
doublyLinkedList.append(1);
45+
doublyLinkedList.append(3);
46+
expect(doublyLinkedList.insert(3, 2)).toBe(false);
47+
expect(doublyLinkedList.toString()).toBe('1, 3');
48+
});
49+
50+
test('should remove node from doubly linked list', () => {
51+
doublyLinkedList.append(1);
52+
doublyLinkedList.append(2);
53+
doublyLinkedList.append(3);
54+
doublyLinkedList.removeAt(1);
55+
expect(doublyLinkedList.toString()).toBe('1, 3');
56+
});
57+
58+
test('should remove node at position 0', () => {
59+
doublyLinkedList.append(1);
60+
doublyLinkedList.append(2);
61+
doublyLinkedList.removeAt(0);
62+
expect(doublyLinkedList.toString()).toBe('2');
63+
});
64+
65+
test('should remove node at invalid position', () => {
66+
doublyLinkedList.append(1);
67+
doublyLinkedList.append(3);
68+
expect(doublyLinkedList.removeAt(3)).toBe(false);
69+
expect(doublyLinkedList.toString()).toBe('1, 3');
70+
});
71+
72+
test('should remove element from doubly linked list', () => {
73+
doublyLinkedList.append(1);
74+
doublyLinkedList.append(2);
75+
doublyLinkedList.append(3);
76+
doublyLinkedList.remove(3);
77+
expect(doublyLinkedList.toString()).toBe('1, 2');
78+
doublyLinkedList.remove(1);
79+
expect(doublyLinkedList.toString()).toBe('2');
80+
doublyLinkedList.remove(2);
81+
expect(doublyLinkedList.toString()).toBe('');
82+
});
83+
84+
test('should remove element that is not in doubly linked list', () => {
85+
doublyLinkedList.append(1);
86+
doublyLinkedList.append(2);
87+
expect(doublyLinkedList.remove(3)).toBe(false);
88+
expect(doublyLinkedList.toString()).toBe('1, 2');
89+
});
90+
91+
test('should find element in doubly linked list', () => {
92+
doublyLinkedList.append(1);
93+
doublyLinkedList.append(2);
94+
doublyLinkedList.append(3);
95+
expect(doublyLinkedList.indexOf(1)).toBe(0);
96+
expect(doublyLinkedList.indexOf(2)).toBe(1);
97+
expect(doublyLinkedList.indexOf(3)).toBe(2);
98+
expect(doublyLinkedList.indexOf(4)).toBe(-1);
99+
});
100+
101+
test('should check if doubly linked list is empty', () => {
102+
expect(doublyLinkedList.isEmpty()).toBe(true);
103+
doublyLinkedList.append(1);
104+
expect(doublyLinkedList.isEmpty()).toBe(false);
105+
});
106+
107+
test('should return the size of the doubly linked list', () => {
108+
expect(doublyLinkedList.getSize()).toBe(0);
109+
doublyLinkedList.append(1);
110+
expect(doublyLinkedList.getSize()).toBe(1);
111+
doublyLinkedList.append(2);
112+
expect(doublyLinkedList.getSize()).toBe(2);
113+
});
114+
115+
test('should convert doubly linked list to string', () => {
116+
doublyLinkedList.append(1);
117+
expect(doublyLinkedList.toString()).toBe('1');
118+
doublyLinkedList.append(2);
119+
expect(doublyLinkedList.toString()).toBe('1, 2');
120+
});
121+
122+
});
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
import {describe, expect, test, beforeEach} from '@jest/globals';
2+
import LinkedList from '../linked-list';
3+
4+
describe('LinkedList', () => {
5+
let linkedList: LinkedList<number>;
6+
7+
beforeEach(() => {
8+
linkedList = new LinkedList<number>();
9+
});
10+
11+
test('should create an empty linked list', () => {
12+
expect(linkedList.toString()).toBe('');
13+
});
14+
15+
test('should append node to linked list', () => {
16+
linkedList.append(1);
17+
linkedList.append(2);
18+
linkedList.append(3);
19+
expect(linkedList.toString()).toBe('1, 2, 3');
20+
});
21+
22+
test('should prepend node to linked list', () => {
23+
linkedList.prepend(2);
24+
expect(linkedList.toString()).toBe('2');
25+
linkedList.append(1);
26+
linkedList.prepend(3);
27+
expect(linkedList.toString()).toBe('3, 2, 1');
28+
});
29+
30+
test('should insert node at position 0', () => {
31+
linkedList.append(1);
32+
linkedList.insert(0, 2);
33+
expect(linkedList.toString()).toBe('2, 1');
34+
});
35+
36+
test('should insert node at given position', () => {
37+
linkedList.append(1);
38+
linkedList.append(3);
39+
linkedList.insert(1, 2);
40+
expect(linkedList.toString()).toBe('1, 2, 3');
41+
});
42+
43+
test('should insert node at invalid position', () => {
44+
linkedList.append(1);
45+
linkedList.append(3);
46+
expect(linkedList.insert(3, 2)).toBe(false);
47+
expect(linkedList.toString()).toBe('1, 3');
48+
});
49+
50+
test('should remove node from linked list', () => {
51+
linkedList.append(1);
52+
linkedList.append(2);
53+
linkedList.append(3);
54+
linkedList.removeAt(1);
55+
expect(linkedList.toString()).toBe('1, 3');
56+
});
57+
58+
test('should remove node at position 0', () => {
59+
linkedList.append(1);
60+
linkedList.append(2);
61+
linkedList.removeAt(0);
62+
expect(linkedList.toString()).toBe('2');
63+
});
64+
65+
test('should remove node at invalid position', () => {
66+
linkedList.append(1);
67+
linkedList.append(2);
68+
expect(() => linkedList.removeAt(2)).toThrowError('Invalid position');
69+
});
70+
71+
test('should remove element from linked list', () => {
72+
linkedList.append(1);
73+
linkedList.append(2);
74+
linkedList.append(3);
75+
linkedList.remove(2);
76+
expect(linkedList.toString()).toBe('1, 3');
77+
});
78+
79+
test('should remove element from linked list head', () => {
80+
linkedList.append(1);
81+
linkedList.append(2);
82+
linkedList.remove(1);
83+
expect(linkedList.toString()).toBe('2');
84+
});
85+
86+
test('should remove element from linked list tail', () => {
87+
linkedList.append(1);
88+
linkedList.append(2);
89+
linkedList.remove(2);
90+
expect(linkedList.toString()).toBe('1');
91+
});
92+
93+
test('should return null if element was not found', () => {
94+
linkedList.append(1);
95+
linkedList.append(2);
96+
expect(linkedList.remove(3)).toBeNull();
97+
});
98+
99+
test('should find element index in linked list', () => {
100+
linkedList.append(1);
101+
linkedList.append(2);
102+
linkedList.append(3);
103+
expect(linkedList.indexOf(1)).toBe(0);
104+
expect(linkedList.indexOf(2)).toBe(1);
105+
expect(linkedList.indexOf(3)).toBe(2);
106+
});
107+
108+
test('should return -1 when element was not found', () => {
109+
linkedList.append(1);
110+
linkedList.append(2);
111+
expect(linkedList.indexOf(3)).toBe(-1);
112+
});
113+
114+
test('should return true if list is empty', () => {
115+
expect(linkedList.isEmpty()).toBe(true);
116+
});
117+
118+
test('should return false if list is not empty', () => {
119+
linkedList.append(1);
120+
expect(linkedList.isEmpty()).toBe(false);
121+
});
122+
123+
test('should return the size of the linked list', () => {
124+
expect(linkedList.getSize()).toBe(0);
125+
linkedList.append(1);
126+
expect(linkedList.getSize()).toBe(1);
127+
linkedList.append(2);
128+
expect(linkedList.getSize()).toBe(2);
129+
});
130+
131+
test('should convert linked list to string', () => {
132+
linkedList.append(1);
133+
expect(linkedList.toString()).toBe('1');
134+
linkedList.append(2);
135+
expect(linkedList.toString()).toBe('1, 2');
136+
});
137+
138+
test('should convert linked list to string with custom stringifier', () => {
139+
const customList = new LinkedList<{value: number; key: string}>();
140+
customList.append({value: 1, key: 'key1'});
141+
expect(customList.toString()).toBe('{"value":1,"key":"key1"}');
142+
});
143+
144+
test('should reverse the linked list', () => {
145+
linkedList.append(1);
146+
linkedList.append(2);
147+
linkedList.append(3);
148+
linkedList.reverse();
149+
expect(linkedList.toString()).toBe('3, 2, 1');
150+
});
151+
152+
test('should reverse the linked list with one element', () => {
153+
linkedList.append(1);
154+
linkedList.reverse();
155+
expect(linkedList.toString()).toBe('1');
156+
});
157+
158+
test('should clear the linked list', () => {
159+
linkedList.append(1);
160+
linkedList.append(2);
161+
linkedList.append(3);
162+
linkedList.clear();
163+
expect(linkedList.toString()).toBe('');
164+
});
165+
});

0 commit comments

Comments
(0)

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