Skip to content

Commit 668792f

Browse files
committed
refactored from ES6 classes to ES6 factory function to Linked List Data Strucutre
1 parent 99bd54c commit 668792f

File tree

5 files changed

+253
-0
lines changed

5 files changed

+253
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Node Linked List member class example
2+
class Node {
3+
constructor(element = 'Head', next = null) {
4+
this.element = element;
5+
this.next = next;
6+
}
7+
}
8+
9+
// Linked List data structure class example
10+
class LList {
11+
constructor(head = new Node()) {
12+
this.head = head;
13+
this.current = this.head;
14+
}
15+
16+
find(item) {
17+
let currNode = this.head;
18+
while (currNode.element != item) {
19+
currNode = currNode.next;
20+
}
21+
return currNode;
22+
}
23+
24+
insert(newElement, item){
25+
const newNode = new Node(newElement);
26+
const current = this.find(item);
27+
newNode.next = current.next;
28+
current.next = newNode;
29+
}
30+
31+
findPrevious(item) {
32+
let currNode = this.head;
33+
while (!(currNode.next === null) && currNode.next.element != item) {
34+
currNode = currNode.next;
35+
}
36+
return currNode;
37+
}
38+
39+
remove(item) {
40+
let prevNode = this.findPrevious(item);
41+
if(!(prevNode.next === null)) {
42+
prevNode.next = prevNode.next.next;
43+
}
44+
}
45+
46+
display() {
47+
let currNode = this.head;
48+
while (!(currNode.next === null)) {
49+
console.log(currNode.next.element);
50+
currNode = currNode.next;
51+
}
52+
}
53+
54+
advance(n) {
55+
let position = n;
56+
while(!(this.current.next === null)) {
57+
if(n-- > 0) {
58+
this.current = this.current.next;
59+
} else if (this.current.next === null) {
60+
console.log(`Can't advanced ${position} position in the list`);
61+
break;
62+
} else {
63+
break;
64+
}
65+
}
66+
}
67+
68+
show() {
69+
console.log(this.current.element);
70+
}
71+
72+
}
73+
74+
// Implementation
75+
// ##########################################
76+
/*
77+
1. Implement the advance(n) function so that when executed, the current node is moved n nodes forward in the list.
78+
*/
79+
console.log('CHAPTER 6');
80+
console.log('### Excercise 1');
81+
82+
const cities = new LList();
83+
cities.insert("Conway", "Head");
84+
cities.insert("Russellville", "Conway");
85+
cities.insert("Carlisle", "Russellville");
86+
cities.insert("Alma", "Carlisle");
87+
cities.display();
88+
console.log('remove; Carlisle\n');
89+
cities.remove("Carlisle");
90+
91+
console.log('Linked List Elements:');
92+
cities.display();
93+
94+
console.log('\nAdvance 3 positions');
95+
cities.advance(3);
96+
console.log('Display current Node');
97+
cities.show();

