list.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package main
  2. import (
  3. "container/list"
  4. "fmt"
  5. )
  6. func printList(l *list.List) {
  7. for temp := l.Front(); temp != nil; temp = temp.Next() {
  8. fmt.Printf("%v ", temp.Value)
  9. }
  10. fmt.Println()
  11. }
  12. func main() {
  13. // foo1()
  14. // foo2()
  15. foo3()
  16. /*
  17. mylist := list.New()
  18. mylist.PushBack(0)
  19. mylist.PushBack(1)
  20. mylist.PushBack(2)
  21. mylist.PushBack(3)
  22. // Получаем указатель на элемент который добавили (*Element)
  23. elem3 := mylist.PushBack(4)
  24. printList(mylist)
  25. // удаляем элемент '3' по указателю (*Element)
  26. mylist.Remove(elem3)
  27. // так же можем воспользоваться методом Front()/Back() чтобы
  28. // получить первый/последний элемент
  29. mylist.Remove(mylist.Front())
  30. fmt.Printf("\nПосле удаления\n")
  31. printList(mylist)
  32. // mylist.PushFront(1)
  33. // mylist.PushFront(2)
  34. // mylist.PushFront(3)
  35. */
  36. }
  37. func foo1() {
  38. // Создаем контейнер list и добавляем в него элементы
  39. mylist := list.New()
  40. mylist.PushBack(2)
  41. mylist.PushBack(5)
  42. mylist.PushBack(3)
  43. mylist.PushBack(11)
  44. mylist.PushBack(12)
  45. // Проходимся по элементам и удаляем те, которые меньше 10
  46. for e := mylist.Front(); e != nil; {
  47. // Запоминаем следующий элемент перед удалением текущего
  48. next := e.Next()
  49. if e.Value.(int) < 10 {
  50. // Удалялем текущий элемент из списка
  51. mylist.Remove(e)
  52. }
  53. e = next // Переходим к следующему элементу
  54. }
  55. // Выводим список после удаления элементов
  56. for e := mylist.Front(); e != nil; e = e.Next() {
  57. fmt.Println(e.Value)
  58. }
  59. }
  60. // ---------------------------------------------------------------
  61. func foo2() {
  62. queue := list.New()
  63. Push(1, queue)
  64. Push(2, queue)
  65. Push(3, queue)
  66. printQueue(queue)
  67. Pop(queue)
  68. printQueue(queue)
  69. // fmt.Println(Pop(queue))
  70. // printQueue(queue)
  71. }
  72. // Добавление элементов в очередь
  73. func Push(elem interface{}, queue *list.List) {
  74. queue.PushBack(elem)
  75. }
  76. // Удуление элемента и его возврат
  77. func Pop(queue *list.List) interface{} {
  78. if queue.Front() != nil {
  79. return nil
  80. }
  81. return queue.Remove(queue.Front())
  82. }
  83. // Печать очереди в одну строку без пробелов
  84. func printQueue(queue *list.List) {
  85. for temp := queue.Front(); temp != nil; temp = temp.Next() {
  86. fmt.Print(temp.Value)
  87. }
  88. }
  89. // ---------------------------------------------------------------
  90. func ReverseList(l *list.List) *list.List {
  91. reversedList := list.New()
  92. for temp := l.Front(); temp != nil; temp = temp.Next() {
  93. reversedList.PushFront(temp.Value)
  94. }
  95. return reversedList
  96. }
  97. func foo3() {
  98. mylist := list.New()
  99. for i := 0; i < 10; i++ {
  100. mylist.PushBack(i)
  101. }
  102. printList(mylist)
  103. printList(ReverseList(mylist))
  104. }