Golang program for implementation of Linked List
A singly unsorted linked list is a data structure consisting of a sequence of nodes, where each node contains a value and a reference (a pointer or a link) to the next node in the sequence. In a singly linked list, each node only has a reference to the next node, and there is no reference to the previous node. In an unsorted linked list, the order of the nodes does not matter, and they can be inserted or removed at any position in the list.
To access a particular node in the list, we start at the head node (the first node in the list) and follow the links until we reach the desired node. If a node has a null reference for its next node, it is considered the last node in the list.
Singly unsorted linked lists are a useful data structure for implementing many algorithms, such as searching, sorting, and graph traversal algorithms. However, they are less efficient than arrays or other data structures for accessing elements by index, since we have to traverse the list from the beginning to find a particular element.
Example
package main
import (
"fmt"
)
type Node struct {
data int
next *Node
}
type List struct {
head *Node
}
func (l *List) add(value int) {
newNode := &Node{data: value}
if l.head == nil {
l.head = newNode
return
}
curr := l.head
for curr.next != nil {
curr = curr.next
}
curr.next = newNode
}
func (l *List) remove(value int) {
if l.head == nil {
return
}
if l.head.data == value {
l.head = l.head.next
return
}
curr := l.head
for curr.next != nil && curr.next.data != value {
curr = curr.next
}
if curr.next != nil {
curr.next = curr.next.next
}
}
func main() {
list := &List{}
list.add(1)
list.add(2)
list.add(3)
list.add(4)
fmt.Println("Initial List: ")
printList(list)
list.remove(2)
fmt.Println("List after removing 2: ")
printList(list)
list.remove(4)
fmt.Println("List after removing 4: ")
printList(list)
}
func printList(l *List) {
curr := l.head
for curr != nil {
fmt.Printf("%d ", curr.data)
curr = curr.next
}
fmt.Println()
}
First, we define two structs: Node and List. The Node struct represents a single node in the linked list, containing the data and a pointer to the next node. The List struct contains a pointer to the head node.
Next, we define the add function, which takes a value and adds it to the end of the list. If the list is empty, the new node becomes the head node. Otherwise, we traverse the list to find the last node, and then append the new node to it.
Then, we define the remove function, which takes a value and removes the first occurrence of it from the list. If the list is empty, or if the value is not found, the function does nothing. If the value is found at the head node, we simply update the head pointer. Otherwise, we traverse the list to find the node before the one to be removed, and then update its next pointer to skip over the node to be removed.
Finally, in the main function, we create a new list and add some nodes to it. We then print the initial list, remove a couple of nodes, and print the list again to see the changes.
The printList function simply prints out the values in the list, to make it easier to see what's going on.