Linked List data structure

xw19 Published on: 2018-11-18

Linked list data structure in which we store data one after another in linear fashion. Linked list is like railway train in which we have engine and other railway coaches which are one after another, similarly in a linked list we have a head node and then we have nodes attached to head node and another node attached with that node and so on the last node we call it as tail node. A node contains two parts first is for storing data and another is the pointer to the next node.

To create a linked list we first create a head node assign the data to it. Then we create a another node and change pointer of the head node to the new node and create another node we assign data to the new node and change the pointer of the current node to the previous node.

Implemetation of linked list in go is as follows

 

package main

import "fmt"

type (
    // Box data type
    Box interface{}

    // Node
    Node struct {
        data Box
        next *Node
    }

    // LinkedList
    LinkedList struct {
        Head   *Node
        length int
    }
)

//CreateLinkedList create linked list
func CreateLinkedList(data Box) *LinkedList {
    head := &Node{data: data, next: nil}
    linkedList := LinkedList{Head: head, length: 1}
    return &linkedList
}

//Add to linked list
func (ll *LinkedList) Add(data Box) {
    if ll.Head != nil {
        tmp := ll.Head
        for tmp.next != nil {
            tmp = tmp.next
        }
        tmp.next = &Node{data: data, next: nil}
        ll.length++
    } else {
        ll.Head = &Node{data: data, next: nil}
        ll.length = 1
    }
}

//Find to linked list
func (ll *LinkedList) Find(data Box) int {
    index := -1
    if ll.Head != nil {
        tmp := ll.Head
        i := 0
        for tmp.next != nil {
            if tmp.data == data {
                return i
            }
            tmp = tmp.next
            i++
        }
    }
    return index
}

//Delete to linked list
func (ll *LinkedList) Delete(data Box) {
    if ll.Head.data == data {
        if ll.Head.next != nil {
            ll.Head = ll.Head.next
            ll.length--
        }
    }
    if ll.length > 1 {
        tmp := ll.Head.next
        oldTmp := ll.Head
        for tmp != nil {
            if tmp.data == data {
                oldTmp.next = tmp.next
                ll.length--
                return
            }
            oldTmp = tmp
            tmp = tmp.next
        }
    }
}

//Find to linked list
func (ll *LinkedList) Print() {
    if ll.Head != nil {
        tmp := ll.Head
        i := 0
        for tmp != nil {
            fmt.Println("Index ", i, " data ", tmp.data)
            tmp = tmp.next
            i++
        }
    }
}

func main() {
    ll := CreateLinkedList(1)
    ll.Add(2)
    ll.Add(3)
    ll.Add(4)
    ll.Add(5)
    ll.Add(6)
    ll.Add(7)
    ll.Delete(4)
    ll.Print()
    fmt.Println("===========================")
    ll.Delete(1)
    ll.Print()
    fmt.Println("===========================")
    ll.Delete(7)
    ll.Print()
    fmt.Println("===========================")
}

The full source is available at

https://gitlab.com/xw19/linked_list/blob/master/main.go