Skip to main content

Insertion sort on doubly linked list in ruby

Ruby program for Insertion sort on doubly linked list. Here problem description and other solutions.

#  Ruby program for
#  Perform insertion sort on doubly linked list
class LinkNode 
	# Define the accessor and reader of class LinkNode
	attr_reader :data, :next, :prev
	attr_accessor :data, :next, :prev
	def initialize(data) 
		self.data = data
		self.next = nil
		self.prev = nil
	end

end

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

	#  Insert new node at end position
	def insert(value) 
		#  Create a node
		node = LinkNode.new(value)
		if (self.head == nil) 
			#  Add first node
			self.head = node
			self.tail = node
			return
		end

		#  Add node at last position
		self.tail.next = node
		node.prev = self.tail
		self.tail = node
	end

	#  Display node element of doubly linked list
	def display() 
		if (self.head == nil) 
			print("Empty Linked List\n")
		else
 
			print("\nLinked List Head to Tail :")
			#  Get first node of linked list
			temp = self.head
			#  iterate linked list 
			while (temp != nil) 
				#  Display node value
				print("  ", temp.data)
				#  Visit to next node
				temp = temp.next
			end

			print("\nLinked List Tail to Head :")
			#  Get last node of linked list
			temp = self.tail
			#  iterate linked list 
			while (temp != nil) 
				#  Display node value
				print("  ", temp.data)
				#  Visit to prev node
				temp = temp.prev
			end

			print("\n")
		end

	end

	#  Swap value of given node
	def swapData(first, second) 
		value = first.data
		first.data = second.data
		second.data = value
	end

	#  Sort elements using insertion sort
	def insertionSort() 
		#  Get first node
		front = self.head
		back = nil
		while (front != nil) 
			#  Get next node
			back = front.next
			#  Update node value when consecutive nodes are not sort
			while (back != nil && 
                   back.prev != nil && 
                   back.data < back.prev.data) 
				#  Modified node data
				self.swapData(back, back.prev)
				#  Visit to previous node
				back = back.prev
			end

			#  Visit to next node
			front = front.next
		end

	end

end

def main() 
	dll = DoublyLinkedList.new()
	# Insert element of linked list
	dll.insert(25)
	dll.insert(2)
	dll.insert(6)
	dll.insert(14)
	dll.insert(12)
	dll.insert(9)
	dll.insert(16)
	dll.insert(3)
	print("\n Before Sort")
	dll.display()
	#  Display all node
	dll.insertionSort()
	print("\n After Sort")
	dll.display()
end

main()

Output

 Before Sort
Linked List Head to Tail :  25  2  6  14  12  9  16  3
Linked List Tail to Head :  3  16  9  12  14  6  2  25

 After Sort
Linked List Head to Tail :  2  3  6  9  12  14  16  25
Linked List Tail to Head :  25  16  14  12  9  6  3  2




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