go 语言 list 循环
时间: 2023-11-27 13:06:07 浏览: 37
你好,我明白了您的要求。下面是您的回答:
在 Go 语言中,我们可以使用 for 循环配合 range 关键字对切片、数组、字符串等类型的序列进行遍历。示例代码如下:
```
package main
import "fmt"
func main() {
list := []string{"apple", "banana", "cherry", "durian"}
for i, fruit := range list {
fmt.Printf("index: %d, value: %s\n", i, fruit)
}
}
```
这段代码中,list 变量是一个字符串切片,我们使用 for 循环和 range 关键字对其进行遍历。在每一次循环中,i 变量代表当前元素的下标,fruit 变量代表当前元素的值。该程序会输出以下结果:
```
index: 0, value: apple
index: 1, value: banana
index: 2, value: cherry
index: 3, value: durian
```
希望对您有所帮助。如果您有其他问题,欢迎继续提问。
相关问题
go语言实现循环单链表
以下是使用Go语言实现循环单链表的示例代码:
```go
package main
import (
"fmt"
)
type Node struct {
data interface{}
next *Node
}
type CircularLinkedList struct {
head *Node
tail *Node
size int
}
func (list *CircularLinkedList) getSize() int {
return list.size
}
func (list *CircularLinkedList) isEmpty() bool {
return list.head == nil
}
func (list *CircularLinkedList) addFirst(data interface{}) {
newNode := &Node{data, nil}
if list.isEmpty() {
list.head = newNode
list.tail = newNode
newNode.next = newNode
} else {
newNode.next = list.head
list.head = newNode
list.tail.next = newNode
}
list.size++
}
func (list *CircularLinkedList) addLast(data interface{}) {
newNode := &Node{data, nil}
if list.isEmpty() {
list.head = newNode
list.tail = newNode
newNode.next = newNode
} else {
newNode.next = list.head
list.tail.next = newNode
list.tail = newNode
}
list.size++
}
func (list *CircularLinkedList) removeFirst() interface{} {
if list.isEmpty() {
return nil
}
data := list.head.data
list.head = list.head.next
list.tail.next = list.head
list.size--
if list.isEmpty() {
list.tail = nil
}
return data
}
func (list *CircularLinkedList) display() {
if list.isEmpty() {
fmt.Println("List is empty")
return
}
fmt.Println("List:")
currentNode := list.head
for {
fmt.Printf("%v -> ", currentNode.data)
currentNode = currentNode.next
if currentNode == list.head {
break
}
}
fmt.Println()
}
func main() {
list := &CircularLinkedList{}
list.addFirst(1)
list.addLast(2)
list.addLast(3)
list.addFirst(0)
list.display()
fmt.Println("Size:", list.getSize())
list.removeFirst()
list.removeFirst()
list.display()
fmt.Println("Size:", list.getSize())
}
```
输出结果:
```
List:
0 -> 1 -> 2 -> 3 ->
Size: 4
List:
2 -> 3 ->
Size: 2
```
该代码实现了循环单链表的基本功能,包括添加节点、删除节点、获取链表大小和显示链表内容等。在该示例中,循环单链表的头节点和尾节点都是存储在链表对象中的,而不是每个节点中都保存头节点和尾节点的指针。同时,该示例还实现了判断链表是否为空的方法。
go语言实现循环双链表
package main
import "fmt"
type Node struct {
Prev *Node
Next *Node
Value interface{}
}
type List struct {
Head *Node
Tail *Node
Len int
}
func NewList() *List {
l := &List{}
l.Head = &Node{}
l.Tail = &Node{}
l.Head.Next = l.Tail
l.Tail.Prev = l.Head
return l
}
func (l *List) Len() int {
return l.Len
}
func (l *List) PushBack(v interface{}) {
n := &Node{Prev: l.Tail.Prev, Next: l.Tail, Value: v}
n.Prev.Next = n
n.Next.Prev = n
l.Len++
}
func (l *List) PushFront(v interface{}) {
n := &Node{Prev: l.Head, Next: l.Head.Next, Value: v}
n.Prev.Next = n
n.Next.Prev = n
l.Len++
}
func (l *List) Remove(n *Node) {
n.Prev.Next = n.Next
n.Next.Prev = n.Prev
l.Len--
}
func (l *List) Print() {
for n := l.Head.Next; n != l.Tail; n = n.Next {
fmt.Print(n.Value, " ")
}
fmt.Println()
}
func main() {
l := NewList()
l.PushBack(1)
l.PushBack(2)
l.PushBack(3)
l.PushFront(0)
l.Print()
for n := l.Head.Next; n != l.Tail; {
next := n.Next
fmt.Println(n.Value)
l.Remove(n)
n = next
}
fmt.Println(l.Len())
}