Stack: Stack data structure

StackR Documentation

Stack data structure

Description

This is a stack data structure, operations you can do with it are:

Details

  • push

  • pop

  • peek

Constructor for Stack

Push an item in the stack

Peek an item from the stack

Return the item on top of the stack. Raises an error if the stack is empty

Pops an item from the stack

returns and removes the item on top of the stack. If the stack is empty, it raises an error.

TRUE is stack is empty

Returns the length of the stack

Internal method for object rappresentatio on REPL

Methods

Public methods


Method new()

Usage
Stack$new(...)
Arguments
...

items to be pushed in the stack

Examples
Stack$new("first")

Method push()

Usage
Stack$push(item)
Arguments
item

object to be pushed


Method peek()

Usage
Stack$peek()
Returns

element on top of the stack


Method pop()

Usage
Stack$pop()
Returns

element on top of the stack


Method empty()

Usage
Stack$empty()
Returns

TRUE if empty, FALSE otherwise


Method length()

Usage
Stack$length()
Returns

the number of elements in the stack


Method show()

Usage
Stack$show(sep = "\n")
Arguments
sep

separator used on the output


Method clone()

The objects of this class are cloneable with this method.

Usage
Stack$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Examples

## Not run: 
 s <- Stack()
 s$push(1)
 s$peek()
 s$pop()

## End(Not run)

## ------------------------------------------------
## Method `Stack$new`
## ------------------------------------------------

Stack$new("first")

giupo/rutils documentation built on Sept. 18, 2022, 6:30 a.m.