You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// description: based on `geeksforgeeks` description Stack is a linear data structure which follows a particular order in which the operations are performed.
// The order may be LIFO(Last In First Out) or FILO(First In Last Out).
// details:
// Stack Data Structure : https://www.geeksforgeeks.org/stack-data-structure-introduction-program/
// Stack (abstract data type) : https://en.wikipedia.org/wiki/Stack_(abstract_data_type)
// author [Milad](https://github.com/miraddo)
// see stacklinkedlist.go, stacklinkedlistwithlist.go, stack_test.go
package stack
typeArray[Tany] struct {
elements []T
}
// NewStack creates and returns a new stack.
funcNewStack[Tany]() *Array[T] {
return&Array[T]{}
}
// Push adds an element to the top of the stack.
func (s*Array[T]) Push(valueT) {
s.elements=append(s.elements, value)
}
// Size returns the number of elements in the stack.
func (s*Array[T]) Length() int {
returnlen(s.elements)
}
// Peek returns the top element of the stack without removing it.
func (s*Array[T]) Peek() T {
ifs.IsEmpty() {
varzeroValueT
returnzeroValue// Stack is empty
}
returns.elements[len(s.elements)-1]
}
// IsEmpty returns true if the stack is empty, false otherwise.
func (s*Array[T]) IsEmpty() bool {
returnlen(s.elements) ==0
}
// Pop removes and returns the top element from the stack.