在这篇文章中,我们将学习如何使用golang程序将链接列表转换为数组。
链接列表 --链接列表中的元素通常不是彼此紧挨着存储的,它们的存储结构也不那么僵硬,它们必须用额外的标签来存储,以指代后面的元素。链接列表是一个动态创建的结构,它有两个元素,一个是存储值,另一个是存储下一个结构的地址。
数组 --在数组中,元素被存储在连续的内存位置,其地址很容易计算,允许在给定的索引上更快地访问一个元素。通过对数组的索引,可以访问数组中的任何元素。
Golang中数组的语法
var array_name[length]Type
要定义一个数组,我们需要定义一个变量,然后是我们希望给出的数组的名称,然后是它应该包含的元素的大小,最后是数组应该包含的数据类型。
Golang中关联列表的语法
type name_of_list struct {
variable type
pointer
}
一个链接列表以 type 关键字开始,表明我们正在定义一个新的类型,然后是它的名称和 struct 关键字,然后我们需要定义变量来存储数据,以及一个指针变量来存储节点的地址。
例子
Golang程序代码将一个链接列表转换为一个数组。
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 的变量中。遍历列表,直到它为空,并将带有当前值的数组追加到列表中。用下一个节点的地址更新当前变量的地址。
-
现在,启动main()函数开始新的列表并向其追加值。然后调用ToArray()函数将这些链接列表的值转换为数组并打印在屏幕上。
总结
在这篇文章中,我们已经成功地编译并执行了一个go语言程序,将一个单链表转换为数组。