Golang program to convert vector to a list


In Golang, a vector is referred to as an array or a slice. Slice is a dynamic array whose size can be changed whereas a linked list data structure is one type of list, where each node has a value and is pointing to the next element. We will use two examples in this program to convert vector to a list. In the very first example a built-in copy function will be used and in the second example we will iterate the slice in the reverse order to obtain the output.

Method 1: Using Copy Function

In this method, we created a vector which will be converted to a list using copy and make function. The copy function will copy the vector elements to the newly made list using make function.

Syntax

func copy(dst, str[] type) int

The copy function in go language is used to copy the values of one source array to the destination array and returns the number of elements copied as the result. It takes the two arrays as an argument.

func make ([] type, size, capacity)

The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments

func len(v Type) int

The len() function is used to get the length of a any parameter. It takes one parameter as the data type variable whose length we wish to find and returns the integer value which is the length of the variable.

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 an int slice vector with some values.

  • Step 3 − In the next step, use the make function to generate a new slice list that is the same length as the vector.

  • Step 4 − Now, use the copy function to add the vector's items to a list.

  • Step 5 − Print the new list and the vector list on the console using fmt.Println() function where ln means new line.

  • Step 6 − It should be noted that in Go, a slice is a dynamic array-like data structure, and making a duplicate of an existing slice usually involves making a new slice with the same length as the original slice. The elements are copied from one slice to another using the copy function.

Example

In this example we will use copy function to convert vector to a list. Let’s see through the code how it can be done.

package main
import (
   "fmt"
)

func main() {
   slice := []int{10, 20, 30, 40, 50}  //create a vector

   // convert slice to list
   list := make([]int, len(slice))
   copy(list, slice)  //use copy function to copy the elements of a slice to a list

   fmt.Println("Vector:", slice)  //print the vector
   fmt.Println("The conversion of slice to list looks like:")
   fmt.Println("List:", list)  //print the list
}

Output

Vector: [10 20 30 40 50]
The conversion of slice to list looks like:
List: [10 20 30 40 50]

Method 2: Using Linked List

This method generates a linked list by iterating through the slice in reverse order and constructing a new node for each element in the slice, followed by the node that came before it.

Syntax

func len(v Type) int

The len() function is used to get the length of a any parameter. It takes one parameter as the data type variable whose length we wish to find and returns the integer value which is the length of the variable.

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 Node struct type with two members named Value and Next that are both of the types int and *Node, respectively.

  • Step 3 − Make a slice of ints and create the type *Node variable head, and set its initial value to nil.

  • Step 4 − Reverse-loop across the slice of integers.

  • Step 5 − Create a new node for each element in the slice by using the Node struct and setting the Value field to the element that is now in the slice and the Next field to the node that was previously created.

  • Step 6 − Create a new node and assign it to the head variable.

  • Step 7 − For every component of the slice, repeat steps 5 and 6.

  • Step 8 − Follow the Next pointers to navigate the linked list, then output each node's value.

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

Example

In this example we will use linked list to convert vector to list. Let’s see through the code to know its execution.

package main
import "fmt"

type Node struct {
   Value int
   Next  *Node
}

func main() {
   // Create a slice of ints
   vector_val := []int{10, 20, 30, 40, 50}  //create a vector
   fmt.Println("The initially created vector is:", vector_val)

   // Convert the slice to a linked list
   var head *Node
   for i := len(vector_val) - 1; i >= 0; i-- {
      head = &Node{vector_val[i], head}  //convert the vector to list
   }

   // Print the linked list
   fmt.Println("The elements of list are:")
   for node := head; node != nil; node = node.Next {
      fmt.Println(node.Value)  //print the values of list
   }
}

Output

The initially created vector is: [10 20 30 40 50]
The elements of list are:
10
20
30
40
50

Conclusion

We executed the program of converting vector to a list using two methods. In the first example we used built-in copy function and in the second example we used linked list to carry on the execution.

Updated on: 20-Feb-2023

615 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements