Move vowels node at the beginning of linked list in golang
Go program for Move vowels node at the beginning of linked list. Here problem description and other solutions.
package main
import "fmt"
/*
Go program for
Shift the vowels node at beginning of linked list
*/
// Linked list node
type LinkNode struct {
data byte
next * LinkNode
}
func getLinkNode(data byte) * LinkNode {
// return new LinkNode
return &LinkNode {
data,
nil,
}
}
type SingleLL struct {
head * LinkNode
tail * LinkNode
}
func getSingleLL() * SingleLL {
// return new SingleLL
return &SingleLL {
nil,
nil,
}
}
// Add new node at end of linked list
func(this *SingleLL) addNode(data byte) {
var node * LinkNode = getLinkNode(data)
if this.head == nil {
this.head = node
} else {
// Append the node at last position
this.tail.next = node
}
this.tail = node
}
// Display linked list element
func(this SingleLL) display() {
if this.head == nil {
fmt.Println("\n Empty linked list")
return
}
var temp * LinkNode = this.head
// iterating linked list elements
for (temp != nil) {
fmt.Printf(" %c →", temp.data)
// Visit to next node
temp = temp.next
}
fmt.Print(" NULL\n")
}
// Determine that given value is an vowel or not
func(this SingleLL) isVowels(value byte) bool {
if value == 'a' || value == 'e' ||
value == 'i' || value == 'o' ||
value == 'u' || value == 'A' ||
value == 'E' || value == 'I' ||
value == 'O' || value == 'U' {
return true
}
return false
}
// Move the vowels value at beginning position
func(this *SingleLL) shiftVowels() {
if this.head == nil {
fmt.Print("\n Empty linked list\n")
return
}
// Define some auxiliary variable
var node * LinkNode = this.head
var back * LinkNode = nil
// iterate linked list nodes
// And shift vowels at front of linked list
for (node != nil) {
if this.isVowels(node.data) == true && back != nil {
// Enter here when
// Node is contain vowel and
// its previous (any) node is not form of vowel
if node == this.tail {
// When is last node
// Set new last node
this.tail = back
}
// Connect previous node to next node
back.next = node.next
// Connect vowel node to first node
node.next = this.head
// Set new head
this.head = node
// Get back node
node = back
} else {
back = node
}
// Visit to next node
node = node.next
}
}
func main() {
var sll * SingleLL = getSingleLL()
// Constructed linked list
sll.addNode('E')
sll.addNode('D')
sll.addNode('U')
sll.addNode('C')
sll.addNode('A')
sll.addNode('T')
sll.addNode('I')
sll.addNode('O')
sll.addNode('N')
fmt.Println(" Before move vowels")
// E → D → U → C → A → T → I → O → N → NULL
sll.display()
sll.shiftVowels()
fmt.Println(" After move vowels")
// O → I → A → U → E → D → C → T → N → NULL
sll.display()
}
Output
Before move vowels
E → D → U → C → A → T → I → O → N → NULL
After move vowels
O → I → A → U → E → D → C → T → N → NULL
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