Insert linked list node at nth last position in go

Go program for Insert linked list node at nth last position. Here problem description and explanation.

package main
import "fmt"
// Go program for
// Insert linked list node at nth last position 
// Linked list node
type LinkNode struct {
    data int
    next * LinkNode
}
func getLinkNode(data int) * LinkNode {
    return &LinkNode {data, nil}
}
type LinkedList struct {
    head * LinkNode
}
func getLinkedList() * LinkedList {
  
    return &LinkedList {nil}
}
// insert node at end position
func(this *LinkedList) insert(value int) {
    // Create a new node
    var node * LinkNode = getLinkNode(value)
    if this.head == nil {
        this.head = node
    } else {
        var temp * LinkNode = this.head
        // Find the last node
        for (temp.next != nil) {
            // Visit to next node
            temp = temp.next
        }
        // Add node
        temp.next = node
    }
}
// Display linked list element
func(this LinkedList) display() {
    if this.head == nil {
        return
    }
    var temp * LinkNode = this.head
    // iterating linked list elements
    for (temp != nil) {
        fmt.Print(temp.data, " → ")
        // Visit to next node
        temp = temp.next
    }
    fmt.Println("NULL")
}
// Add node at specific position from the end of linked list
func(this *LinkedList) endPosition(position, value int) {
    if this.head == nil {
        fmt.Println("Empty Linked list")
    } else if position <= 0 {
        fmt.Println("Invalid position")
    } else {
        var temp * LinkNode = this.head
        var location * LinkNode = nil
        for (temp != nil) {
            position--
            if position <= 0 {
                if location == nil {
                    location = this.head
                } else {
                    location = location.next
                }
            }
            // visit to next node
            temp = temp.next
        }
        if position <= 1 {
            var node * LinkNode = getLinkNode(value)
            if location == nil {
                // Add node at first place
                node.next = this.head
                this.head = node
            } else {
                // Add node at intermediate position
                node.next = location.next
                location.next = node
            }
        } else {
            fmt.Println("Opps position not found")
        }
    }
}
func main() {
    var sll * LinkedList = getLinkedList()
    // Add node
    sll.insert(5)
    sll.insert(4)
    sll.insert(3)
    sll.insert(2)
    sll.insert(1)
    sll.display()
    var position int = 2
    var data int = 10
    sll.endPosition(position, data)
    fmt.Println(" Add ", data, " at last ", 
        position, "-nd position")
    sll.display()
}

Output

5 → 4 → 3 → 2 → 1 → NULL
 Add 10 at last 2-nd position
5 → 4 → 3 → 2 → 10 → 1 → 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







© 2021, kalkicode.com, All rights reserved