stack

package
Version: v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2019 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package stack provides implementation to get and print formatted stack.

Example:

import "github.com/shivamMg/ppds/stack"

// a stack implementation.
type myStack struct {
	elems []int
}

func (s *myStack) push(ele int) {
	s.elems = append(s.elems, ele)
}

func (s *myStack) pop() (int, bool) {
	l := len(s.elems)
	if l == 0 {
		return 0, false
	}
	ele := s.elems[l-1]
	s.elems = s.elems[:l-1]
	return ele, true
}

// myStack implements stack.Stack. Notice that the receiver is of *myStack
// type - since Push and Pop are required to modify s.
func (s *myStack) Pop() (interface{}, bool) {
	return s.pop()
}

func (s *myStack) Push(ele interface{}) {
	s.push(ele.(int))
}

// s := myStack{}
// s.push(11)
// s.push(12)
// stack.Print(&s)

Index

Constants

View Source
const (
	BoxVer     = "│"
	BoxHor     = "─"
	BoxUpHor   = "┴"
	BoxUpLeft  = "┘"
	BoxUpRight = "└"
)

Variables

This section is empty.

Functions

func Print

func Print(s Stack)

Print prints the formatted stack to standard output.

func Sprint

func Sprint(s Stack) (str string)

Sprint returns the formatted stack.

Types

type Stack

type Stack interface {
	// Pop must pop the top element out of the stack and return it. In case of
	// an empty stack, ok should be false, else true.
	Pop() (ele interface{}, ok bool)
	// Push must insert an element in the stack. Since ele is of interface type,
	// type assertion must be done before inserting in the stack.
	Push(ele interface{})
}

Stack represents a stack of elements.

Source Files

Jump to

Keyboard shortcuts

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