Class: Liste

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

Overview

Doubly Linked List Enumerable functions included

Author:

  • roro

Defined Under Namespace

Classes: Node

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializenil

Initialize Initializes all to nil



17
18
19
20
21
# File 'lib/prct06/list.rb', line 17

def initialize()
	@head = nil
	@tail = nil
	@size = 0
end

Instance Attribute Details

#headObject (readonly)

Returns the value of attribute head.



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

def head
  @head
end

#sizeObject (readonly)

Returns the value of attribute size.



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

def size
  @size
end

#tailObject (readonly)

Returns the value of attribute tail.



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

def tail
  @tail
end

Instance Method Details

#eachobj.value

Each Method, necessarie for the enumeration

Returns:

  • (obj.value)
    gives back the value of the actual node


27
28
29
30
31
32
33
# File 'lib/prct06/list.rb', line 27

def each()
	act = @head
	while act != nil
		yield act.value
		act = act.next
	end
end

#eachsObject

Tarea 4 - each



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/prct06/list.rb', line 140

def eachs
	sorted = [@head.value]
	self.each_with_index do |x, pos_x|
		if (pos_x != 0)
			sorted.each_with_index do |y, pos_y|
				if (pos_y == sorted.size - 1)
					if (x < y)
						sorted.insert(pos_y, x)
						break
					else
						sorted.push(x)
						break
					end
				elsif (x < y)
					sorted.insert(pos_y, x)
					break
				end
			end
		end
	end
	return sorted
end

#forsObject

Tarea 3 - for



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/prct06/list.rb', line 122

def fors
	sorted = [@head.value]
	act = @head
	for i in (1...@size)
		act = act.next
		for j in (0..sorted.size)
			if (j == sorted.size)
				sorted.push(act.value)
			elsif (act.value < sorted[j])
				sorted.insert(j, act.value)
				break
			end
		end
	end
	return sorted
end

#popstring

Pops the last Element of the list and prints it via the to_s method.

Returns:

  • (string)
    Returns the String of the Object


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/prct06/list.rb', line 68

def pop()
	if size > 0
		a = @tail.value
		@size -= 1
		@tail = @tail.prev
		if size > 0
			@tail.next = nil
		else
			@head = nil
		end
	else
		puts "No elements"
	end
	return a.to_s
end

#popbObject



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

def popb()
	if size > 0
		a = @head.value
		@size -= 1
		@head = @head.next
		if size > 0
			@head.prev = nil
		else
			@head = nil
		end
	else
		puts "No elements"
	end
	return a.to_s
end

#popbn(i) ⇒ Object



115
116
117
118
119
# File 'lib/prct06/list.rb', line 115

def popbn(i)
	while @size > 0 && i > 0 do
		popb()
	end
end

#popn(i) ⇒ Object



110
111
112
113
114
# File 'lib/prct06/list.rb', line 110

def popn(i)
	while @size > 0 && i > 0 do
		pop()
	end
end

#push(obj) ⇒ string

Pushes a new Object at the end of the list.

Parameters:

  • obj (object)
    Object that should be placed at the end of the list.

Returns:

  • (string)
    Returns the String of the Object


40
41
42
43
44
45
46
47
48
49
50
# File 'lib/prct06/list.rb', line 40

def push(obj)
	a = Node.new(obj, nil, @tail)
	if size > 0
		@tail.next = a
	else
		@head = a
	end
	@tail = a
	@size += 1
	return a.value.to_s
end

#pushb(obj) ⇒ Object



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

def pushb(obj)
	a = Node.new(obj, @head, nil)
	if size > 0
		@head.prev = a
	else
		@tail = a
	end
	@head = a
	@size += 1
	return a.value.to_s
end

#pushbn(ar) ⇒ Object



105
106
107
108
109
# File 'lib/prct06/list.rb', line 105

def pushbn(ar)
	ar.each do |i|
           pushb(i)
       end
end

#pushn(ar) ⇒ Object



100
101
102
103
104
# File 'lib/prct06/list.rb', line 100

def pushn(ar)
	ar.each do |i|
           push(i)
       end
end