06-chapter-Linked-List/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
(function (exports) {
2+
const {linkedList} = require('./linked-list.module')
3+
// const {doubleLinked} = require('./double-linked-list.module')
4+
// const {circularLinked} = require('./circular-linked-list.module')
5+
6+
Object.assign(exports, {linkedList})
7+
}((typeof module.exports !== 'undefined') ? module.exports : window))
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
(function (exports) {
2+
const node = (element = 'Head', next = null) => {
3+
const o = {
4+
element,
5+
next
6+
}
7+
return Object.assign(Object.create(o), {element, next})
8+
}
9+
10+
const linkedList = (h = null, c = null, l = 0) => {
11+
const list = {}
12+
let head = h
13+
let current = c
14+
let length = l
15+
16+
const findPrevious = (item) => {
17+
let currNode = getHead()
18+
while (!(currNode.next === null) && currNode.next.element !== item) {
19+
currNode = currNode.next
20+
}
21+
return currNode
22+
}
23+
24+
const getHead = () => head
25+
26+
list.contains = (item) => {
27+
let node = getHead()
28+
while (node) {
29+
if (node.element === item) {
30+
return true
31+
}
32+
node = node.next
33+
}
34+
return false
35+
}
36+
37+
list.add = (element) => {
38+
const newNode = node(element)
39+
if (!head && !current) {
40+
head = newNode
41+
current = head
42+
} else {
43+
current.next = newNode
44+
current = current.next
45+
}
46+
length += 1
47+
}
48+
49+
list.remove = (item) => {
50+
let prevNode = findPrevious(item)
51+
if (!(prevNode.next === null)) {
52+
prevNode.next = prevNode.next.next
53+
current = prevNode.next
54+
}
55+
length -= 1
56+
}
57+
58+
list.display = () => {
59+
let currNode = head
60+
let show = currNode.element + ' -> '
61+
while (!(currNode.next === null)) {
62+
show += currNode.next.element + ' -> '
63+
currNode = currNode.next
64+
}
65+
show += 'end'
66+
console.log(show)
67+
}
68+
69+
list.advance = (n) => {
70+
let position = n
71+
while (!(current.next === null)) {
72+
if (n-- > 0) {
73+
current = current.next
74+
} else if (current.next === null) {
75+
console.log(`Can't advanced ${position} position in the list`)
76+
break
77+
} else {
78+
break
79+
}
80+
}
81+
}
82+
83+
list.show = () => current.element
84+
85+
list.size = () => length
86+
87+
return Object.create(list)
88+
}
89+
90+
Object.assign(exports, {linkedList})
91+
}((typeof module.exports !== 'undefined') ? module.exports : window))

06-chapter-Linked-List/readme.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Linked List, Double Linked List, Circular Linked List
2+
3+
### Code Examples
4+
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')
8+
- [Import complete module]('./index')
9+
10+
### Definition Linked List
11+
12+
The simplest form of linked lists — a singly linked list — is a series of nodes where each individual node contains both a value and a pointer to the next node in the list.
13+
14+
common operations you can perform on graphs:
15+
16+
**Additions**
17+
- `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.
20+
**Removals**
21+
- `remove`: will always remove from a given position in the list.
22+
23+
**Search**
24+
- `contains`: will search the list for a value.
25+
26+
### Example use cases
27+
- Storing values in a hash table to prevent collisions
28+
29+
### Resources
30+
31+
- [A Gentle Introduction to Data Structures: How Linked Lists Work](https://medium.freecodecamp.com/a-gentle-introduction-to-data-structures-how-linked-lists-work-5adc793897dd#.ti1dvnsog)

06-chapter-Linked-List/test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const {linkedList} = require('./index')
2+
3+
test('Linked List test', assert => {
4+
const amazingRace = linkedList()
5+
6+
amazingRace.add('Colombo, Sri Lanka')
7+
8+
assert.equal(amazingRace.show(), 'Colombo, Sri Lanka', 'can add an element')
9+
10+
amazingRace.add('Lagos, Nigeria')
11+
amazingRace.add('Surat, India')
12+
amazingRace.add('Suzhou, China')
13+
14+
assert.equal(4, amazingRace.size(), 'can get the size of the linked list')
15+
console.log(`contains 'Suzhou, China' ? ${amazingRace.contains('Suzhou, China')}`) // true
16+
console.log(`contains 'Hanoi, Vietnam' ? ${amazingRace.contains('Hanoi, Vietnam')}`) // true
17+
amazingRace.add('Hanoi, Vietnam')
18+
console.log(`contains 'Seattle, Washington' ? ${amazingRace.contains('Seattle, Washington')}`) // true
19+
amazingRace.add('Seattle, Washington')
20+
console.log(`contains 'North Pole' ? ${amazingRace.contains('North Pole')}`) // true
21+
amazingRace.add('North Pole')
22+
23+
amazingRace.remove('North Pole')
24+
amazingRace.remove('Hanoi, Vietnam')
25+
26+
assert.equal(amazingRace.size(), 5, 'can remove elements')
27+
})

0 commit comments

Comments
 (0)