Skip to content
On this page

# Stack ​

Stack is an abstract data type that serves as a collection of elements. Elements follow the LIFO principle. FIFO is last-in, first-out, meaning that the most recently produced items are recorded as sold first.

## Source ​

- https://github.com/duke-git/lancet/blob/main/datastructure/stack/arraystack.go
- https://github.com/duke-git/lancet/blob/main/datastructure/stack/linkedstack.go

## Usage ​

go

    import (
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    import (
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

## Index ​

### 1. ArrayStack ​

- NewArrayStack
- Push
- Pop
- Peak
- Data
- Size
- IsEmpty
- Clear

### 2. LinkedStack ​

- NewLinkedStack
- Push
- Pop
- Peak
- Data
- Size
- IsEmpty
- Clear
- Print

## Documentation ​

### 1. ArrayStack ​

ArrayStack is a stack implemented by slice.

### NewArrayStack ​

Return a empty ArrayStack pointer

Signature:

go

    type ArrayStack[T any] struct {
    	data   []T
    	length int
    }
    func NewArrayStack[T any]() *ArrayStack[T]

    type ArrayStack[T any] struct {
    	data   []T
    	length int
    }
    func NewArrayStack[T any]() *ArrayStack[T]

Example:

go

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewArrayStack[int]()
        fmt.Println(sk)
    }

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewArrayStack[int]()
        fmt.Println(sk)
    }

### Push ​

Push element into array stack

Signature:

go

    func (s *ArrayStack[T]) Push(value T)

    func (s *ArrayStack[T]) Push(value T)

Example:

go

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewArrayStack[int]()
        sk.Push(1)
        sk.Push(2)
        sk.Push(3)

        fmt.Println(sk.Data()) //[]int{3, 2, 1}
    }

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewArrayStack[int]()
        sk.Push(1)
        sk.Push(2)
        sk.Push(3)

        fmt.Println(sk.Data()) //[]int{3, 2, 1}
    }

### Pop ​

Delete the top element of stack then return it, if stack is empty, return nil and error

Signature:

go

    func (s *ArrayStack[T]) Pop() (*T, error)

    func (s *ArrayStack[T]) Pop() (*T, error)

Example:

go

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewArrayStack[int]()
        sk.Push(1)
        sk.Push(2)
        sk.Push(3)

        val, err := sk.Pop()
        fmt.Println(err) //nil
        fmt.Println(*val) //3

        fmt.Println(sk.Data()) //[]int{2, 1}
    }

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewArrayStack[int]()
        sk.Push(1)
        sk.Push(2)
        sk.Push(3)

        val, err := sk.Pop()
        fmt.Println(err) //nil
        fmt.Println(*val) //3

        fmt.Println(sk.Data()) //[]int{2, 1}
    }

### Peak ​

Return the top element of array stack

Signature:

go

    func (s *ArrayStack[T]) Peak() (*T, error)

    func (s *ArrayStack[T]) Peak() (*T, error)

Example:

go

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewArrayStack[int]()
        sk.Push(1)
        sk.Push(2)
        sk.Push(3)

        val, err := sk.Peak()
        fmt.Println(err) //nil
        fmt.Println(*val) //3

        fmt.Println(sk.Data()) //[]int{3, 2, 1}
    }

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewArrayStack[int]()
        sk.Push(1)
        sk.Push(2)
        sk.Push(3)

        val, err := sk.Peak()
        fmt.Println(err) //nil
        fmt.Println(*val) //3

        fmt.Println(sk.Data()) //[]int{3, 2, 1}
    }

### Data ​

Return a slice of all data in array stack

Signature:

go

    func (s *ArrayStack[T]) Data() []T

    func (s *ArrayStack[T]) Data() []T

Example:

go

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewArrayStack[int]()
        sk.Push(1)
        sk.Push(2)
        sk.Push(3)

        fmt.Println(sk.Data()) //[]int{3, 2, 1}
    }

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewArrayStack[int]()
        sk.Push(1)
        sk.Push(2)
        sk.Push(3)

        fmt.Println(sk.Data()) //[]int{3, 2, 1}
    }

