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 語言程式,用於將單鏈錶轉換為陣列。

更新於: 2022-12-22

583 次瀏覽

啟動您的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.