1 | import { LinkList } from "../linklist/LinkList";
|
2 | export class CycleLinkList {
|
3 | constructor() {
|
4 | this.linklist = new LinkList();
|
5 | }
|
6 | setCircle() {
|
7 | this.getTailNode().setNext(this.getHeadNode());
|
8 | }
|
9 | get Size() {
|
10 | return this.linklist.Size;
|
11 | }
|
12 | append(value) {
|
13 | const result = this.linklist.append(value);
|
14 | this.setCircle();
|
15 | return result;
|
16 | }
|
17 | prepend(value) {
|
18 | const result = this.linklist.prepend(value);
|
19 | this.setCircle();
|
20 | return result;
|
21 | }
|
22 | deleteNode(arg) {
|
23 | const isFirstOrLast = this.linklist.findNode(arg) === this.getHeadNode()
|
24 | || this.linklist.findNode(arg) === this.getTailNode(), result = this.linklist.deleteNode(arg);
|
25 | if (isFirstOrLast) {
|
26 | this.setCircle();
|
27 | }
|
28 | return result;
|
29 | }
|
30 | findNode(arg) {
|
31 | return this.linklist.findNode(arg);
|
32 | }
|
33 | getHeadNode() {
|
34 | return this.linklist.getHeadNode();
|
35 | }
|
36 | getTailNode() {
|
37 | return this.linklist.getTailNode();
|
38 | }
|
39 | shift() {
|
40 | const result = this.linklist.shift();
|
41 | if (this.Size) {
|
42 | this.setCircle();
|
43 | }
|
44 | return result;
|
45 | }
|
46 | pop() {
|
47 | const result = this.linklist.pop();
|
48 | if (this.Size) {
|
49 | this.setCircle();
|
50 | }
|
51 | return result;
|
52 | }
|
53 | insertAfter(value, oriNode) {
|
54 | return this.linklist.insertAfter(value, oriNode);
|
55 | }
|
56 | clear() {
|
57 | this.linklist.clear();
|
58 | }
|
59 | toString() {
|
60 | return this.linklist.toString();
|
61 | }
|
62 | static fromArray(arr) {
|
63 | if (!arr) {
|
64 | return new CycleLinkList();
|
65 | }
|
66 | const linkList = new CycleLinkList();
|
67 | arr.forEach(item => {
|
68 | linkList.append(item);
|
69 | });
|
70 | return linkList;
|
71 | }
|
72 | toArray() {
|
73 | return this.linklist.toArray();
|
74 | }
|
75 | getEnumerator() {
|
76 | let temp = this.getHeadNode();
|
77 | const enumerator = {
|
78 | next: () => {
|
79 | temp = temp.Next;
|
80 | enumerator.Current = {
|
81 | value: temp.Value,
|
82 | done: false,
|
83 | };
|
84 | return enumerator;
|
85 | },
|
86 | Current: {
|
87 | value: temp.Value,
|
88 | done: false,
|
89 | },
|
90 | };
|
91 | return enumerator;
|
92 | }
|
93 | }
|
94 | export default CycleLinkList;
|