UNPKG

4.8 kBJavaScriptView Raw
1import { Collection } from "../Collection";
2import { DoubleLinkNode } from "./DoubleLinkNode";
3export class DoubleLinkList extends Collection {
4 constructor() {
5 super();
6 this.headNode = null;
7 this.tailNode = null;
8 this.size = 0;
9 }
10 get Size() {
11 return this.size;
12 }
13 append(value) {
14 this.size++;
15 if (!this.headNode) {
16 this.headNode = this.tailNode = new DoubleLinkNode(value, null, null);
17 return this.headNode;
18 }
19 if (this.headNode === this.tailNode) {
20 this.tailNode = new DoubleLinkNode(value);
21 this.headNode.setNext(this.tailNode);
22 return this.headNode;
23 }
24 const tailNode = new DoubleLinkNode(value);
25 this.tailNode.setNext(tailNode);
26 this.tailNode = tailNode;
27 return this.headNode;
28 }
29 prepend(value) {
30 if (!this.headNode) {
31 this.headNode = this.tailNode = new DoubleLinkNode(value);
32 }
33 else {
34 const headNode = this.headNode;
35 this.headNode = new DoubleLinkNode(value);
36 this.headNode.setNext(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 DoubleLinkNode(value);
94 if (oriNode) {
95 const nextNode = oriNode.Next;
96 if (!nextNode) {
97 this.tailNode = newNode;
98 }
99 newNode.setNext(nextNode);
100 oriNode.setNext(newNode);
101 return true;
102 }
103 return false;
104 }
105 getHeadNode() {
106 return this.headNode;
107 }
108 getTailNode() {
109 return this.tailNode;
110 }
111 shift() {
112 if (this.size === 0) {
113 return null;
114 }
115 else if (this.size === 1) {
116 this.tailNode = null;
117 }
118 const temp = this.headNode;
119 this.headNode = temp.Next;
120 this.size--;
121 return temp;
122 }
123 pop() {
124 let temp = this.headNode;
125 let result;
126 let prevNode;
127 if (this.size === 0) {
128 return null;
129 }
130 else if (this.size === 1) {
131 result = this.headNode;
132 this.emptyList();
133 }
134 else {
135 while (temp) {
136 if (!temp.Next) {
137 result = temp;
138 this.tailNode = prevNode;
139 prevNode.setNext(null);
140 break;
141 }
142 prevNode = temp;
143 temp = temp.Next;
144 }
145 this.size--;
146 }
147 return result;
148 }
149 __iterate(fn) {
150 let temp = this.headNode, index = 0;
151 while (temp) {
152 fn(temp, index);
153 index++;
154 const nextNode = temp.Next;
155 if (!nextNode || nextNode === this.headNode)
156 break;
157 temp = nextNode;
158 }
159 }
160 toString() {
161 return this.toArray().map(node => node.toString()).toString();
162 }
163 static fromArray(arr) {
164 if (!arr) {
165 return new DoubleLinkList();
166 }
167 const linkList = new DoubleLinkList();
168 arr.forEach(item => {
169 linkList.append(item);
170 });
171 return linkList;
172 }
173}
174export default DoubleLinkList;