Golang program to add elements to a linkedlist



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 9 − To add items to the linked list repeatedly while updating the list's head, use the add_node 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
}

func add_node(head *node, value int) *node {
   newNode := &node{value: value}
   if head == nil {
      head = newNode
	} else {
      current := head
      for current.next != nil {
         current = current.next
	  }
      current.next = newNode
   }
   return head
}

func main() {
   fmt.Println("The elements added in the linked list are:")
   var head *node
   head = add_node(head, 10)  //add elements to the list
   head = add_node(head, 20)
   head = add_node(head, 30)

   current := head
   for current != nil {
      fmt.Println(current.value) //print the added values
      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 9 − To add items to the linked list, use the addNode function repeatedly.

  • 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
}

type linkedList struct {
   head *node
}

func (lt *linkedList) addNode(value int) {
   newNode := &node{value: value}
   if lt.head == nil {
      lt.head = newNode
   } else {
      current := lt.head
      for current.next != nil {
         current = current.next
      }
      current.next = newNode
   }
}

func (lt *linkedList) traverse() {
   current := lt.head
   for current != nil {
      fmt.Println(current.value)  //print the added values
      current = current.next
   }
}

func main() {
   list := &linkedList{}
   fmt.Println("The elements added to linked list are:")
   list.addNode(10)  //add values to the list
   list.addNode(20)
   list.addNode(30)
   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.


Advertisements