How to resolve the algorithm Stack step by step in the Nim programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Stack step by step in the Nim programming language

Table of Contents

Problem Statement

A stack is a container of elements with   last in, first out   access policy.   Sometimes it also called LIFO. The stack is accessed through its top. The basic stack operations are:

Sometimes the last pushed stack element is made accessible for immutable access (for read) or mutable access (for write):

Stacks allow a very simple hardware implementation. They are common in almost all processors. In programming, stacks are also very popular for their way (LIFO) of resource management, usually memory. Nested scopes of language objects are naturally implemented by a stack (sometimes by multiple stacks). This is a classical way to implement local variables of a re-entrant or recursive subprogram. Stacks are also used to describe a formal computational framework. See stack machine. Many algorithms in pattern matching, compiler construction (e.g. recursive descent parsers), and machine learning (e.g. based on tree traversal) have a natural representation in terms of stacks.

Create a stack supporting the basic operations: push, pop, empty.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Stack step by step in the Nim programming language

Source code in the nim programming language

type Stack[T] = distinct seq[T]

func initStack[T](initialSize = 32): Stack[T] =
  Stack[T](newSeq[T](initialSize))

func isEmpty[T](stack: Stack[T]): bool =
  seq[T](stack).len == 0

func push[T](stack: var Stack[T]; item: sink T) =
  seq[T](stack).add(item)

func pop[T](stack: var Stack[T]): T =
  if stack.isEmpty:
    raise newException(IndexDefect, "stack is empty.")
  seq[T](stack).pop()

func top[T](stack: Stack[T]): T =
  if stack.isEmpty:
    raise newException(IndexDefect, "stack is empty.")
  seq[T](stack)[^1]

func mtop[T](stack: var Stack[T]): var T =
  if stack.isEmpty:
    raise newException(IndexDefect, "stack is empty.")
  seq[T](stack)[^1]

func `mtop=`[T](stack: var Stack[T]; value: T) =
  if stack.isEmpty:
    raise newException(IndexDefect, "stack is empty.")
  seq[T](stack)[^1] = value

when isMainModule:

  var s = initStack[int]()
  s.push 2
  echo s.pop
  s.push 3
  echo s.top
  s.mtop += 1
  echo s.top
  s.mtop = 5
  echo s.top


  

You may also check:How to resolve the algorithm Semordnilap step by step in the Transd programming language
You may also check:How to resolve the algorithm Numbers which are the cube roots of the product of their proper divisors step by step in the Delphi programming language
You may also check:How to resolve the algorithm Van Eck sequence step by step in the ALGOL-M programming language
You may also check:How to resolve the algorithm Keyboard input/Keypress check step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Hello world/Graphical step by step in the Ioke programming language