- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program to Convert Linked list to an Array
In this article we are going to learn about how to convert linked list to an array using golang program.
Linked List − Elements in linked lists are typically not stored in close proximity to one another and their storage structures are less rigid, they must be stored with additional tags that refer to the subsequent element. A linked list is a structure that is created dynamically it has two elements in it one is to store the value and the other is to store the address of the next structure.
Array − In arrays elements are stored in contiguous memory locations with addresses that are simple to calculate, allowing for quicker access to an element at a given index. It is possible to access any element of the array just by indexing over the array.
Syntax of an Array in Golang
var array_name[length]Type
To define an array we need to define a variable followed by the name of the array we wish to give then the size of elements that it should contain followed by the data type that the array should contain.
Syntax of a Linked List in Golang
type name_of_list struct { variable type pointer }
A linked list starts with the type keyword that suggests that we are defining a new type followed by its name and struct keyword then we need to define the variable to store the data and a pointer variable to store the address of the node.
Example
Golang program code to convert a linked list to an array.
package main // fmt package allows us to print anything on the screen import "fmt" // describing a node that contains data and the address of the next node type node struct { data int next *node } // defining a type LinkedList that contains the address of the head node and the length of the node type linkedlist struct { len int head *node } // function to get the address of the linked list func initList() *linkedlist { return &linkedlist{} } // function to add a new node func (l *linkedlist) prepend(data int) { node := &node{ data: data, } if l.head == nil { l.head = node } else { node.next = l.head l.head = node } l.len++ return } // function to get the size of the linked list func (s *linkedlist) Size() int { return s.len } // Function to convert a singly linked list to an array func (s *linkedlist) ToArray() []int { // creating an array of integers named myarr var myarr []int // storing the first address of the list to a variable called the current current := s.head // traversing over the list until it is empty and appending the current value to the array for current.next != nil { fmt.Printf("\nAdding Element to array: %d", current.data) myarr = append(myarr, current.data) // updating the address of the current variable with the address of the next node current = current.next } fmt.Printf("\nAdding Element to array: %d", current.data) myarr = append(myarr, current.data) // returning the array thus formed return myarr } func main() { // creating a new list named mylist mylist := initList() // adding elements to the linked list fmt.Printf("converting the below elements into array") mylist.prepend(100) mylist.prepend(200) mylist.prepend(300) mylist.prepend(400) // calling the ToArray() function to convert values of the linked list myarr := mylist.ToArray() fmt.Printf("\nThe size of the linked list is: %d\n", mylist.Size()) // printing the final array fmt.Println("\nThe final array obtained from the linked list is:", myarr) }
Output
converting the below elements into array Adding Element to array: 400 Adding Element to array: 300 Adding Element to array: 200 Adding Element to array: 100 The size of the linked list is: 4 The final array obtained from the linked list is: [400 300 200 100]
Description of The Code
First, we need to import the fmt package that allows us to print anything on the screen.
Then we need to define a new structure named node that will contain the data as well as the address to the next node.
Then we create a LinkedList struct that contains the length of the list as well as the pointer to the head node of the linked list. The head is the current first element of the list from where the list gets started.
Then we need to define some functions. The first function is the initlist() which returns the address of the linked list and a size function that will give us the size of the linked list.
We also need a function to prepend a new node at the start of the linked list every time we want to add a new element for that we have created a prepend() function.
This function will receive an integer value as the parameter and will update that value to the data element of the node and will update the head.
Next we need a function to convert the linked list values to array for this we have defined the ToArray().
This function is defined on the linked list and returns the array of integers. Create the array in which we wish to store the values of the linked list. Store the current head of the list to a variable called current. Traverse over the list until it is empty and append the array with the current value to the list. update the address of the current variable with the address of the next node.
Now, start the main() function start the new list and append values to it. Then call the ToArray() function to convert these linked list values to the array and print them on the screen.
Conclusion
In this article, we have successfully complied and executed a go language program to convert a singly linked list to an array.
- Related Articles
- Golang program to convert a linked list into an array and vice-versa
- Golang program to implement linked list
- Golang Program to Convert String to an Array
- Golang program to join two linked list
- Convert an Array to a Circular Doubly Linked List in C++
- Golang Program to define a singly linked list.
- Golang Program to reverse a given linked list.
- Golang program to reverse a circular linked list
- Java program to convert a list to an array
- Java program to convert an array to a list
- Golang Program to Convert List to Map
- Golang Program to Convert List to Set
- Golang Program To Convert Array To Set
- Golang program to remove elements from the linked list
- Golang program to access elements from a linked list
