Golang Program to Convert List to Map


In this tutorial, we will write a golang program to convert a list to map. A linked list is a structure that is created dynamically it has two elements one to store the value and other to store the address of the next structure. A map store element in key:value pairs. A map is an unsorted, flexible collection that forbids duplications.

Convert A List to Map

In this article, we will discuss to convert a linked list to map. The following program illustrates this conversion process.

Syntax

func make([]type, length, capacity) []type

The make function is used to make a slice or map. It takes three arguments one is the slice name and type that we wish to make followed by the length and capacity of the slice. The function then returns the final slice.

Algorithm

Step 1 − First, we need to import the fmt package.

Step 2 − The next step is to create a node. For that we are defining a new structure called node.

Step 3 − Then we need to create a linkedlist struct. It also has two fields one is the pointer variable called head. This variable points to the head node

Step 4 − Then we have initialized two functions one is the initlist() function. This function is defined on the linkedlist struct and returns the address of the linkedlist{}.

Step 5 − Create a second function naming as prepend and it is used to add a new node element to the linked list and takes a node as an argument.

Step 6 − Once the data is added to the current node of linked list we need to make the next pointer of linked list to point to the next node and increment the length of linked list.

Step 7 − Start the main function and create a new linked list named mylist by calling the initlist() function.

Step 8 − After that we have created couple of nodes and stored string values to them.

Step 9 − The next step is to arrange these nodes together to form a linked list for that we are calling prepend() function by passing each node as an argument to the function.

Step 10 − Then we need to create a map. We have named it as newmap and it stores data in key value pairs in int and string format.

Step 11 − The next step is to iterate over the linked list and on each iteration we are extracting keys and data from the linked list and are storing it on the set.

Step 12 − Then we need to make the pointer variable of linked list to point to the next node and repeat this process until we get the nil or null value.

Step 13 − The next step is to print the map on the screen using fmt.Println() function.

Example

package main
import (
   "fmt"
)
type node struct {
   data string
   key int
   next *node
}
type linkedlist struct {
   len int
   head *node
}
func initList() *linkedlist {
   return &linkedlist{}
}

// function to add a new node to the linked list
func (l *linkedlist) prepend(n *node) {
   node := &node{
      data: n.data,
      key: n.key,
   }
   if l.head == nil {
      l.head = node
   } else {
      node.next = l.head
      l.head = node
   }
   l.len++
   return
}
func main() {
   mylist := initList()
   node1 := &node{key: 1, data: "Sunday"}
   node2 := &node{key: 2, data: "Monday"}
   node3 := &node{key: 3, data: "Tuesday"}
   mylist.prepend(node1)
   mylist.prepend(node2)
   mylist.prepend(node3)
   newmap := make(map[int]string)
   for mylist.head != nil {
      newmap[mylist.head.key] = mylist.head.data
      mylist.head = mylist.head.next
   }
   fmt.Println("The obtained map from linked list is:", newmap)
}

Output

The obtained map from linked list is: map[2:Monday 1:Sunday 3:Tuesday]

Conclusion

We have successfully compiled and executed a go language program to covert a linked list to map along with example.

Updated on: 28-Dec-2022

943 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements