Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How To Implement Linked List in GoLang

How To Implement Linked List In GoLang

This tutorial help to implement single linked list implementation using golang, As we know, Data structure is the bone of the computer science, Linked is the linear collection of data elements.

Linked List each Node has address of the next node.If there is single node then next node address will nil.

We will create two struct one is for node and other for node list, Lets create node struct in golang:

type Node struct {
	num int
	next *Node
}

the struct have node value and next node pointer value.

We will create node list struct in Golang.

type List struct {
	length int
	start *Node
}

This will contains all node list, first is the length of list, You can omit this params from this struct. The start have the first node address.

Golang Code To Implement Linked List

The below is the simple code to add element into list and display them.

package main

import (
	"fmt"
	"math/rand"
	)

type Node struct {
	num int
	next *Node
}

type List struct {
	length int
	start *Node
}



func main() {
	items := &List{}
	size := 10
    //rand_number := make([]int, size, size)
 	for i := 0; i ", list.num)
       list = list.next
    }
    fmt.Println()
}

func (l *List)insertNode(newNode *Node) {
	if l.length == 0 {
		l.start = newNode
	} else {
		currentNode :=  l.start
		for currentNode.next != nil {
			currentNode = currentNode.next
		}
		currentNode.next = newNode
	}

	l.length++
}

The post How To Implement Linked List in GoLang appeared first on Rest Api Example.



This post first appeared on Rest Api Example, please read the originial post: here

Share the post

How To Implement Linked List in GoLang

×

Subscribe to Rest Api Example

Get updates delivered right to your inbox!

Thank you for your subscription

×