Sha256: 20ae1822bbf4e1b2f62f259a79d35045735bf7fbea3fe99f63edf32b991386dd

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

Nodo = Struct.new(:dato,:next_,:prev_)

class Lista

	attr_reader :head, :tail
	include Enumerable

	def initialize()
		@head=@tail=nil
	end
	
	def add_back(dato)
		if dato.instance_of?Alimento
			node=Nodo.new(dato,nil,nil)

			@head=node if @head.nil?
			node.prev_=@tail unless @tail.nil?
			@tail.next_=node unless @tail.nil?
			@tail=node
		end
		if dato.instance_of?Array
			i=0
			while i<dato.length do
				node=Nodo.new(dato[i],nil,nil)

				@head=node if @head.nil?
				node.prev_=@tail unless @tail.nil?
				@tail.next_=node unless @tail.nil?
				@tail=node
				i+=1
			end
		end
	end
	
	def add_front(dato)
		if dato.instance_of?Alimento 
			node=Nodo.new(dato,nil,nil)
		
			@tail = node if @tail.nil?
			node.next_ = @head unless @head.nil?
			@head.prev_ = node unless @head.nil?		
			@head = node
		end
		if dato.instance_of?Array
			i=0
			while i<dato.length do
				@tail = node if @tail.nil?
				node.next_ = @head unless @head.nil?
				@head.prev_ = node unless @head.nil?		
				@head = node
				i+=1
			end			
		end
	end
	
	def pop_back
		if (@tail==@head)&&(@tail==nil)
			return nil
		elsif @tail==@head
			aux=@tail.dup
			@tail=@head=nil
			return aux
		else 
			aux=@tail.dup
			@tail=@tail.prev_
			@tail.next_=nil
			aux.prev_=nil
			return aux
		end
	end

	def pop_front
		if (@tail==@head)&&(@tail==nil)
			return nil
		elsif @tail==@head
			aux=@head.dup
			@head=@tail=nil
			return aux
		else
			aux=@head.dup
			@head=@head.next_
			@head.prev_=nil
			aux.next_=nil
			return aux
		end
	end
	
	def mostrar
		str= ""
		aux=@head
		while aux!=nil do
			str+="\t"+aux.dato.to_s+"\n"
			aux=aux.next_
		end
		str
	end
	
	def [] (index)
		i=0
		aux=@head
		while i<index&&aux!=nil do
			i+=1
			aux=aux.next_
		end
		aux			
	end
	
	def each
		aux=@head
		while aux!=nil do
			yield aux.dato
			aux=aux.next_
		end
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
alimento0100886306-0.1.0 lib/alimento/lista.rb