gstack

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 26, 2023 License: MIT Imports: 2 Imported by: 0

README

gstack

Simple Generic Stack implementation in Go using linked list

Usage

s := gstack.New(1, 2, 3, 4)
fmt.Println(s.Len())
fmt.Println(s.Pop())
fmt.Println(s.Pop())
fmt.Println(s.Pop())
fmt.Println(s.Pop())
s.Push(5)
fmt.Println(s.Peek())
// Output: 4
// 4
// 3
// 2
// 1
// 5

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GStack

type GStack[T any] struct {
	// contains filtered or unexported fields
}

GStack is generic Stack implementation using linked list

Example
package main

import (
	"fmt"
	"github.com/sv-tools/gstack"
)

func main() {
	s := gstack.New(1, 2, 3, 4)
	fmt.Println(s.Len())
	fmt.Println(s.Pop())
	fmt.Println(s.Pop())
	fmt.Println(s.Pop())
	fmt.Println(s.Pop())
	s.Push(5)
	fmt.Println(s.Peek())
}
Output:

4
4
3
2
1
5

func New

func New[T any](items ...T) GStack[T]

New creates an instance of GStack and pushes all given items to the created stack in reversed order

func (*GStack[T]) Clear

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

Clear clears the stack

func (*GStack[T]) Len

func (s *GStack[T]) Len() int

Len returns the length of the stack

func (*GStack[T]) Peek

func (s *GStack[T]) Peek() T

Peek returns the top item without removing it or a zero object of given type

func (*GStack[T]) Pop

func (s *GStack[T]) Pop() T

Pop returns the top item and removes it from the stack or returns a zero object of given type

func (*GStack[T]) Push

func (s *GStack[T]) Push(item T)

Push puts the given item into the stack

func (*GStack[T]) String

func (s *GStack[T]) String() string

String returns the list of all items in text format

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL