[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
611 lines (419 loc) · 9.23 KB

stack.md

File metadata and controls

611 lines (419 loc) · 9.23 KB

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

Usage

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

Index

1. ArrayStack

2. LinkedStack

Documentation

1. ArrayStack

ArrayStack is a stack implemented by slice.

NewArrayStack

Return a empty ArrayStack pointer

Signature:

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

Example:

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:

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

Example:

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:

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

Example:

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:

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

Example:

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:

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

Example:

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:

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

Example:

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:

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

Example:

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:

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

Example:

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:

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:

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:

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

Example:

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:

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

Example:

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:

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

Example:

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:

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

Example:

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:

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

Example:

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:

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

Example:

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:

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

Example:

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:

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

Example:

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>},  ]
}