Go语言程序:将链表转换为数组
在本文中,我们将学习如何使用 Go 语言程序将链表转换为数组。
链表 - 链表中的元素通常不会存储在彼此靠近的位置,并且它们的存储结构不太严格,它们必须与引用后续元素的其他标签一起存储。链表是一种动态创建的结构,它包含两个元素,一个是存储值,另一个是存储下一个结构的地址。
数组 - 在数组中,元素存储在连续的内存位置,其地址易于计算,从而可以更快地访问给定索引处的元素。只需遍历数组即可访问数组的任何元素。
Go 语言中数组的语法
var array_name[length]Type
要定义一个数组,我们需要定义一个变量,然后是我们要赋予数组的名称,然后是它应该包含的元素的大小,最后是数组应该包含的数据类型。
Go 语言中链表的语法
type name_of_list struct {
variable type
pointer
}
链表以 type 关键字开头,表示我们正在定义一个新类型,然后是它的名称和 struct 关键字,然后我们需要定义一个变量来存储数据,以及一个指针变量来存储节点的地址。
示例
将链表转换为数组的 Go 语言程序代码。
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)
}
输出
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]
代码描述
首先,我们需要导入 fmt 包,该包允许我们在屏幕上打印任何内容。
然后,我们需要定义一个名为 node 的新结构,它将包含数据以及指向下一个节点的地址。
然后,我们创建一个 LinkedList 结构,它包含列表的长度以及指向链表头节点的指针。头部是列表的当前第一个元素,从这里开始列表。
然后,我们需要定义一些函数。第一个函数是 initlist(),它返回链表的地址;还有一个 size 函数,它将提供链表的大小。
我们还需要一个函数,以便每次我们想要添加新元素时,都在链表的开头添加一个新节点,为此,我们创建了一个 prepend() 函数。
此函数将接收一个整数作为参数,并将该值更新到节点的数据元素,并更新头部。
接下来,我们需要一个函数将链表值转换为数组,为此,我们定义了 ToArray()。
此函数在链表上定义,并返回整数数组。创建我们希望将链表值存储其中的数组。将列表的当前头部存储到名为 current 的变量中。遍历列表直到它为空,并将当前值附加到列表的数组中。使用下一个节点的地址更新 current 变量的地址。
现在,开始 main() 函数,开始新的列表并向其中追加值。然后调用 ToArray() 函数将这些链表值转换为数组,并在屏幕上打印它们。
结论
在本文中,我们成功地编译并执行了一个 Go 语言程序,用于将单链表转换为数组。
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP