class List

@author Daniel Darias Sánchez <alu0100783230@ull.edu.es>

Attributes

size[R]

Public Class Methods

new() click to toggle source
# File lib/Prct07/List.rb, line 11
def initialize
  @head = @tail = nil
  @size = 0
end

Public Instance Methods

back() click to toggle source

returns the last element of the list

# File lib/Prct07/List.rb, line 81
def back
  @tail.value
end
each() { |value| ... } click to toggle source

necessary for enumerable module

# File lib/Prct07/List.rb, line 104
def each
  aux = @head
  while aux!= nil do
    yield aux.value
    aux = aux.next
  end
end
empty() click to toggle source

checks if the list is empty

# File lib/Prct07/List.rb, line 42
def empty
  @size == 0
end
front() click to toggle source

returns the first element of the list

# File lib/Prct07/List.rb, line 86
def front
  @head.value
end
getAt(index) click to toggle source

returns the element specificated by the index

# File lib/Prct07/List.rb, line 91
def getAt index
  if (index >= @size)
    nil
  else
    aux = @head
    for i in 0..index - 1
      aux = aux.next
    end
    aux.value
  end
end
pop_back() click to toggle source

extracts the last element of the list and returns it

# File lib/Prct07/List.rb, line 64
def pop_back
  @size = @size - 1
  if (@head == nil)
    nil
  else
    aux = @tail
    if (@tail.prev != nil)
      @tail = @tail.prev
      @tail.next = nil
    else
      @head = nil
    end
    aux.value
  end
end
pop_front() click to toggle source

extracts the first element of the list and returns it

# File lib/Prct07/List.rb, line 47
def pop_front
  @size = @size - 1
  if (@head == nil)
    nil
  else
    aux = @head
    if (@head.next != nil)
      @head = @head.next
      @head.prev = nil
    else
      @head = nil
    end
    aux.value
  end
end
push_back(element) click to toggle source

inserts elements by the tail of the list

# File lib/Prct07/List.rb, line 17
def push_back element
  @size = @size + 1
  if (@head == nil)
    @head = @tail = Node.new element, nil, nil
  else
    @tail.next = Node.new element, @tail, nil
    @tail = @tail.next
  end
  true
end
push_front(element) click to toggle source

inserts elements by the head of list

# File lib/Prct07/List.rb, line 29
def push_front element
  @size = @size + 1
  if (@head == nil)
    @head = @tail = Node.new element, nil, nil
  else
    aux = @head
    @head = Node.new element, nil, aux
    aux.prev = @head
  end
  true
end