MCPcopy Create free account
hub / github.com/TodePond/ScreenPond / LinkedList

Class LinkedList

source/list.js:4–78  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2// LIST //
3//======//
4export class LinkedList {
5 constructor(iterable = []) {
6 this.start = undefined;
7 this.end = undefined;
8 this.isEmpty = true;
9
10 for (const item of iterable) {
11 this.push(item);
12 }
13 }
14
15 *[Symbol.iterator]() {
16 let link = this.start;
17 while (link !== undefined) {
18 yield link;
19 link = link.next;
20 }
21 }
22
23 push(item) {
24 const link = makeLink(item);
25 if (this.isEmpty) {
26 this.start = link;
27 this.end = link;
28 this.isEmpty = false;
29 } else {
30 this.end.next = link;
31 link.previous = this.end;
32 this.end = link;
33 }
34 }
35
36 pop() {
37 if (this.isEmpty) {
38 return undefined;
39 }
40
41 const item = this.start.item;
42 if (this.start === this.end) {
43 this.clear();
44 return item;
45 }
46
47 this.end = this.end.previous;
48 this.end.next = undefined;
49 return item;
50 }
51
52 shift() {
53 if (this.isEmpty) {
54 return undefined;
55 }
56
57 const item = this.start.item;
58 if (this.start === this.end) {
59 this.clear();
60 return item;
61 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected