Skip to content

Commit b483f08

Browse files
authored
Add simple-linked-list (#334)
1 parent c7939d6 commit b483f08

File tree

7 files changed

+224
-0
lines changed

7 files changed

+224
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,14 @@
698698
"practices": [],
699699
"prerequisites": [],
700700
"difficulty": 8
701+
},
702+
{
703+
"slug": "simple-linked-list",
704+
"name": "Simple Linked List",
705+
"uuid": "1c96f455-f1d5-4aa1-950c-6e60fc2b09ae",
706+
"practices": [],
707+
"prerequisites": [],
708+
"difficulty": 8
701709
}
702710
]
703711
},
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Instructions
2+
3+
Write a prototype of the music player application.
4+
5+
For the prototype, each song will simply be represented by a number.
6+
Given a range of numbers (the song IDs), create a singly linked list.
7+
8+
Given a singly linked list, you should be able to reverse the list to play the songs in the opposite order.
9+
10+
~~~~exercism/note
11+
The linked list is a fundamental data structure in computer science, often used in the implementation of other data structures.
12+
13+
The simplest kind of linked list is a **singly** linked list.
14+
That means that each element (or "node") contains data, along with something that points to the next node in the list.
15+
16+
If you want to dig deeper into linked lists, check out [this article][intro-linked-list] that explains it using nice drawings.
17+
18+
[intro-linked-list]: https://medium.com/basecs/whats-a-linked-list-anyway-part-1-d8b7e6508b9d
19+
~~~~
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Introduction
2+
3+
You work for a music streaming company.
4+
5+
You've been tasked with creating a playlist feature for your music player application.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"simple-linked-list.coffee"
8+
],
9+
"test": [
10+
"simple-linked-list.spec.coffee"
11+
],
12+
"example": [
13+
".meta/example.coffee"
14+
]
15+
},
16+
"blurb": "Write a simple linked list implementation that uses Elements and a List.",
17+
"source": "Inspired by 'Data Structures and Algorithms with Object-Oriented Design Patterns in Ruby', singly linked-lists.",
18+
"source_url": "https://web.archive.org/web/20160731005714/http://brpreiss.com/books/opus8/html/page96.html"
19+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Element
2+
constructor: (@value, @next = null) ->
3+
4+
class LinkedList
5+
constructor: (values) ->
6+
@count = 0
7+
@head = null
8+
if values?
9+
for value in values
10+
@add new Element value
11+
12+
length: ->
13+
@count
14+
15+
add: (element) ->
16+
elt = element
17+
elt.next = @head
18+
@head = elt
19+
@count += 1
20+
21+
toArray: ->
22+
elements = []
23+
element = @head
24+
while element?
25+
elements.push element.value
26+
element = element.next
27+
elements
28+
29+
reverse: (prev = null) ->
30+
if @head?.next
31+
current = @head
32+
@head = @head.next
33+
current.next = prev
34+
@reverse current
35+
else
36+
@head.next = prev
37+
@
38+
39+
module.exports.Element = Element
40+
module.exports.LinkedList = LinkedList
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Element
2+
constructor: (value) ->
3+
4+
class LinkedList
5+
constructor: (values) ->
6+
7+
length: ->
8+
9+
add: (element) ->
10+
11+
toArray: ->
12+
13+
reverse: ->
14+
15+
module.exports.Element = Element
16+
module.exports.LinkedList = LinkedList
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
{ Element, LinkedList } = require './simple-linked-list'
2+
3+
describe 'Simple Linked List', ->
4+
describe 'Element class', ->
5+
xit 'has constructor', ->
6+
element = new Element 1
7+
expect(element.value).toEqual 1
8+
9+
xit 'value reflects constructor arg', ->
10+
element = new Element 2
11+
expect(element.value).toEqual 2
12+
13+
xit 'has null for next by default', ->
14+
element = new Element 1
15+
expect(element.next).toEqual null
16+
17+
describe 'List class', ->
18+
xit 'has constructor', ->
19+
list = new LinkedList
20+
expect(list).toBeDefined()
21+
22+
xit 'new lists should have length 0', ->
23+
list = new LinkedList
24+
expect(list.length()).toEqual 0
25+
26+
xit 'can add a element', ->
27+
list = new LinkedList
28+
element = new Element 1
29+
expect -> list.add element
30+
.not.toThrow()
31+
32+
xit 'adding a element increments length', ->
33+
list = new LinkedList
34+
element = new Element 1
35+
list.add element
36+
expect(list.length()).toEqual 1
37+
38+
xit 'adding two elements increments twice', ->
39+
list = new LinkedList
40+
element1 = new Element 1
41+
element2 = new Element 3
42+
list.add element1
43+
list.add element2
44+
expect(list.length()).toEqual 2
45+
46+
xit 'new Lists have a null head element', ->
47+
list = new LinkedList
48+
expect(list.head).toEqual null
49+
50+
xit 'adding an Element to an empty list sets the head Element', ->
51+
list = new LinkedList
52+
element = new Element 1
53+
list.add element
54+
expect(list.head.value).toEqual 1
55+
56+
xit 'adding a second Element updates the head Element', ->
57+
list = new LinkedList
58+
element1 = new Element 1
59+
element2 = new Element 3
60+
list.add element1
61+
list.add element2
62+
expect(list.head.value).toEqual 3
63+
64+
xit 'can get the next Element from the head', ->
65+
list = new LinkedList
66+
element1 = new Element 1
67+
element2 = new Element 3
68+
list.add element1
69+
list.add element2
70+
expect(list.head.next.value).toEqual 1
71+
72+
xit 'can be inxitialized wxith an array', ->
73+
list = new LinkedList [1, 2, 3]
74+
expect(list.length()).toEqual 3
75+
expect(list.head.value).toEqual 3
76+
77+
describe 'Lists wxith multiple elements', ->
78+
79+
xit 'wxith correct length', ->
80+
list = new LinkedList [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
81+
expect(list.length()).toEqual 10
82+
83+
xit 'wxith correct head value', ->
84+
list = new LinkedList [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
85+
expect(list.head.value).toEqual 10
86+
87+
xit 'can traverse the list', ->
88+
list = new LinkedList [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
89+
expect(list.head.next.next.next.value).toEqual 7
90+
91+
xit 'can convert to an array', ->
92+
list = new LinkedList [1]
93+
expect(list.toArray()).toEqual [1]
94+
95+
xit 'head of list is final element from input array', ->
96+
list = new LinkedList [1, 2]
97+
expect(list.head.value).toEqual 2
98+
99+
xit 'can convert longer list to an array', ->
100+
list = new LinkedList [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
101+
expect(list.toArray()).toEqual [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
102+
103+
xit 'can be reversed', ->
104+
list = new LinkedList [1, 2]
105+
expect(list.reverse().toArray()).toEqual [1, 2]
106+
107+
xit 'can be reversed when xit has more elements', ->
108+
list = new LinkedList [1, 2, 3]
109+
expect(list.reverse().toArray()).toEqual [1, 2, 3]
110+
111+
xit 'can reverse with many elements', ->
112+
list = new LinkedList [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
113+
expect(list.reverse().toArray()).toEqual [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
114+
115+
xit 'can reverse a reversal', ->
116+
list = new LinkedList [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
117+
expect(list.reverse().reverse().toArray()).toEqual [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

0 commit comments

Comments
 (0)