Delete all nodes whose node digit sum is odd from circular linked list in golang
Go program for Delete all nodes whose node digit sum is odd from circular linked list. Here more information.
package main
import "fmt"
// Go Program
// Remove all the Odd digit sum nodes from a circular linked list
type LinkNode struct {
data int
next * LinkNode
}
func getLinkNode(data int) * LinkNode {
// return new LinkNode
return &LinkNode {
data,
nil,
}
}
type CircularLinkedList struct {
head * LinkNode
tail * LinkNode
}
func getCircularLinkedList() * CircularLinkedList {
// return new CircularLinkedList
return &CircularLinkedList {
nil,
nil,
}
}
//Add node at the end position efficiently
func(this *CircularLinkedList) insert(value int) {
// Create a new node
var node * LinkNode = getLinkNode(value)
if this.head == nil {
// When first node
this.head = node
} else {
// Connect new node at the last
this.tail.next = node
}
// Connect last node to first node
node.next = this.head
// Set new last node
this.tail = node
}
// Display node element of circular linked list
func(this CircularLinkedList) display() {
if this.head == nil {
fmt.Println("\n Empty Linked List")
} else {
// First node of linked list
fmt.Print(" ", this.head.data)
var temp * LinkNode = this.head.next
// Display linked list node
for (temp != nil && temp != this.head) {
// Display node value
fmt.Print(" ", temp.data)
// visit to next node
temp = temp.next
}
}
}
// Sum of digits
func(this CircularLinkedList) digitSum(num int) int {
var n int = num
var sum int = 0
if n < 0 {
// When node value is negative
n = -n
}
for (n > 0) {
sum += (n % 10)
n = n / 10
}
return sum
}
func(this *CircularLinkedList) removeOddDigitSumNode() {
if this.head == nil {
return
}
var temp * LinkNode = this.head
var auxiliary * LinkNode = this.head
var point * LinkNode = nil
for (temp != nil) {
if (this.digitSum(temp.data) % 2) != 0 {
auxiliary = temp
temp = temp.next
if auxiliary == this.head {
// Enter to remove first node
if auxiliary == this.tail {
// Enter to removing single node
this.tail = nil
this.head = nil
temp = nil
point = nil
} else {
this.head = temp
this.tail.next = temp
}
} else if auxiliary == this.tail {
// When Remove last node
if point != nil {
point.next = this.head
}
this.tail = point
} else {
// When removing intermediate node
if point != nil {
point.next = temp
}
}
auxiliary = nil
} else {
point = temp
// Visit to next node
temp = temp.next
if temp == this.head {
// Stop the process
temp = nil
}
}
}
}
func main() {
var cll * CircularLinkedList = getCircularLinkedList()
// Insert node
cll.insert(131)
cll.insert(12)
cll.insert(33)
cll.insert(10)
cll.insert(143)
cll.insert(91)
cll.insert(27)
cll.insert(125)
cll.insert(89)
fmt.Println("\n Before remove odd digit sum nodes")
cll.display()
/*
Node Digit Sum Odd
----- ----------- --------
131 [1+3+1] 5 Yes
12 [1+2] 3 Yes
33 [3+3] 6 No
10 [10] 1 Yes
143 [1+4+3] 8 No
91 [9+1] 10 No
27 [2+7] 9 Yes
125 [1+2+5] 8 No
89 [8+9] 17 Yes
--------------------------
Result
------
33 → 143 → 91 → 125 ┐
└-----------------⤶
*/
cll.removeOddDigitSumNode()
fmt.Println("\n After remove odd digit sum nodes")
cll.display()
}
Output
Before remove odd digit sum nodes
131 12 33 10 143 91 27 125 89
After remove odd digit sum nodes
33 143 91 125
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