1
2/**
3 * @constructor SLL
4 *
5 * @param head The head of the list.
6 * @param klass The reference to the SLLNode class
7 * @param size the size or length of the list.
8 * @arguments <klass:SLLNode>
9 * @description The SLL Factory
10 *
11 */
12export function SLL(klass = Node) {
13 this.head = new Node();
14 this.klass = Node;
15 this.size = 0;
16}
17
18/**
19 * @function Add|Method
20 *
21 * @returns SLLNode
22 * @param node The node you will append
23 * @arguments <key:string>
24 * @description A method that will insert a new node at the beginning of the
25 * list.
26 */
27SLL.prototype.add = function insert(key) {
28 const node = new this.klass(key);
29 this.head = this.head.add(node);
30 this.size++;
31 return this;
32}
33
34/**
35 * @function Find|Method
36 *
37 * @returns SLLNode
38 * @param key The key of the node you wish to find.
39 * @arguments <key:string>
40 * @description A method that will retireve a node associated with the
41 * corrosponding key.
42 */
43SLL.prototype.find = function find(key) {
44 return this.head && this.head.find(key);
45}
46
47/**
48 * @function Insert|Method
49 *
50 * @returns SLLNode
51 * @param key The key of the node you wish to append a new node.
52 * @param key1 The key of the node you wish to insert
53 * @arguments <key:string>, <key1:string>
54 * @description A method that will insert a new node into the middle of the
55 * list
56 */
57SLL.prototype.insert = function insert(key, key1) {
58 const node = new this.klass(key1);
59 return this.head && this.head.insert(key, node);
60}
61
62/**
63 * @function Del|Method
64 *
65 * @returns SLLNode
66 * @param key The key of the node you wish to delete.
67 * @arguments <key:string>
68 * @description A method that will delete a node associated with the
69 * corrosponding key.
70 */
71SLL.prototype.del = function del(key) {
72 return this.head.del(key);
73}
74
75/**
76 * @returns SLLNode
77 * @param key The key of the node you wish to create.
78 * @arguments <key:null?string>
79 * @description The SLLNode Interface
80 *
81 * @interface ISLLNode
82 */
83function ISSLNode(key=null) {
84 this.key = key;
85 this.next = null;
86}
87
88/**
89 * @constructor SLLNode
90 *
91 * @param key The key of the node you wish to create.
92 * @arguments <key:null?string>
93 * @description The SLLNode Factory
94 *
95 * @implements ISLLNode
96 */
97export function SLLNode(key=null) {
98 ISSLNode.call(this, key=null);
99}
100
101/**
102 * @function Add|Method
103 *
104 * @returns SLLNode
105 * @param node The node you will append
106 * @arguments <node:SLLNode>
107 * @description A method that will insert a new node at the beginning of the
108 * list.
109 */
110SLLNode.prototype.add = function add(node) {
111 if(!this.key && !this.next) {
112 this.key = node.key;
113 this.next = node.next;
114 return node;
115 }
116 node.next = this;
117 return node;
118}
119
120/**
121 * @function Find|Method
122 *
123 * @returns SLLNode
124 * @param key The key of the node you wish to find.
125 * @arguments <key:string>
126 * @description A method that will retireve a node associated with the
127 * corrosponding key.
128 */
129SLLNode.prototype.find = function find(key) {
130 if (this.key === key) {
131 this.next = null;
132 return this;
133 }
134 if (this.key !== key) {
135 if(!this.next) {
136 return 0;
137 }
138 return this.next.find(key);
139 }
140}
141
142/**
143 * @function Insert|Method
144 *
145 * @returns SLLNode
146 * @param key The key of the node you wish to append a new node.
147 * @param node The node you will append
148 * @arguments <key:string>, <node:SLLNode>
149 * @description A method that will insert a new node into the middle of the
150 * list
151 */
152SLLNode.prototype.insert = function insert(key, node) {
153 if (this.key === key) {
154 let tmp = this.next;
155 this.next = node;
156 node.next = tmp;
157 return node;
158 }
159 if (this.key !== key) {
160 if (!this.next) {
161 return 0;
162 }
163 return this.next.insert(key, node);
164 }
165}
166
167/**
168 * @function Del|Method
169 *
170 * @returns SLLNode
171 * @param key The key of the node you wish to delete.
172 * @param pre
173 * @arguments <key:string>, <pre:null?SLLNode>
174 * @description A method that will delete a node associated with the
175 * corrosponding key.
176 */
177SLLNode.prototype.del = function del(key, pre=null) {
178 if (this.key === key) {
179 let tmp = this.next;
180 pre.next = tmp;
181 return this;
182 }
183 if (this.key !== key) {
184 if (!this.next) {
185 return 0;
186 }
187 return this.next.del(key, this);
188 }
189}