Golang 将链表转换为数组

Golang 将链表转换为数组

在本文中,我们将学习如何使用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()。

  • 这个函数在链表上定义,并返回整数数组。创建我们希望存储链表值的数组。将列表的当前头部保存到一个变量中。遍历列表直到列表为空,并将当前值附加到数组中。更新当前变量的地址为下一个节点的地址。

  • 现在,开始main()函数,并启动新列表并向其附加值。然后调用ToArray()函数将这些链表值转换为数组,并将其打印在屏幕上。

结论

在本文中,我们成功编写并执行了一个go语言程序,将单链表转换为数组。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程