main.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //https://books.studygolang.com/The-Golang-Standard-Library-by-Example/chapter03/03.3.html
  2. package main
  3. import (
  4. "container/list"
  5. "fmt"
  6. "reflect"
  7. )
  8. func main() {
  9. fmt.Println("hello")
  10. // insertPushList()
  11. listAddrness()
  12. }
  13. func insertPushList() {
  14. // 创建一个新的链表,并向链表里面添加几个数字
  15. l := list.New()
  16. fmt.Println(l)
  17. e4 := l.PushBack(4)
  18. fmt.Println("e4", e4.Value, &e4, e4)
  19. e1 := l.PushFront(1)
  20. fmt.Println("e1", e1.Value, &e1, e1)
  21. l.InsertBefore(3, e4)
  22. l.InsertAfter(2, e1)
  23. // 遍历链表并打印它包含的元素
  24. for e := l.Front(); e != nil; e = e.Next() {
  25. fmt.Println("e", e.Value, e, reflect.TypeOf(e))
  26. }
  27. var test list.List
  28. test.PushBack(99)
  29. // fmt.Println(&(test.Front().Value), test.Front())
  30. }
  31. func listAddrness() {
  32. l := list.New()
  33. l.PushBack(1)
  34. l.PushBack(2)
  35. l.PushBack(3)
  36. l.PushBack(4)
  37. l.PushBack(5)
  38. l.PushBack(6)
  39. for e := l.Front(); e != nil; e = e.Next() {
  40. fmt.Println("e", e.Value, e, reflect.TypeOf(e))
  41. }
  42. /*
  43. e 的内容 (结构体取地址)
  44. &{0xc0000744e0 0xc000074480 0xc000074480 1}
  45. &{next元素指针 prev元素指针 元素所在链表指针 1}
  46. */
  47. }