Class: TrickBag::Collections::LinkedList

Inherits:
Object
  • Object
show all
Defined in:
lib/trick_bag/collections/linked_list.rb

Overview

Linked List based on [email protected]:neilparikh/ruby-linked-list.git, but modified somewhat.

Defined Under Namespace

Classes: Node

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*items) ⇒ LinkedList

Returns a new instance of LinkedList.

Parameters:

  • items

    items with which to initialize the list



22
23
24
25
26
# File 'lib/trick_bag/collections/linked_list.rb', line 22

def initialize(*items)
  @length = items.length
  @first = Node.new(items.shift)
  items.each { |item| push(item) }
end

Instance Attribute Details

#firstObject

Returns the value of attribute first.



17
18
19
# File 'lib/trick_bag/collections/linked_list.rb', line 17

def first
  @first
end

#lengthObject (readonly)

Returns the value of attribute length.



18
19
20
# File 'lib/trick_bag/collections/linked_list.rb', line 18

def length
  @length
end

Instance Method Details

#popObject

Returns the last element from the list and removes it

Returns:

  • the last element’s value



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/trick_bag/collections/linked_list.rb', line 45

def pop
  case(@length)

    when 0
      raise "List is empty"

    when 1
      @length = 0
      value = @first.value
      @first = nil
      value

    else
      current = @first
      while current.next && current.next.next
        current = current.next
      end
      value = current.next.value
      current.next = nil
      @length -= 1
      value
  end
end

#push(value) ⇒ Object

Returns self.

Parameters:

  • value

    value to add to end of list

Returns:

  • self



31
32
33
34
35
36
37
38
39
40
# File 'lib/trick_bag/collections/linked_list.rb', line 31

def push(value)
  node = Node.new(value)
  current_node = @first
  while current_node.next
    current_node = current_node.next
  end
  current_node.next = node
  @length += 1
  self
end

#shiftObject

Removes the first element from the list

Returns:

  • the first element’s value



83
84
85
86
87
88
89
# File 'lib/trick_bag/collections/linked_list.rb', line 83

def shift
  raise "List is empty" if @length < 1
  return_value = @first.value
  @first = @first.next
  @length -= 1
  return_value
end

#to_aObject

Returns the values in this list as an array.

Returns:

  • the values in this list as an array



93
94
95
96
97
98
99
100
101
# File 'lib/trick_bag/collections/linked_list.rb', line 93

def to_a
  current_node = @first
  array = []
  while current_node
    array << current_node.value
    current_node = current_node.next
  end
  array
end

#to_aryObject

Returns the values in this list as an array.

Returns:

  • the values in this list as an array



105
106
107
# File 'lib/trick_bag/collections/linked_list.rb', line 105

def to_ary
  to_a
end

#unshift(value) ⇒ Object

Adds a value to the beginning of the list

Parameters:

  • value

    value to add to beginning of list

Returns:

  • self



73
74
75
76
77
78
# File 'lib/trick_bag/collections/linked_list.rb', line 73

def unshift(value)
  node = Node.new(value, @first)
  @first = node
  @length += 1
  self
end