stack

package module
v0.0.0-...-d101f8d Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2022 License: MIT Imports: 0 Imported by: 0

README

stack

GoDoc License Build Status Report Card GoCover

Simple Golang stack implemented through a linked list

Installation
go get -u github.com/tiny-go/stack
Usage
package main

import (
	"fmt"

	"github.com/tiny-go/stack"
)

func main() {
	var a, b, c = 1, 2, 3

	// init stack
	stk := new(stack.Stack[int])

	// push elements
	stk.Push(a)
	stk.Push(b)
	stk.Push(c)

	// get top element
	fmt.Println(stk.Top()) // 3 true
	fmt.Println(stk.Len()) // 3


	// pop elements
	fmt.Println(stk.Pop()) // 3 true
	fmt.Println(stk.Pop()) // 2 true
	fmt.Println(stk.Pop()) // 1 true
	fmt.Println(stk.Pop()) // 0 false
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Element

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

Element is the single element of the Stack.

type Stack

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

Stack for elements.

func (*Stack[T]) Len

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

Len returns the stack's length.

func (*Stack[T]) Pop

func (s *Stack[T]) Pop() (value T, ok bool)

Pop removes the top element from the stack and returns it's value.

func (*Stack[T]) Push

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

Push a new element onto the stack.

func (*Stack[T]) Top

func (s *Stack[T]) Top() (value T, ok bool)

Top returns a value of the top element of the stack.

Jump to

Keyboard shortcuts

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