Skip to content

Commit d7ae7e3

Browse files
committed
refactored from ES6 classes to factory function to Linked Lists types Data Strucutre
1 parent 21655c3 commit d7ae7e3

File tree

2 files changed

+36
-44
lines changed

2 files changed

+36
-44
lines changed

06-chapter-Linked-Lists-types/linked.proto.module.js

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
const linked = (type) => {
2020

2121
const link = {}
22-
// private variables
22+
// common private variables linked lists data structures
2323
let head = null
2424
let current = null
2525
let length = 0
@@ -82,21 +82,12 @@
8282
return false
8383
}
8484

85+
// functions for linked list
8586
const add = (data) => {
8687
let args = (!head && !current) ? [setHead] : [setNext(current)]
8788
compose(...args, setCurrent, setLength)(node(data))
8889
}
8990

90-
const add2 = (data) => {
91-
let args = (!head && !current) ? [setHead] : [setNext(current), setPrev(current)]
92-
compose(...args, setCurrent, setLength)(node(data))
93-
}
94-
95-
const add3 = (data) => {
96-
let args = (!head && !current) ? [setHead2] : [setNext2(current), setPrev2(current)]
97-
compose(...args, setCurrent, setLength)(node(data))
98-
}
99-
10091
const remove = (data) => {
10192
let prev = findPrev(data)
10293
if (!(prev.next === null)) {
@@ -105,6 +96,22 @@
10596
setLength(false)
10697
}
10798

99+
const display = () => {
100+
let c = head
101+
let show = ''
102+
while (!(c === null)) {
103+
show += `${c.data} ${(c.next !== null) ? ' ->' : ''} `
104+
c = c.next
105+
}
106+
return show
107+
}
108+
109+
// functions for double linked list
110+
const add2 = (data) => {
111+
let args = (!head && !current) ? [setHead] : [setNext(current), setPrev(current)]
112+
compose(...args, setCurrent, setLength)(node(data))
113+
}
114+
108115
const remove2 = (data) => {
109116
let prev = findPrev(data)
110117
if (!(prev.next === null)) {
@@ -113,6 +120,12 @@
113120
setLength(false)
114121
}
115122

123+
// functions for cirle linked list
124+
const add3 = (data) => {
125+
let args = (!head && !current) ? [setHead2] : [setNext2(current), setPrev2(current)]
126+
compose(...args, setCurrent, setLength)(node(data))
127+
}
128+
116129
const remove3 = (data) => {
117130
let prev = findPrev(data)
118131
if (!(prev.next === null)) {
@@ -121,27 +134,6 @@
121134
setLength(false)
122135
}
123136

124-
link.contains = (data) => {
125-
let c = head
126-
while (!(c === null)) {
127-
if (c.data === data) {
128-
return true
129-
}
130-
c = c.next
131-
}
132-
return false
133-
}
134-
135-
const display = () => {
136-
let c = head
137-
let show = ''
138-
while (!(c === null)) {
139-
show += `${c.data} ${(c.next !== null) ? ' ->' : ''} `
140-
c = c.next
141-
}
142-
return show
143-
}
144-
145137
const display2 = () => {
146138
let c = head
147139
let show = `${c.data} -> `
@@ -152,6 +144,17 @@
152144
return show
153145
}
154146

147+
// Common functions for linked list's
148+
link.contains = (data) => {
149+
let c = head
150+
while (!(c === null)) {
151+
if (c.data === data) {
152+
return true
153+
}
154+
c = c.next
155+
}
156+
return false
157+
}
155158
link.getCurrent = () => current
156159
link.getList = () => head
157160
link.size = () => length

06-chapter-Linked-Lists-types/readme.md

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
### Code Examples
44

5-
- [Linked List Data Structure]('./linked-list.module')
6-
- [Double Linked List Data Structure]('./double-linked-list.module')
7-
- [Circular Linked List Data Structure]('./circular-linked-list.module')
5+
- [Single, Double, Circular, Linked List, Data Structure]('./linked.proto.module')
86
- [Import complete module]('./index')
97

108
### Definition Linked List
@@ -15,11 +13,8 @@ common operations you can perform on linked list:
1513

1614
**Additions**
1715
- `add`: grow the list by adding items to the end of the list.
18-
- `addHead`: Similar to our add method above, we always know the position of the head, so no iteration necessary.
19-
- `insertAfter`: have to iterate over the entire list to find the target node that your value should be inserted after.
2016
**Removals**
2117
- `remove`: will always remove from a given position in the list.
22-
2318
**Search**
2419
- `contains`: will search the list for a value.
2520

@@ -39,18 +34,12 @@ common operations you can perform on linked list:
3934

4035
**Additions**
4136
- `add`: grow the list by adding items to the end of the list.
42-
- `addHead`: Similar to our add method above, we always know the position of the head, so no iteration necessary.
43-
- `insertAfter`: ave to iterate over the entire list to find the target node that your value should be inserted after.
44-
- `insertLast`: adds an element at the end of the list.
4537
**Removals**
4638
- `remove`: will always remove from a given position in the list.
47-
- `removeLast`: deletes an element from the end of the list.
4839
**Search**
4940
- `contains`: will search the list for a value.
5041
**Display**
5142
- `display`: displays the complete list in a forward manner.
52-
- `displayReverse`: displays the complete list in a backward manner.
53-
5443

5544
### Definition Circular Linked List
5645

0 commit comments

Comments
 (0)