Golang program to convert a linked list into an array and vice-versa


In the Go programming language, a linkedlist is a data structure made up of a series of nodes, each of which has a value and a reference (pointer) to the node after it. Since items can be added to or removed from a list without rearranging the entire data set, linked lists offer a dynamic and adaptable approach to store data. Using structs and pointers, linked lists can be implemented in Go whereas array is a fixed-size group of identical elements that may be accessed by their respective indexes, which are integers with a zero-based basis. An array's size is predetermined at the moment of declaration and cannot be altered later on.

Method 1: Converting linked list into array

In this method, we converted linked list into array using using the node struct. The value is set in the linked list and converted into array which is created using make function. The loop is run over the linked list and the elements are appended into array and then printed on the console using fmt package.

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 struct of type node containing val of type int and next of type Node.

  • Step 3 − Create a blank array named array.

  • Step 4 − Make a pointer called curr and initialize it to the linked list's head.

  • Step 5 − until curr is not nil run a loop with the command curr.next.

  • Step 6 − Return the array to the linked list.

Example

In this example, we will convert linked list to array.

package main
import "fmt"
type Node struct {
   val  int
   next *Node
}

func main() {
   // Creating linked list
   head := &Node{val: 10, next: &Node{val: 20, next: &Node{val: 30, next: nil}}}

   // Converting linked list to an array
   array := make([]int, 0)
   for curr := head; curr != nil; curr = curr.next {
      array = append(array, curr.val)
   }
   fmt.Println("The linked list is converted into array:")
   fmt.Println("Linked List:", array)
}

Output

The linked list is converted into array:
Linked List: [10 20 30]

Method 2: Converting array into linked list

In this method, we converted the array into linked list with the help of a function array_to_List in which a loop will be run and the head value will be returned to the function. The list will be printed on the console using fmt package.

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 − Make a head node called head with the value of the array's first element.

  • Step 3 − Create a pointer curr and set its initial value to head. Then, iterate through the array's remaining elements.

  • Step 4 − Add a new node. newNode with the current element's value

  • Step 5 − Move the curr pointer on newNode.

  • Step 6 − In the next step, return the head to the function.

Example

In this example, we will convert array to linked list.

package main
import "fmt"

type Node struct {
   val  int
   next *Node
}

func array_to_List(array []int) *Node {
   head := &Node{val: array[0], next: nil}
   curr := head
   for i := 1; i < len(array); i++ {
      curr.next = &Node{val: array[i], next: nil}
      curr = curr.next
   }
   return head
}

func main() {
   // Creating array
   array := []int{10, 20, 30}

   // Converting array to linked list
   head := array_to_List(array)

   // Printing linked list
   fmt.Println("The array after its converted to linked list:")
   for curr := head; curr != nil; curr = curr.next {
      fmt.Print(curr.val, " -> ")
   }
   fmt.Print("nil")
}

Output

The array after its converted to linked list:
10 -> 20 -> 30 -> nil

Conclusion

We executed the program of converting the linked list into array and vice-versa using two examples. In the first example we used we converted linked list to array using array into linked list. Both the programs generated desired output.

Updated on: 21-Feb-2023

253 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements