-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
exercises/practice/simple-linked-list/.docs/instructions.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Instructions | ||
|
||
Write a prototype of the music player application. | ||
|
||
For the prototype, each song will simply be represented by a number. | ||
Given a range of numbers (the song IDs), create a singly linked list. | ||
|
||
Given a singly linked list, you should be able to reverse the list to play the songs in the opposite order. | ||
|
||
~~~~exercism/note | ||
The linked list is a fundamental data structure in computer science, often used in the implementation of other data structures. | ||
The simplest kind of linked list is a **singly** linked list. | ||
That means that each element (or "node") contains data, along with something that points to the next node in the list. | ||
If you want to dig deeper into linked lists, check out [this article][intro-linked-list] that explains it using nice drawings. | ||
[intro-linked-list]: https://medium.com/basecs/whats-a-linked-list-anyway-part-1-d8b7e6508b9d | ||
~~~~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Introduction | ||
|
||
You work for a music streaming company. | ||
|
||
You've been tasked with creating a playlist feature for your music player application. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"authors": [ | ||
"BNAndras" | ||
], | ||
"files": { | ||
"solution": [ | ||
"simple-linked-list.coffee" | ||
], | ||
"test": [ | ||
"simple-linked-list.spec.coffee" | ||
], | ||
"example": [ | ||
".meta/example.coffee" | ||
] | ||
}, | ||
"blurb": "Write a simple linked list implementation that uses Elements and a List.", | ||
"source": "Inspired by 'Data Structures and Algorithms with Object-Oriented Design Patterns in Ruby', singly linked-lists.", | ||
"source_url": "https://web.archive.org/web/20160731005714/http://brpreiss.com/books/opus8/html/page96.html" | ||
} |
40 changes: 40 additions & 0 deletions
40
exercises/practice/simple-linked-list/.meta/example.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
class Element | ||
constructor: (@value, @next = null) -> | ||
|
||
class LinkedList | ||
constructor: (values) -> | ||
@count = 0 | ||
@head = null | ||
if values? | ||
for value in values | ||
@add new Element value | ||
|
||
length: -> | ||
@count | ||
|
||
add: (element) -> | ||
elt = element | ||
elt.next = @head | ||
@head = elt | ||
@count += 1 | ||
|
||
toArray: -> | ||
elements = [] | ||
element = @head | ||
while element? | ||
elements.push element.value | ||
element = element.next | ||
elements | ||
|
||
reverse: (prev = null) -> | ||
if @head?.next | ||
current = @head | ||
@head = @head.next | ||
current.next = prev | ||
@reverse current | ||
else | ||
@head.next = prev | ||
@ | ||
|
||
module.exports.Element = Element | ||
module.exports.LinkedList = LinkedList |
16 changes: 16 additions & 0 deletions
16
exercises/practice/simple-linked-list/simple-linked-list.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class Element | ||
constructor: (value) -> | ||
|
||
class LinkedList | ||
constructor: (values) -> | ||
|
||
length: -> | ||
|
||
add: (element) -> | ||
|
||
toArray: -> | ||
|
||
reverse: -> | ||
|
||
module.exports.Element = Element | ||
module.exports.LinkedList = LinkedList |
117 changes: 117 additions & 0 deletions
117
exercises/practice/simple-linked-list/simple-linked-list.spec.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
{ Element, LinkedList } = require './simple-linked-list' | ||
|
||
describe 'Simple Linked List', -> | ||
describe 'Element class', -> | ||
xit 'has constructor', -> | ||
element = new Element 1 | ||
expect(element.value).toEqual 1 | ||
|
||
xit 'value reflects constructor arg', -> | ||
element = new Element 2 | ||
expect(element.value).toEqual 2 | ||
|
||
xit 'has null for next by default', -> | ||
element = new Element 1 | ||
expect(element.next).toEqual null | ||
|
||
describe 'List class', -> | ||
xit 'has constructor', -> | ||
list = new LinkedList | ||
expect(list).toBeDefined() | ||
|
||
xit 'new lists should have length 0', -> | ||
list = new LinkedList | ||
expect(list.length()).toEqual 0 | ||
|
||
xit 'can add a element', -> | ||
list = new LinkedList | ||
element = new Element 1 | ||
expect -> list.add element | ||
.not.toThrow() | ||
|
||
xit 'adding a element increments length', -> | ||
list = new LinkedList | ||
element = new Element 1 | ||
list.add element | ||
expect(list.length()).toEqual 1 | ||
|
||
xit 'adding two elements increments twice', -> | ||
list = new LinkedList | ||
element1 = new Element 1 | ||
element2 = new Element 3 | ||
list.add element1 | ||
list.add element2 | ||
expect(list.length()).toEqual 2 | ||
|
||
xit 'new Lists have a null head element', -> | ||
list = new LinkedList | ||
expect(list.head).toEqual null | ||
|
||
xit 'adding an Element to an empty list sets the head Element', -> | ||
list = new LinkedList | ||
element = new Element 1 | ||
list.add element | ||
expect(list.head.value).toEqual 1 | ||
|
||
xit 'adding a second Element updates the head Element', -> | ||
list = new LinkedList | ||
element1 = new Element 1 | ||
element2 = new Element 3 | ||
list.add element1 | ||
list.add element2 | ||
expect(list.head.value).toEqual 3 | ||
|
||
xit 'can get the next Element from the head', -> | ||
list = new LinkedList | ||
element1 = new Element 1 | ||
element2 = new Element 3 | ||
list.add element1 | ||
list.add element2 | ||
expect(list.head.next.value).toEqual 1 | ||
|
||
xit 'can be inxitialized wxith an array', -> | ||
list = new LinkedList [1, 2, 3] | ||
expect(list.length()).toEqual 3 | ||
expect(list.head.value).toEqual 3 | ||
|
||
describe 'Lists wxith multiple elements', -> | ||
|
||
xit 'wxith correct length', -> | ||
list = new LinkedList [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | ||
expect(list.length()).toEqual 10 | ||
|
||
xit 'wxith correct head value', -> | ||
list = new LinkedList [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | ||
expect(list.head.value).toEqual 10 | ||
|
||
xit 'can traverse the list', -> | ||
list = new LinkedList [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | ||
expect(list.head.next.next.next.value).toEqual 7 | ||
|
||
xit 'can convert to an array', -> | ||
list = new LinkedList [1] | ||
expect(list.toArray()).toEqual [1] | ||
|
||
xit 'head of list is final element from input array', -> | ||
list = new LinkedList [1, 2] | ||
expect(list.head.value).toEqual 2 | ||
|
||
xit 'can convert longer list to an array', -> | ||
list = new LinkedList [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | ||
expect(list.toArray()).toEqual [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] | ||
|
||
xit 'can be reversed', -> | ||
list = new LinkedList [1, 2] | ||
expect(list.reverse().toArray()).toEqual [1, 2] | ||
|
||
xit 'can be reversed when xit has more elements', -> | ||
list = new LinkedList [1, 2, 3] | ||
expect(list.reverse().toArray()).toEqual [1, 2, 3] | ||
|
||
xit 'can reverse with many elements', -> | ||
list = new LinkedList [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | ||
expect(list.reverse().toArray()).toEqual [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | ||
|
||
xit 'can reverse a reversal', -> | ||
list = new LinkedList [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | ||
expect(list.reverse().reverse().toArray()).toEqual [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] |