Home  >  Article  >  Backend Development  >  golang stack implementation

golang stack implementation

王林
王林Original
2023-05-21 15:10:07490browse

Golang is a relatively new programming language, and its efficiency and concurrency make it an increasingly popular programming language. Golang has a powerful standard library that contains various data structures and algorithms, one of which is the stack.

The stack is a data structure that can be used to store and manipulate data in a program. It's similar to a stack of plates, you can add plates to the top, or remove plates from the top. This data structure usually follows the "last in first out (LIFO)" principle. That is, the last element we added to the stack will be popped first.

In Golang, we can use Slice to implement the stack. Next, we will demonstrate how to implement a basic stack in Golang.

First, we need to declare a stack structure, as shown below:

type Stack struct {
    items []int //用于存储栈中的元素
}

Among them, items is the slice we use to store the elements in the stack. We can use this slice to implement various operations on the stack.

Next, we need to implement the method of adding elements to the stack (Push), as shown below:

func (s *Stack) Push(item int) {
    s.items = append(s.items, item) //将元素添加到切片的末尾
}

In the function, we use the built-in append function to add the incoming elements to the end of the slice.

Next, we need to implement the method (Pop) to delete elements from the stack, as shown below:

func (s *Stack) Pop() int {
    length := len(s.items) - 1    //获取栈中元素数量
    lastItem := s.items[length]   //获取栈顶元素
    s.items = s.items[:length]    //从切片中删除栈顶元素
    return lastItem                //返回栈顶元素
}

In the function, we first get the last element in the slice (that is, the stack top element) and then use the slice's Slicing property to remove that element from the slice. Finally, we return the top element of the stack to the calling part.

Next, we need to implement the method (Peek) to get the top element of the stack, as shown below:

func (s *Stack) Peek() int {
    return s.items[len(s.items)-1]   //返回栈顶元素
}

In the function, we use len(s.items)-1 to get the stack The subscript of the top element, since the subscript of the last element in a slice is always len(s.items)-1.

Finally, we need to implement the method (IsEmpty) to check whether the stack is empty, as shown below:

func (s *Stack) IsEmpty() bool {
    return len(s.items) == 0    //判断栈中是否有元素
}

In the function, we use len(s.items) to check the items in the stack Number of elements. If there are no elements in the stack, we will return true, otherwise we will return false.

Now we have a basic Golang stack implementation. In the following sample code, we use Golang's main function to test the functionality of the stack:

func main() {
    myStack := Stack{}    //初始化一个栈
    
    myStack.Push(23)     //向栈中添加元素
    myStack.Push(45)
    myStack.Push(67)
    
    fmt.Println("栈顶元素为:", myStack.Peek())     //获取栈顶元素
    
    fmt.Println("弹出:", myStack.Pop())      //从栈中弹出元素
    fmt.Println("栈是否为空?", myStack.IsEmpty())    //检查栈是否为空

}

In this code, we first initialize a stack and then add three elements to it (23, 45 and 67). Next, we use the Peek method to get the top element of the stack, and the Pop method to pop the element from the stack. Finally, we use the IsEmpty method to check if the stack is empty.

After running the program, the output will look like this:

栈顶元素为: 67
弹出: 67
栈是否为空? false

Summary

In this article, we introduced how to implement the stack in Golang. We saw how to define a stack structure and how to implement the Push, Pop, Peek and IsEmpty methods. This simple stack implementation can help us store and manipulate data in Golang programs and better understand how the stack data structure works.

The above is the detailed content of golang stack implementation. 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
Previous article:golang programming methodNext article:golang programming method