Skip to main content

Delete all even parity nodes from circular linked list in ruby

Ruby program for Delete all even parity nodes from circular linked list. Here more information.

#  Ruby program for
#  Remove all even parity nodes from a circular single linked list
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 CircularLinkedList 
	# Define the accessor and reader of class CircularLinkedList
	attr_reader :head, :tail
	attr_accessor :head, :tail
	def initialize() 
		self.head = nil
		self.tail = nil
	end

	def insert(value) 
		node = LinkNode.new(value)
		if (self.head == nil) 
			self.head = node
		else
 
			self.tail.next = node
		end

		node.next = self.head
		self.tail = node
	end

	#  Display node element of circular linked list
	def display() 
		if (self.head == nil) 
			print("\n Empty Linked List\n")
		else
 
			print("\n Linked List Element \n")
			#  First node of linked list
			print("  ", self.head.data)
			temp = self.head.next
			#  Display linked list node
			while (temp != nil && temp != self.head) 
				#  Display node value
				print("  ", temp.data)
				#  Visit to next node
				temp = temp.next
			end

		end

	end

	#  Handles the request to find set bits in given integer
	def coutSetBit(num) 
		result = num
		result = result - ((result >> 1) & 1431655765)
		result = (result & 858993459) + ((result >> 2) & 858993459)
		result = (result + (result >> 4)) & 252645135
		result = result + (result >> 8)
		result = result + (result >> 16)
		result = result & 63
		return result
	end

	def removeNode() 
		if (self.head == nil) 
			return
		end

		temp = self.head
		auxiliary = self.head
		point = nil
		while (temp != nil) 
			if (self.coutSetBit(temp.data) % 2 == 0) 
				auxiliary = temp
				#  Visit to next node
				temp = temp.next
				if (auxiliary == self.head) 
					#  Remove head node
					if (auxiliary == self.tail) 
						#  Removing a single node
						self.tail = nil
						self.head = nil
						temp = nil
					else
 
						self.head = temp
						self.tail.next = temp
					end

				elsif(auxiliary == self.tail) 
					if (point != nil) 
						#  When Remove last node
						point.next = self.head
					end

					self.tail = point
				else
 
					if (point != nil) 
						#  When Remove last node
						point.next = temp
					end

				end

				auxiliary = nil
			else
 
				point = temp
				#  Visit to next node
				temp = temp.next
				if (temp == self.head) 
					#  Stop the process
					temp = nil
				end

			end

		end

	end

end

def main() 
	cll = CircularLinkedList.new()
	#  Insert node
	cll.insert(3)
	cll.insert(12)
	cll.insert(32)
	cll.insert(14)
	cll.insert(27)
	cll.insert(19)
	cll.insert(1)
	cll.insert(18)
	print("\n Before Remove ")
	cll.display()
	#    Node  Binary     Even Parity
	#     3     (11)         Yes
	#    12     (1100)       Yes
	#    32     (100000)     No
	#    14     (1110)       No
	#    27     (11011)      Yes
	#    19     (10011)      No
	#    1         (1)       No        
	#    18     (10010)      Yes
	cll.removeNode()
	print("\n\n After Remove")
	cll.display()
end

main()

Output

 Before Remove 
 Linked List Element 
  3  12  32  14  27  19  1  18

 After Remove
 Linked List Element 
  32  14  19  1




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