Akik Look at that coder

Public account: Look at that code farmer

Last installment introduced the Use of Go language learning map | Go Theme month

  • Use of maps
  • Definition of mapping
  • Add elements to the map
  • Delete elements from the map
  • Traversing the Map’s “key-value pairs”

This article will continue to take you into the world of Go.

1. Introduction to this paper

Use of lists for Go language learning

2. List definition

A list is a container of discontinuous storage consisting of multiple nodes that record their relationships to each other through variables. Lists can be implemented in many ways, such as singly and doubly linked lists.

3. How to declare a list

There are two ways to initialize a list

New and declaration.

Both methods have the same initialization effect.

1. Initialize the list using the New method

Variable name: = list. The New ()

2. Initialize the list with a declaration

Var The name of the variable list.list

Unlike slicing and maps, lists are not restricted by specific element types. So the elements of the list can be of any type.

4. Insert elements into the list

The following code shows how to add elements to a list:

package main

import (
   "container/list"
   "fmt"
)

func main(){
      newlist:=list.New()
      newlist.PushBack("Second")
      newlist.PushFront("First")
      var node=1
      fori:=newlist.Front(); i! =nil; i=i.Next(){ fmt.Printf("The %v element of the list is: %v\n",node,i.Value)
         node++
      }
   }
Copy the code

The output is:

There are four ways to insert elements into a list

methods function
List. InsertAfter (” word “, mark) Insert the element word after the mark dot
List. The InsertBefore (” word “, mark) Insert the element word before the mark dot
Word list. PushFront (” “) Inserts the element word into the head of the list
Word list. PushBack (” “) Insert the element word at the end of the list

The specific use is shown in the following code:

package main

import (
   "container/list"
   "fmt"
)

func main(){
      newlist:=list.New()

      newlist.PushFront("First")
      mark:=newlist.PushBack("Second")

      var node=1
      fori:=newlist.Front(); i! =nil; i=i.Next(){ fmt.Printf("The %v element of the old list is: %v\n",node,i.Value)
         node++
      }

      newlist.InsertAfter("World",mark)
      newlist.InsertBefore("Hello",mark)

      var node2=1
      fori:=newlist.Front(); i! =nil; i=i.Next(){ fmt.Printf("The %v element of the new list is: %v\n",node2,i.Value)
         node2++
      }
      
   }
Copy the code

The output is:

5. Delete elements from the list

We use the remove method to remove elements from the list

The details are as follows:

package main

import (
   "container/list"
   "fmt"
)

func main(){
      newlist:=list.New()

      newlist.PushFront("First")
      mark:=newlist.PushBack("Second")

      var node=1
      fori:=newlist.Front(); i! =nil; i=i.Next(){ fmt.Printf("The %v element of the old list is: %v\n",node,i.Value)
         node++
      }

      newlist.InsertAfter("World",mark)
      newlist.InsertBefore("Hello",mark)

      var node2=1
      fori:=newlist.Front(); i! =nil; i=i.Next(){ fmt.Printf("The %v element of the new list is: %v\n",node2,i.Value)
         node2++
      }

      newlist.Remove(mark)

      var node3=1
      fori:=newlist.Front(); i! =nil; i=i.Next(){ fmt.Printf("The %v element in the list after deletion is: %v\n",node2,i.Value)
         node3++
   }
   
   }
Copy the code

The output is:

6. Traverse the list – Access each element of the list

The Front function can be used to retrieve the header element while iterating through the list, and as long as the element is not empty, you can continue. Call Next of the element after each iteration to get the Next element.

The specific code is as follows:

package main

import (
   "container/list"
   "fmt"
)

func main(){
      newlist:=list.New()

      newlist.PushFront("First")
      newlist.PushBack("Second")

      var node=1
      fori:=newlist.Front(); i! =nil; i=i.Next(){ fmt.Printf("The %v element of the list is: %v\n",node,i.Value)
         node++
      }

   }
Copy the code

The output is:

If you find this helpful:

1. Click “like” to support it, so that more people can see this article

2, pay attention to the public number: look at that code farmers, we study together and progress together.