diff --git a/examples/linkedlist/README.md b/examples/linkedlist/README.md index 9f0a953..30d2334 100644 --- a/examples/linkedlist/README.md +++ b/examples/linkedlist/README.md @@ -2,3 +2,4 @@ Linked List =========== * [Unordered Linked List](https://github.com/lucianlature/data-structures/tree/master/examples/linkedlist/unordered.md) + * [Ordered Linked List](https://github.com/lucianlature/data-structures/tree/master/examples/linkedlist/ordered.md) diff --git a/examples/linkedlist/ordered.md b/examples/linkedlist/ordered.md new file mode 100644 index 0000000..e946950 --- /dev/null +++ b/examples/linkedlist/ordered.md @@ -0,0 +1,130 @@ +Ordered Linked List +===================== + +Plain JavaScript implementation of an ordered linked list. + +------------ + +###Ordered List### + +The list object returned when require is called on this library + + var List = require('OrderedList'); + var list = new List(); + +------------ + +###Methods### + +**add(item)** +Adds a new item to the list. It returns nothing. + + list.add(3); + list.add(13); + list.add(8); + list.add(5); + list.add(21); + + // list has - 3 -> 5 -> 8 -> 13 -> 21 + +**remove(item)** +Removes the item from the list + + list.add(3); + list.add(13); + list.add(8); + list.add(5); + list.add(21); + + list.remove(8); + + //> list has - 3 -> 5 -> 13 -> 21 + + +**search(item)** +Searches for the item in the list + + list.add(3); + list.add(13); + list.add(8); + list.add(5); + list.add(21); + + console.log(list.search(3)); + //> true + console.log(list.search(4)); + //> false + + +**isEmpty()** +Checks if the list is empty or not + + if (!list.isEmpty()) { + while(list.pop()); + } + console.log(list.isEmpty()); + //> true + + +**size()** +Returns the number of the items in the list + + list.add(3); + list.add(13); + list.add(8); + list.add(5); + list.add(21); + + console.log(list.size()); + //> 5 + + if (!list.isEmpty()) { + while(list.pop()); + } + + console.log(list.size()); + //> 0 + +**indexOf(item)** +Returns the position of item in the list + + list.add(3); + list.add(13); + list.add(8); + list.add(5); + list.add(21); + + console.log(list.indexOf(8)); + //> 2 + console.log(list.indexOf(5)); + //> 1 + +**pop()** +Removes and returns the last item in the list. + + list.add(3); + list.add(13); + list.add(8); + list.add(5); + list.add(21); + + //> list has - 3 -> 5 -> 8 -> 13 -> 21 + + console.log(list.pop()); + //> 21 //list now has - 3 -> 5 -> 8 -> 13 + + +**pop(pos)** +Removes and returns the item at position pos in the list. + + list.add(3); + list.add(13); + list.add(8); + list.add(5); + list.add(21); + + //> list has - 3 -> 5 -> 8 -> 13 -> 21 + + console.log(list.pop(1)); + //> 5 //list now has - 3 -> 8 -> 13 -> 21 + diff --git a/examples/linkedlist/unordered.md b/examples/linkedlist/unordered.md index 8d6e3b4..9e6a05f 100644 --- a/examples/linkedlist/unordered.md +++ b/examples/linkedlist/unordered.md @@ -5,9 +5,8 @@ Plain JavaScript implementation of an unordered linked list. ------------ -###Objects### +###Unordered List### -**Unordered List** The list object returned when require is called on this library var List = require('UnorderedList');