Home  >  Article  >  Backend Development  >  How to add a list to a list in go language

How to add a list to a list in go language

青灯夜游
青灯夜游Original
2023-01-17 15:20:551615browse

In the go language, you can use the PushFrontList() function and PushBackList() function to add a list to the list. The PushFrontList() function can insert another list at the head of the list, the syntax is "list variable.PushFrontList(list to be inserted)"; the PushBackList() function can insert another list at the end of the list, the syntax is "list variable.PushBackList(to be inserted) list of)".

How to add a list to a list in go language

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

In addition to supporting the insertion of elements, Golang's list can also insert the entire list into another list. Inserting a list into another list only supports two situations: inserting a list at the head and inserting a list at the tail.

Insert a list at the head

In Go, you can use the PushFrontList() function to insert another list at the head of the list.

Syntax

PushFrontList(other *List)
##ParametersDescription#The list to insert.
other
Instructions:

  • Insert the list other at the head of the list.

Example: Use PushFrontList to insert a list at the head of the list

package main
import (
	"container/list"
	"fmt"
)
func main() {
	//使用 PushFrontList 在列表头部插入一个列表
	listHaiCoder := list.New()
	listHaiCoder.PushFront("Hello")
	listHaiCoder.PushFront("HaiCoder")
	listInsert := list.New()
	listInsert.PushBack("你好")
	listInsert.PushBack("hi")
	listHaiCoder.PushFrontList(listInsert)
	for i := listHaiCoder.Front(); i != nil; i = i.Next() {
		fmt.Println("Element =", i.Value)
	}
}

How to add a list to a list in go language

Analysis:

  • We created two lists, listHaiCoder and listInsert, through list.New, and then used the PushFront function and PushBack function to insert elements into the two lists respectively.

  • Finally, we use PushFrontList to insert all elements of listInsert into the head of listHaiCoder. Finally, we print the final list and find that the elements of listInsert are ranked at the top of the entire list. Front.

Insert a list at the end

In Go, you can use the PushBackList() function to insert another list at the end of the list. a list.

Syntax

PushBackList(other *List)

Instructions:

  • Insert the list other at the end of the list.

Example: Use PushBackList to insert a list at the end of the list

package main
import (
	"container/list"
	"fmt"
)
func main() {
	//使用 PushBackList 在列表尾部插入一个列表
	listHaiCoder := list.New()
	listHaiCoder.PushFront("Hello")
	listHaiCoder.PushFront("HaiCoder")
	listInsert := list.New()
	listInsert.PushBack("你好")
	listInsert.PushBack("hi")
	listHaiCoder.PushBackList(listInsert)
	for i := listHaiCoder.Front(); i != nil; i = i.Next() {
		fmt.Println("Element =", i.Value)
	}
}

How to add a list to a list in go language

[Related recommendations:

Go video tutorialprogramming teaching

The above is the detailed content of How to add a list to a list in go language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn