In Golang, we can use node struct and linkedlist struct to add elements to a linkedlist. A linkedlist is a data structure made up of a series of nodes, each of which includes an element and a reference to the node after it in the sequence. It is a linear data structure with pointers connecting the items, and from the first node (head) to the last node, each node can be visited (tail). As opposed to arrays, which require all elements to be shifted, linked lists just require altering the pointers of the adjacent nodes, making them helpful in situations where data needs to be added or withdrawn often and dynamically.

## Method 1: Using Node Struct

In this method, the linkedlist is shown as a chain of nodes, where each node is made up of a value and a pointer to the node after it in the list. The add_node function adds a new node holding the value to the end of the list by using a reference to the list's head and a value. The primary function provides examples of traversing and adding members to a list.

### Algorithm

• Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.

• Step 2 − Create a node struct with an integer value and a pointer to the following node.

• Step 3 − Create an add_node function that accepts an integer value and a pointer to the linked list's head as arguments.

• Step 4 − Create a new node with the specified value using the add_node function.

• Step 5 − Assign the new node to the head of the linked list if the head is null.

• Step 6 − Traverse the linked list to the last node if the head of the list is not nil and give the new node the last node's next pointer.

• Step 7 − Return the connected list's head.

• Step 8 − Create a pointer to the linked list's head and initialize it to nil in the main method.

• Step 10 − Print each node's value as you traverse the linked list starting at the head.

• Step 11 − The print statement is executed using fmt.Println() function where ln means new line.

### Example

In this example, we will use node struct to add elements to the linked list. Let’s have a look at the code.

package main
import "fmt"

type node struct {   //define a node struct
value int
next  *node
}

newNode := &node{value: value}
} else {
for current.next != nil {
current = current.next
}
current.next = newNode
}
}

func main() {

for current != nil {
current = current.next
}
}


### Output

The elements added in the linked list are:
10
20
30


## Method 2: Using LinkedList Struct

This example uses a distinct linkedList struct to represent the linked list, which includes a pointer to the head node. A new node holding the given integer value is added to the end of the linked list by the addNode method of the linkedList struct. The linkedList struct's traverse method outputs the values of each node in the list. The main function shows how to build a linked list, add entries to it, and traverse it using its functions.

### Algorithm

• Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.

• Step 2 − Create a node struct with an integer value and a pointer to the following node.

• Step 3 − Create a linkedList struct with a pointer to the linked list's head node.

• Step 4 − Create an addNode method that accepts an integer value for the linkedList struct.

• Step 5 − Create a new node with the specified value using the addNode function.

• Step 6 − Assign the new node to the head of the linked list if the head is null. Then traverse the linked list to the last node if the head of the list is not nil.

• Step 7 − Give the new node the last node's next pointer.

• Step 8 − Make a pointer to a linkedList struct and initialize it in the main function.

• Step 10 − To print the values of each node in the linked list, use the traverse method.

• Step 11 − The print statement is executed using fmt.Println() function where ln means new line.

### Example

In this example, we will use LinkedList struct to add elements to the list.

package main
import "fmt"

type node struct { //define a node struct
value int
next  *node
}

}

newNode := &node{value: value}
} else {
for current.next != nil {
current = current.next
}
current.next = newNode
}
}

for current != nil {
current = current.next
}
}

func main() {
list.traverse()
}


### Output

The elements added to linked list are:
10
20
30


## Conclusion

We executed the program of adding elements in the linked list using two examples. In the first example we used node struct and in the second example we used LinkedList struct.