Posted on by Kalkicode
Code Single linked list

Segregate even and odd nodes in sorted order in golang

Go program for Segregate even and odd nodes in sorted order. Here problem description and other solutions.

package main
import "fmt"
// Go program for
// Segregate even and odd nodes in ascending order

// Linked list node
type LinkNode struct {
	data int
	next * LinkNode
}
func getLinkNode(data int) * LinkNode {
	// return new LinkNode
	return &LinkNode {
		data,
		nil,
	}
}
type SingleLL struct {
	head * LinkNode
}
func getSingleLL() * SingleLL {
	// return new SingleLL
	return &SingleLL {
		nil,
	}
}
// Add new node at the end of linked list
func(this *SingleLL) insert(value int) {
	// Create  node
	var node * LinkNode = getLinkNode(value)
	if this.head == nil {
		this.head = node
	} else {
		var temp * LinkNode = this.head
		// Find last node
		for (temp.next != nil) {
			// Visit to next node
			temp = temp.next
		}
		// Add node at last position
		temp.next = node
	}
}
// Display all Linked List elements
func(this SingleLL) display() {
	if this.head != nil {
		var temp * LinkNode = this.head
		for (temp != nil) {
			// Display node value
			fmt.Print("  ", temp.data)
			// Visit to next node
			temp = temp.next
		}
	} else {
		fmt.Println("Empty Linked list")
	}
}
func(this *SingleLL) sortedAdd(element * LinkNode) {
	if this.head == nil {
		this.head = element
	} else if this.head.data >= element.data {
		element.next = this.head
		this.head = element
	} else {
		var temp * LinkNode = this.head
		// Finding location of inserting node
		for (temp.next != nil && temp.next.data < element.data) {
			// Visit to next node
			temp = temp.next
		}
		// Add node 
		element.next = temp.next
		temp.next = element
	}
}
func(this *SingleLL) segregateNode(odd, even *SingleLL) {
	var node * LinkNode = nil
	// Iterating the linked list node
	for (this.head != nil) {
		node = this.head
		// Visit to next node
		this.head = node.next
		// Set null to next node
		node.next = nil
		if node.data % 2 == 0 {
			// When node value is Even
			even.sortedAdd(node)
		} else {
			// When node value is Odd
			odd.sortedAdd(node)
		}
	}
}
func main() {
	// Create linked lists
	var sll * SingleLL = getSingleLL()
	var odd * SingleLL = getSingleLL()
	var even * SingleLL = getSingleLL()
	// Linked list sll
	// 4 → 3 → 5 → 2 → 11 → 1 → 6 → NULL
	sll.insert(4)
	sll.insert(3)
	sll.insert(5)
	sll.insert(2)
	sll.insert(11)
	sll.insert(1)
	sll.insert(6)
	fmt.Println("Initial Element")
	// Display all node
	sll.display()
	sll.segregateNode(odd, even)
	fmt.Println("\nEven Element")
	// Display all even node
	even.display()
	fmt.Println("\nOdd Element")
	// Display all odd node
	odd.display()
}

Output

Initial Element
  4  3  5  2  11  1  6
Even Element
  2  4  6
Odd Element
  1  3  5  11

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