### Size ​

Return number of elements in array stack

Signature:

go

    func (s *ArrayStack[T]) Size() int

    func (s *ArrayStack[T]) Size() int

Example:

go

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewArrayStack[int]()
        sk.Push(1)
        sk.Push(2)
        sk.Push(3)

        fmt.Println(sk.Size()) //3
    }

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewArrayStack[int]()
        sk.Push(1)
        sk.Push(2)
        sk.Push(3)

        fmt.Println(sk.Size()) //3
    }

### IsEmpty ​

Check if array stack is empty or not

Signature:

go

    func (s *ArrayStack[T]) IsEmpty() bool

    func (s *ArrayStack[T]) IsEmpty() bool

Example:

go

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewArrayStack[int]()
        fmt.Println(sk.IsEmpty()) //true

        sk.Push(1)
        sk.Push(2)
        sk.Push(3)

        fmt.Println(sk.IsEmpty()) //false
    }

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewArrayStack[int]()
        fmt.Println(sk.IsEmpty()) //true

        sk.Push(1)
        sk.Push(2)
        sk.Push(3)

        fmt.Println(sk.IsEmpty()) //false
    }

### Clear ​

Clear all elments in array stack

Signature:

go

    func (s *ArrayStack[T]) Clear()

    func (s *ArrayStack[T]) Clear()

Example:

go

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewArrayStack[int]()

        sk.Push(1)
        sk.Push(2)
        sk.Push(3)

        sk.Clear()

        fmt.Println(sk.Data()) //[]int{}
    }

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewArrayStack[int]()

        sk.Push(1)
        sk.Push(2)
        sk.Push(3)

        sk.Clear()

        fmt.Println(sk.Data()) //[]int{}
    }

### 2. LinkedStack ​

LinkedStack is a stack implemented by linked list.

### NewLinkedStack ​

Return a empty LinkedStack pointer

Signature:

go

    type StackNode[T any] struct {
    	Value T
    	Next  *StackNode[T]
    }
    type LinkedStack[T any] struct {
    	top    *datastructure.StackNode[T]
    	length int
    }
    func NewLinkedStack[T any]() *LinkedStack[T]

    type StackNode[T any] struct {
    	Value T
    	Next  *StackNode[T]
    }
    type LinkedStack[T any] struct {
    	top    *datastructure.StackNode[T]
    	length int
    }
    func NewLinkedStack[T any]() *LinkedStack[T]

Example:

go

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewLinkedStack[int]()
        fmt.Println(sk)
    }

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewLinkedStack[int]()
        fmt.Println(sk)
    }

### Push ​

Push element into linked stack

Signature:

go

    func (s *LinkedStack[T]) Push(value T)

    func (s *LinkedStack[T]) Push(value T)

Example:

go

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewLinkedStack[int]()
        sk.Push(1)
        sk.Push(2)
        sk.Push(3)

        fmt.Println(sk.Data()) //[]int{3, 2, 1}
    }

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewLinkedStack[int]()
        sk.Push(1)
        sk.Push(2)
        sk.Push(3)

        fmt.Println(sk.Data()) //[]int{3, 2, 1}
    }

### Pop ​

Delete the top element of stack then return it, if stack is empty, return nil and error

Signature:

go

    func (s *LinkedStack[T]) Pop() (*T, error)

    func (s *LinkedStack[T]) Pop() (*T, error)

Example:

go

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewLinkedStack[int]()
        sk.Push(1)
        sk.Push(2)
        sk.Push(3)

        val, err := sk.Pop()
        fmt.Println(err) //nil
        fmt.Println(*val) //3

        fmt.Println(sk.Data()) //[]int{2, 1}
    }

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewLinkedStack[int]()
        sk.Push(1)
        sk.Push(2)
        sk.Push(3)

        val, err := sk.Pop()
        fmt.Println(err) //nil
        fmt.Println(*val) //3

        fmt.Println(sk.Data()) //[]int{2, 1}
    }

