Class: LinkedList

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

Overview

Clase lista

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLinkedList

Constructor



15
16
17
# File 'lib/prct06/list.rb', line 15

def initialize
  @head = @tail = nil
end

Instance Attribute Details

#headObject

Variables



11
12
13
# File 'lib/prct06/list.rb', line 11

def head
  @head
end

#tailObject

Variables



11
12
13
# File 'lib/prct06/list.rb', line 11

def tail
  @tail
end

Instance Method Details

#add(value, *more) ⇒ Object

Añadir nodos a la lista



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/prct06/list.rb', line 20

def add (value, *more)

  node = Node.new(value)

  @tail = node if @tail.nil?

  @head.next = node unless @head.nil?
  node.prev = @head unless @head.nil?

  @head = node

  more.each do |i|
    node2 = Node.new(i)
    @head.next = node2
    node2.prev = @head
    @head = @head.next
  end
end

#eachObject



66
67
68
69
70
71
72
73
# File 'lib/prct06/list.rb', line 66

def each
  aux = @tail

  while aux != nil do
    yield aux.value
    aux = aux.next
  end
end

#emptyObject



46
47
48
49
50
51
# File 'lib/prct06/list.rb', line 46

def empty()
  emptyy = false
  emptyy = true if @head.nil?

  return emptyy
end

#popObject

Saca la cabeza de la lista



40
41
42
43
44
# File 'lib/prct06/list.rb', line 40

def pop()
  node = @head
  @head = @head.next
  return node
end

#to_sObject

Mostrar la lista



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/prct06/list.rb', line 54

def to_s
  $i = @tail

  while ($i != nil) do
    puts "#{$i.value}"
    $i = $i.next
  end

  puts "CABEZA: #{@head.value}"
  puts "COLA: #{@tail.value}"
end