UNPKG

5.39 kBJavaScriptView Raw
1import { Collection } from "../Collection";
2import CycleLinkList from "../cyclelinklist/CycleLinkList";
3import DoubleLinkList from "../doublelinklist/DoubleLinkList";
4import { LinkNode } from "./LinkNode";
5export class LinkList extends Collection {
6 constructor() {
7 super();
8 this.headNode = null;
9 this.tailNode = null;
10 this.size = 0;
11 }
12 get Size() {
13 return this.size;
14 }
15 append(value) {
16 this.size++;
17 if (!this.headNode) {
18 this.headNode = this.tailNode = new LinkNode(value);
19 return this.headNode;
20 }
21 if (this.headNode === this.tailNode) {
22 this.tailNode = new LinkNode(value);
23 this.headNode.setNext(this.tailNode);
24 return this.headNode;
25 }
26 const tailNode = new LinkNode(value);
27 this.tailNode.setNext(tailNode);
28 this.tailNode = tailNode;
29 return this.headNode;
30 }
31 prepend(value) {
32 if (!this.headNode) {
33 this.headNode = this.tailNode = new LinkNode(value);
34 }
35 else {
36 this.headNode = new LinkNode(value, this.headNode);
37 }
38 this.size++;
39 return this.headNode;
40 }
41 emptyList() {
42 this.headNode = this.tailNode = null;
43 this.size = 0;
44 }
45 clear() {
46 this.emptyList();
47 }
48 deleteNode(arg) {
49 let temp = this.headNode;
50 let result = false;
51 let prevNode;
52 while (temp) {
53 const match = typeof arg === "function" ? arg(temp.Value) : (temp.Value === arg);
54 if (match) {
55 this.size--;
56 result = true;
57 if (temp === this.headNode) {
58 this.headNode = temp.Next;
59 }
60 else if (temp === this.tailNode) {
61 prevNode.setNext(null);
62 this.tailNode = prevNode;
63 }
64 else {
65 prevNode.setNext(temp.Next);
66 }
67 }
68 if (temp.Next && temp.Next === this.headNode)
69 break;
70 if (this.size === 0) {
71 this.emptyList();
72 break;
73 }
74 prevNode = temp;
75 temp = temp.Next;
76 }
77 return result;
78 }
79 findNode(arg) {
80 let temp = this.headNode;
81 let result;
82 while (temp) {
83 const match = typeof arg === "function" ? arg(temp.Value) : (temp.Value === arg);
84 if (match) {
85 result = temp;
86 break;
87 }
88 temp = temp.Next;
89 }
90 return result;
91 }
92 insertAfter(value, oriNode) {
93 const newNode = new LinkNode(value);
94 if (oriNode) {
95 const nextNode = oriNode.Next;
96 if (!nextNode || nextNode === this.headNode) {
97 this.tailNode = newNode;
98 }
99 newNode.setNext(nextNode);
100 oriNode.setNext(newNode);
101 this.size++;
102 return true;
103 }
104 return false;
105 }
106 getHeadNode() {
107 return this.headNode;
108 }
109 getTailNode() {
110 return this.tailNode;
111 }
112 shift() {
113 if (this.size === 0) {
114 return null;
115 }
116 else if (this.size === 1) {
117 this.tailNode = null;
118 }
119 const temp = this.headNode;
120 this.headNode = temp.Next;
121 this.size--;
122 return temp;
123 }
124 pop() {
125 let temp = this.headNode;
126 let result;
127 let prevNode;
128 if (this.size === 0) {
129 return null;
130 }
131 if (this.size === 1) {
132 result = this.headNode;
133 this.emptyList();
134 return result;
135 }
136 while (temp) {
137 const nextNode = temp.Next;
138 if (!nextNode || nextNode === this.headNode) {
139 result = temp;
140 this.tailNode = prevNode;
141 prevNode.setNext(nextNode);
142 break;
143 }
144 prevNode = temp;
145 temp = nextNode;
146 }
147 this.size--;
148 return result;
149 }
150 __iterate(fn) {
151 let temp = this.headNode, index = 0;
152 while (temp) {
153 fn(temp, index);
154 index++;
155 const nextNode = temp.Next;
156 if (!nextNode || nextNode === this.headNode) {
157 break;
158 }
159 temp = nextNode;
160 }
161 }
162 toString() {
163 return this.toArray().map(node => node.toString()).toString();
164 }
165 static fromArray(arr) {
166 if (!arr) {
167 return new LinkList();
168 }
169 const linkList = new LinkList();
170 arr.forEach(item => {
171 linkList.append(item);
172 });
173 return linkList;
174 }
175 toDoubleLinkList() {
176 if (!this.headNode) {
177 return new DoubleLinkList();
178 }
179 const arr = this.toArray();
180 const doubleListList = new DoubleLinkList();
181 arr.forEach(item => {
182 doubleListList.append(item.Value);
183 });
184 return doubleListList;
185 }
186 toCycleLinkList() {
187 const cyclelinklist = new CycleLinkList();
188 this.toArray().forEach((node) => {
189 cyclelinklist.append(node.Value);
190 });
191 return cyclelinklist;
192 }
193}
194export default LinkList;