### Peak ​

Return the top element of linked stack

Signature:

go

    func (s *LinkedStack[T]) Peak() (*T, error)

    func (s *LinkedStack[T]) Peak() (*T, error)

Example:

go

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewLinkedStack[int]()
        sk.Push(1)
        sk.Push(2)
        sk.Push(3)

        val, err := sk.Peak()
        fmt.Println(err) //nil
        fmt.Println(*val) //3

        fmt.Println(sk.Data()) //[]int{3, 2, 1}
    }

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewLinkedStack[int]()
        sk.Push(1)
        sk.Push(2)
        sk.Push(3)

        val, err := sk.Peak()
        fmt.Println(err) //nil
        fmt.Println(*val) //3

        fmt.Println(sk.Data()) //[]int{3, 2, 1}
    }

### Data ​

Return a slice of all data in linked stack

Signature:

go

    func (s *LinkedStack[T]) Data() []T

    func (s *LinkedStack[T]) Data() []T

Example:

go

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewLinkedStack[int]()
        sk.Push(1)
        sk.Push(2)
        sk.Push(3)

        fmt.Println(sk.Data()) //[]int{3, 2, 1}
    }

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewLinkedStack[int]()
        sk.Push(1)
        sk.Push(2)
        sk.Push(3)

        fmt.Println(sk.Data()) //[]int{3, 2, 1}
    }

### Size ​

Return number of elements in linked stack

Signature:

go

    func (s *LinkedStack[T]) Size() int

    func (s *LinkedStack[T]) Size() int

Example:

go

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewLinkedStack[int]()
        sk.Push(1)
        sk.Push(2)
        sk.Push(3)

        fmt.Println(sk.Size()) //3
    }

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewLinkedStack[int]()
        sk.Push(1)
        sk.Push(2)
        sk.Push(3)

        fmt.Println(sk.Size()) //3
    }

### IsEmpty ​

Check if linked stack is empty or not

Signature:

go

    func (s *LinkedStack[T]) IsEmpty() bool

    func (s *LinkedStack[T]) IsEmpty() bool

Example:

go

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewLinkedStack[int]()
        fmt.Println(sk.IsEmpty()) //true

        sk.Push(1)
        sk.Push(2)
        sk.Push(3)

        fmt.Println(sk.IsEmpty()) //false
    }

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewLinkedStack[int]()
        fmt.Println(sk.IsEmpty()) //true

        sk.Push(1)
        sk.Push(2)
        sk.Push(3)

        fmt.Println(sk.IsEmpty()) //false
    }

### Clear ​

Clear all elments in linked stack

Signature:

go

    func (s *LinkedStack[T]) Clear()

    func (s *LinkedStack[T]) Clear()

Example:

go

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewLinkedStack[int]()

        sk.Push(1)
        sk.Push(2)
        sk.Push(3)

        sk.Clear()

        fmt.Println(sk.Data()) //[]int{}
    }

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewLinkedStack[int]()

        sk.Push(1)
        sk.Push(2)
        sk.Push(3)

        sk.Clear()

        fmt.Println(sk.Data()) //[]int{}
    }

### Print ​

Print the structure of a linked stack

Signature:

go

    func (s *LinkedStack[T]) Print()

    func (s *LinkedStack[T]) Print()

Example:

go

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewLinkedStack[int]()

        sk.Push(1)
        sk.Push(2)
        sk.Push(3)

        sk.Print() //[ &{Value:3 Next:0xc000010260}, &{Value:2 Next:0xc000010250}, &{Value:1 Next:<nil>},  ]
    }

    package main

    import (
        "fmt"
        stack "github.com/duke-git/lancet/v2/datastructure/stack"
    )

    func main() {
        sk := stack.NewLinkedStack[int]()

        sk.Push(1)
        sk.Push(2)
        sk.Push(3)

        sk.Print() //[ &{Value:3 Next:0xc000010260}, &{Value:2 Next:0xc000010250}, &{Value:1 Next:<nil>},  ]
    }