Skip to main content

Organize linked list into groups of even and odd nodes in ruby

Ruby program for Organize linked list into groups of even and odd nodes. Here problem description and explanation.

#  Ruby program for
#  Grouping of even and odd nodes in linked list

#  Linked list node
class LinkNode 
	# Define the accessor and reader of class LinkNode
	attr_reader :data, :next
	attr_accessor :data, :next
	def initialize(data) 
		self.data = data
		self.next = nil
	end
end

class SingleLL 
	# Define the accessor and reader of class SingleLL
	attr_reader :head
	attr_accessor :head
	def initialize() 
		self.head = nil
	end

	#  Adding new node at beginning of linked list
	def insert(data) 
		#  Create new node
		node = LinkNode.new(data)
		#  Connect current node to head
		node.next = self.head
		self.head = node
	end

	#  Display linked list element
	def display() 
		if (self.head == nil) 
			return
		end

		temp = self.head
		#  iterating linked list elements
		while (temp != nil) 
			print(" ", temp.data ," →")
			#  Visit to next node
			temp = temp.next
		end
		print(" NULL\n")
	end

	#  Arrange alternative even and odd nodes in linked list
	def arrangeEvenOdds() 
		#  Define some auxiliary variables
		even_node = nil
		odd_node = nil
		tail_1 = nil
		tail_2 = nil
		auxiliary = self.head
		self.head = nil
		#  Segregation of even and odd nodes elements
		while (auxiliary != nil) 
			if (auxiliary.data % 2 == 0) 
				#  Even Node element
				if (even_node == nil) 
					even_node = auxiliary
				else
 
					#  Add node at end of even list
					tail_1.next = auxiliary
				end

				#  Get new last node
				tail_1 = auxiliary
				#  visit to next node
				auxiliary = auxiliary.next
				#  set next node is null
				tail_1.next = nil
			else
 
				#  Odd Node element
				if (odd_node == nil) 
					odd_node = auxiliary
				else
 
					#  Add node at end of odd list
					tail_2.next = auxiliary
				end

				#  Get new last node
				tail_2 = auxiliary
				#  visit to next node
				auxiliary = auxiliary.next
				#  set next node is null
				tail_2.next = nil
			end

		end

		if (even_node != nil) 
			#  new first node of linked list
			self.head = even_node
		else
 
			#  When only odd element exists in linked list
			self.head = odd_node
		end

		#  Combine even and odd nodes
		while (even_node != nil && odd_node != nil) 
			#  next node of even list
			tail_1 = even_node.next
			#  next node of odd list
			tail_2 = odd_node.next
			even_node.next = odd_node
			if (tail_1 != nil) 
				#  This is useful to handle 
				#  when even linked list next node is empty 
				#  and odd elements are next node not empty.
				odd_node.next = tail_1
			end

			#  Visit to next node
			even_node = tail_1
			odd_node = tail_2
		end
	end
end

def main() 
	list1 = SingleLL.new()
	list2 = SingleLL.new()
	list3 = SingleLL.new()
	#  Create linked list1
	list1.insert(8)
	list1.insert(2)
	list1.insert(9)
	list1.insert(7)
	list1.insert(3)
	list1.insert(10)
	list1.insert(5)
	list1.insert(4)
	list1.insert(6)
	#  Create linked list2
	list2.insert(1)
	list2.insert(1)
	list2.insert(4)
	list2.insert(2)
	list2.insert(3)
	list2.insert(1)
	#  Create linked list3
	list3.insert(2)
	list3.insert(3)
	list3.insert(3)
	list3.insert(8)
	list3.insert(4)
	list3.insert(6)
	list3.insert(2)
	#  Test A
	print(" Before arrange : \n")
	#  Before arrange nodes
	list1.display()
	#  Case A
	#  When linked list contains same length of 
	#  Even and Odd nodes
	list1.arrangeEvenOdds()
	print(" After arrange  : \n")
	#  After arrange nodes
	list1.display()
	#  Test B
	print("\n Before arrange : \n")
	#  Before arrange nodes
	list2.display()
	#  Case B
	#  When linked list Odd nodes are more than Even
	list2.arrangeEvenOdds()
	print(" After arrange  : \n")
	#  After arrange nodes
	list2.display()
	#  Test C
	print("\n Before arrange : \n")
	#  Before arrange nodes
	list3.display()
	#  Case C
	#  When linked list Odd nodes are more than Even
	list3.arrangeEvenOdds()
	print(" After arrange  : \n")
	#  After arrange nodes
	list3.display()
end

main()

Output

 Before arrange : 
 6 → 4 → 5 → 10 → 3 → 7 → 9 → 2 → 8 → NULL
 After arrange  : 
 6 → 5 → 4 → 3 → 10 → 7 → 2 → 9 → 8 → NULL

 Before arrange : 
 1 → 3 → 2 → 4 → 1 → 1 → NULL
 After arrange  : 
 2 → 1 → 4 → 3 → 1 → 1 → NULL

 Before arrange : 
 2 → 6 → 4 → 8 → 3 → 3 → 2 → NULL
 After arrange  : 
 2 → 3 → 6 → 3 → 4 → 8 → 2 → NULL




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment