InfraRuby Vision delivers solutions for mobile and web applications.
You can try InfraRuby live in your browser!
Try the InfraRuby statically typed Ruby compiler live in your browser. Try our examples or write your own code.
Follow @InfraRuby on Twitter for news and updates.

LinkedList ✦ Examples

InfraRuby is a compiler and runtime for statically typed Ruby. You can use InfraRuby for your own projects with our free download!

coding style

The InfraRuby compiler supports many coding styles. In particular, you may use either spaces or tabs for indentation and you may omit the return keyword where applicable.

For more information:

This example implements a linked list.

LinkedList

## <E: ..Object?>
##   @head_entry: Entry<E>?
##   @tail_entry: Entry<E>?
##   @n: int32
class LinkedList
	## <E: ..Object?>
	##   @prev_entry: Entry<E>?
	##   @next_entry: Entry<E>?
	##   @e: E
	class Entry
		## E -> void
		def initialize(e)
			@prev_entry = nil
			@next_entry = nil
			@e = e
			return
		end

		attr_accessor :prev_entry, :next_entry
		attr_reader :e
	end

	## -> void
	def initialize
		@head_entry = nil
		@tail_entry = nil
		@n = 0.i32
		return
	end

	## -> E
	def __first
		entry = @head_entry.not_nil!
		return entry.e
	end

	## -> E
	def __last
		entry = @tail_entry.not_nil!
		return entry.e
	end

	## E -> void
	def __push(e)
		entry = Entry.new(e)
		tail_entry = @tail_entry
		if tail_entry.nil?
			@head_entry = entry
		else
			tail_entry.next_entry = entry
			entry.prev_entry = tail_entry
		end
		@tail_entry = entry
		@n += 1
		return
	end

	## E -> void
	def __unshift(e)
		entry = Entry.new(e)
		head_entry = @head_entry
		if head_entry.nil?
			@tail_entry = entry
		else
			head_entry.prev_entry = entry
			entry.next_entry = head_entry
		end
		@head_entry = entry
		@n += 1
		return
	end

	## -> E
	def __pop
		entry = @tail_entry.not_nil!
		tail_entry = entry.prev_entry
		if tail_entry.nil?
			@head_entry = nil
		else
			tail_entry.next_entry = nil
		end
		@tail_entry = tail_entry
		@n -= 1
		return entry.e
	end

	## -> E
	def __shift
		entry = @head_entry.not_nil!
		head_entry = entry.next_entry
		if head_entry.nil?
			@tail_entry = nil
		else
			head_entry.prev_entry = nil
		end
		@head_entry = head_entry
		@n -= 1
		return entry.e
	end

	## -> boolean
	def empty?
		return @n == 0
	end

	## -> Integer
	def size
		return @n.to_i
	end

	## -> int32
	def size_as_int32
		return @n
	end

	## -> void
	def clear
		@head_entry = nil
		@tail_entry = nil
		@n = 0.i32
		return
	end

	## -> E?
	def first
		if empty?
			return nil
		end
		return __first
	end

	## -> E
	def first!
		if empty?
			raise IndexError
		end
		return __first
	end

	## -> E?
	def last
		if empty?
			return nil
		end
		return __last
	end

	## -> E
	def last!
		if empty?
			raise IndexError
		end
		return __last
	end

	## E -> self
	def <<(e)
		__push(e)
		return self
	end

	## E -> void
	def push(e)
		__push(e)
		return
	end

	## E -> void
	def unshift(e)
		__unshift(e)
		return
	end

	## -> E?
	def pop
		if empty?
			return nil
		end
		return __pop
	end

	## -> E
	def pop!
		if empty?
			raise IndexError
		end
		return __pop
	end

	## -> E?
	def shift
		if empty?
			return nil
		end
		return __shift
	end

	## -> E
	def shift!
		if empty?
			raise IndexError
		end
		return __shift
	end
end
Follow @InfraRuby on Twitter
Copyright © 2011-2017 InfraRuby Vision Limited