ARS. - Stack Operations

<< Click to Display Table of Contents >>

Navigation:  3. Script Language > Arrays and Data-Structures > ARS. - Array's >

ARS. - Stack Operations

 

📘 SPR Stack Operations Manual

Welcome to the SPR Stack Operations Manual! 🌟

This guide provides a comprehensive overview of Stack operations in SPR, explaining what they are, how to use them, and why they are a powerful tool for scripting. Whether you're a beginner or an experienced user, this manual will help you master Stack operations with clear explanations and practical examples. Let’s dive in! 🚀

 

📄 Table of Contents

What is a Stack?

Stack Commands Overview

Code Examples

Illustrations

 

What is a Stack? 📜

A Stack in SPR is a linear data structure that follows the Last In, First Out (LIFO) principle. This means that the last element added to the stack will be the first one to be removed. Think of it as a stack of plates: you add plates to the top and remove plates from the top.

Stacks are used in various scenarios, including:

1.Function call management (e.g., recursion)

2.Expression evaluation (e.g., parsing)

3.Backtracking algorithms (e.g., depth-first search)

 

Stack Commands Overview 🛠️

Here’s a quick overview of the most common ARS commands for Stack operations:

 

Basic Stack Operations

 

' Pushes an element onto the stack.

ARS.StkPush|$$ARS|Value

 

' Pops the top element from the stack.

ARS.StkPop|$$ARS|$$RES

 

' Peeks at the top element of the stack without removing it.

ARS.StkPeek|$$ARS|$$RES

 

Advanced Stack Operations

' Checks if the stack is empty.

ARS.StkIsEmpty|$$ARS|$$RES

 

' Returns the number of elements in the stack.

ARS.StkCount|$$ARS|$$RES

 

' Clears all elements from the stack.

ARS.StkClear|$$ARS

 

Code Examples

Here are some practical examples of using Stack operations in SPR:

 

' Push elements onto the stack.

ARS.StkPush|$$ARS|10

ARS.StkPush|$$ARS|20

ARS.StkPush|$$ARS|30

 

' Pop the top element from the stack.

ARS.StkPop|$$ARS|$$RES

 

' Peek at the top element of the stack.

ARS.StkPeek|$$ARS|$$RES

 


Illustrations

Below is an illustration of how a stack operates using Unicode characters:

 

Stack Operations Illustration

 

Initial Stack:

+---+

| 3 |

+---+

| 2 |

+---+

| 1 |

+---+

 

Push Operation (Adding 4 to the stack):

+---+

| 4 |

+---+

| 3 |

+---+

| 2 |

+---+

| 1 |

+---+

 

Pop Operation (Removing the top element, 4):

+---+

| 3 |

+---+

| 2 |

+---+

| 1 |

+---+

 

Peek Operation (Viewing the top element, 3):

+---+

| 3 |

+---+

| 2 |

+---+

| 1 |

+---+