Class: Lista

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(val) ⇒ Lista

Constructor

Parameters:

  • val

    as first element



9
10
11
12
13
# File 'lib/pract/list.rb', line 9

def initialize(val)
	@head = @tail = Nodo.new(nil, val, nil)
	@vector = Array.new
	@vector2 = Array.new
end

Instance Attribute Details

#headObject

Returns the value of attribute head



5
6
7
# File 'lib/pract/list.rb', line 5

def head
  @head
end

#tailObject

Returns the value of attribute tail



5
6
7
# File 'lib/pract/list.rb', line 5

def tail
  @tail
end

Instance Method Details

#add_first(val) ⇒ Object

Add val as first element



35
36
37
38
39
# File 'lib/pract/list.rb', line 35

def add_first (val)
	point = @head
	point.prev = Nodo.new(nil, val, point)
	@head = point.prev
end

#add_last(val) ⇒ Object

Add val as last element



29
30
31
32
33
# File 'lib/pract/list.rb', line 29

def add_last (val)
	point = @tail
	point.next = Nodo.new(point, val, nil)
	@tail = point.next
end

#each {|point.val| ... } ⇒ Object

Make list enumerable

Yields:

  • (point.val)


101
102
103
104
105
106
107
108
# File 'lib/pract/list.rb', line 101

def each
	point = @head
	while(point != @tail) do
		yield point.val
		point = point.next
	end
	yield(point.val)
end

#remove(val) ⇒ Object

Remove an element

Parameters:

  • val (value)

    as the value we want to remove



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/pract/list.rb', line 52

def remove (val)
	if (@head.val == val) 
		remove_first
	elsif (@tail.val == val) 
		remove_last
	else
		point = @head
		while (point.next != nil) && (point.val != val)
			point = point.next
		end
		if point != @tail
			before = point.prev
			after = point.next
			before.next = after
			after.prev = before
			point.prev = nil
			point.next = nil
		end
	end
end

#remove_firstObject

Remove the first element



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

def remove_first 
	point = @head
	@head = point.next
end

#remove_lastObject

Remove the last element



46
47
48
49
# File 'lib/pract/list.rb', line 46

def remove_last
	point = @tail
	@tail = point.prev
end

#search(val) ⇒ Object

Returns position of val[from 1] or nil is not found

Returns:

  • position of val[from 1] or nil is not found



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/pract/list.rb', line 84

def search(val)
	num = 1
	point = @head
	while(point != @tail)
		if(point.val == val) 
			return num
		else 
			num = num+1
			point = point.next
		end
	end
	if @tail.val == val 
		return num
	else return 0
	end
end

#szObject

Returns size of element

Returns:

  • size of element



73
74
75
76
77
78
79
80
81
82
# File 'lib/pract/list.rb', line 73

def sz
	size_list = 0
	point = @head
	while(point != tail)
		size_list = size_list + 1
		point = point.next
	end
	size_list = size_list+1
	return size_list
end

#to_sObject

Returns string format

Parameters:

  • point

    as auxiliar pointer

  • elements

    as showing variable

Returns:

  • string format



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pract/list.rb', line 17

def to_s 
	elements = "[" 
	point = @head 
		while point != @tail
		elements = elements + point.val.to_s
			elements = elements + "], ["
		point = point.next
		end 
		elements = elements + point.val.to_s
		elements = elements + "]"
end