|
19 | 19 | const linked = (type) => {
|
20 | 20 |
|
21 | 21 | const link = {}
|
22 |
| - // private variables |
| 22 | + // common private variables linked lists data structures |
23 | 23 | let head = null
|
24 | 24 | let current = null
|
25 | 25 | let length = 0
|
|
82 | 82 | return false
|
83 | 83 | }
|
84 | 84 |
|
| 85 | + // functions for linked list |
85 | 86 | const add = (data) => {
|
86 | 87 | let args = (!head && !current) ? [setHead] : [setNext(current)]
|
87 | 88 | compose(...args, setCurrent, setLength)(node(data))
|
88 | 89 | }
|
89 | 90 |
|
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 |
| - |
100 | 91 | const remove = (data) => {
|
101 | 92 | let prev = findPrev(data)
|
102 | 93 | if (!(prev.next === null)) {
|
|
105 | 96 | setLength(false)
|
106 | 97 | }
|
107 | 98 |
|
| 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 | + |
108 | 115 | const remove2 = (data) => {
|
109 | 116 | let prev = findPrev(data)
|
110 | 117 | if (!(prev.next === null)) {
|
|
113 | 120 | setLength(false)
|
114 | 121 | }
|
115 | 122 |
|
| 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 | + |
116 | 129 | const remove3 = (data) => {
|
117 | 130 | let prev = findPrev(data)
|
118 | 131 | if (!(prev.next === null)) {
|
|
121 | 134 | setLength(false)
|
122 | 135 | }
|
123 | 136 |
|
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 |
| - |
145 | 137 | const display2 = () => {
|
146 | 138 | let c = head
|
147 | 139 | let show = `${c.data} -> `
|
|
152 | 144 | return show
|
153 | 145 | }
|
154 | 146 |
|
| 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 | + } |
155 | 158 | link.getCurrent = () => current
|
156 | 159 | link.getList = () => head
|
157 | 160 | link.size = () => length
|
|
0 commit comments