Created
August 20, 2021 10:43
-
-
Save r14152/5ae2002cad1e085879e069496ee6ff87 to your computer and use it in GitHub Desktop.
link list using golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
//Create prototype | |
//LL container which going to store list | |
type LL struct { | |
list *linklist | |
} | |
//linklist for value and next pointer details | |
type linklist struct { | |
val int | |
next *linklist | |
} | |
// createNode use for create node for list | |
func createNode(value int) *linklist { | |
return &linklist{ | |
val: value, | |
next: nil, | |
} | |
} | |
func (lstVal *LL) insertAtBeginning(data int) { | |
if nil == lstVal.list { | |
lstVal.list = createNode(data) | |
return | |
} | |
tempNode := createNode(data) | |
head := lstVal.list | |
tempNode.next = head | |
lstVal.list = tempNode | |
} | |
func (lstVal *LL) printList() { | |
if nil != lstVal && nil != lstVal.list { | |
head := lstVal.list | |
for nil != head.next { | |
fmt.Println("Node Value: ", head.val) | |
head = head.next | |
} | |
} | |
} | |
func (lstVal *LL) deleteFromBeginning() { | |
if nil != lstVal && nil != lstVal.list { | |
head := lstVal.list | |
lstVal.list = head.next | |
head = nil | |
} | |
} | |
func main() { | |
staticList := []int{1, 2, 3, 4, 5} | |
linklst := new(LL) | |
linklst.list = new(linklist) | |
for _, value := range staticList { | |
linklst.insertAtBeginning(value) | |
} | |
fmt.Println("PrintList") | |
linklst.printList() | |
linklst.deleteFromBeginning() | |
fmt.Println("PrintList") | |
linklst.printList() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment