# 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/arraystack.go) - [https://github.com/duke-git/lancet/blob/main/datastructure/stack/linkedstack.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" ) ``` ## Index ### 1. ArrayStack - [NewArrayStack](#NewArrayStack) - [Push](#ArrayStack_Push) - [Pop](#ArrayStack_Pop) - [Peak](#ArrayStack_Peak) - [Data](#ArrayStack_Data) - [Size](#ArrayStack_Size) - [IsEmpty](#ArrayStack_IsEmpty) - [Clear](#ArrayStack_Clear) ### 2. LinkedStack - [NewLinkedStack](#NewLinkedStack) - [Push](#LinkedStack_Push) - [Pop](#LinkedStack_Pop) - [Peak](#LinkedStack_Peak) - [Data](#LinkedStack_Data) - [Size](#LinkedStack_Size) - [IsEmpty](#LinkedStack_IsEmpty) - [Clear](#LinkedStack_Clear) - [Print](#LinkedStack_Print) ## Documentation ### 1. ArrayStack ArrayStack is a stack implemented by slice. ### NewArrayStackReturn a empty ArrayStack pointer
Signature: ```go 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) } ``` ### PushPush element into array stack
Signature: ```go 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} } ``` ### PopDelete 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) ``` 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} } ``` ### PeakReturn the top element of array stack
Signature: ```go 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} } ``` ### DataReturn a slice of all data in array stack
Signature: ```go 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} } ``` ### SizeReturn number of elements in array stack
Signature: ```go 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 } ``` ### IsEmptyCheck if array stack is empty or not
Signature: ```go 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 } ``` ### ClearClear all elments in array stack
Signature: ```go 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{} } ``` ### 2. LinkedStack LinkedStack is a stack implemented by linked list. ### NewLinkedStackReturn 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] ``` Example: ```go package main import ( "fmt" stack "github.com/duke-git/lancet/v2/datastructure/stack" ) func main() { sk := stack.NewLinkedStack[int]() fmt.Println(sk) } ``` ### PushPush element into linked stack
Signature: ```go 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} } ``` ### PopDelete 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) ``` 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} } ``` ### PeakReturn the top element of linked stack
Signature: ```go 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} } ``` ### DataReturn a slice of all data in linked stack
Signature: ```go 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} } ``` ### SizeReturn number of elements in linked stack
Signature: ```go 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 } ``` ### IsEmptyCheck if linked stack is empty or not
Signature: ```go 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 } ``` ### ClearClear all elments in linked stack
Signature: ```go 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{} } ``` ### PrintPrint the structure of a linked stack
Signature: ```go 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: