Class: DataStructList::Stack

Inherits:
Object
  • Object
show all
Defined in:
lib/data_struct_list.rb

Direct Known Subclasses

SimpleLinkedList

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStack

Returns a new instance of Stack.



26
27
28
29
30
# File 'lib/data_struct_list.rb', line 26

def initialize
  @head = Head.new
  @last = nil
  @first = nil
end

Instance Attribute Details

#firstObject (readonly)

Returns the value of attribute first.



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

def first
  @first
end

#headObject (readonly)

Returns the value of attribute head.



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

def head
  @head
end

#lastObject (readonly)

Returns the value of attribute last.



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

def last
  @last
end

Instance Method Details

#find(id) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/data_struct_list.rb', line 48

def find(id)
  elm = @first

  until elm == nil
    if elm.id == id then break end
    elm = elm.next
  end

  return elm
end

#insert(hash) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/data_struct_list.rb', line 32

def insert(hash)
  if @head.quant == 0 then
    @head.next = Node.new(hash)
    @first = @head.next
    @last = @head.next
    @head.quant += 1

  else
    @last.next =  Node.new(hash)
    @last = @last.next
    @head.quant += 1
  end

  return @last.id
end

#removeObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/data_struct_list.rb', line 59

def remove
  return nil if @head.quant == 0

  elm = @head.next
  @head.quant-=1

  until elm.next == @last
    if elm == @last then break end
    elm = elm.next
  end

  if @first == @last then
    @first = nil
    @last = nil
    @head.next = nil
    return nil
  end

  aux = @last
  @last = elm
  elm.next = nil
  aux = nil
end