Class: List

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/prct06/list.rb

Instance Method Summary collapse

Constructor Details

#initializeList

Returns a new instance of List.



20
21
22
23
# File 'lib/prct06/list.rb', line 20

def initialize
  @head = nil
  @tail = nil
end

Instance Method Details

#add(value) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/prct06/list.rb', line 32

def add(value)
  node = Node.new(value, nil, nil)
  if @head == nil
    @head = node
  end
  if @tail != nil
    @tail.nex = node
    node.prev = @tail
  end
  @tail = node
  return self
end

#each(&block) ⇒ Object



89
90
91
# File 'lib/prct06/list.rb', line 89

def each(&block)
  to_array.each(&block)  
end

#headObject



25
26
27
# File 'lib/prct06/list.rb', line 25

def head
  @head
end

#popObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/prct06/list.rb', line 62

def pop
  if @tail != nil
    if @tail.prev != nil
      toReturn = @tail
      @tail = @tail.prev
      @tail.nex = nil
      return toReturn
    else
      lastElement = @tail
      @tail = nil
      return lastElement
    end
  else
    return "List is empty" 
  end
end

#shiftObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/prct06/list.rb', line 45

def shift
  if @head != nil
    if @head.nex != nil
      toReturn = @head
      @head = @head.nex
      @head.prev = nil
      return toReturn
    else
      lastElement = @head
      @head = nil
      return lastElement
    end
  else
    return "List is empty"
  end
end

#tailObject



28
29
30
# File 'lib/prct06/list.rb', line 28

def tail
  @tail
end

#to_arrayObject



79
80
81
82
83
84
85
86
87
# File 'lib/prct06/list.rb', line 79

def to_array
  resultArray = []
  current = @head
  while current != nil do
    resultArray << current
    current = current.nex
  end
  return resultArray
end