August 2020
Intermediate to advanced
508 pages
11h 53m
English
Most programming languages don’t actually come with the stack as a built-in data type or class. Instead, it’s up to you to implement it yourself. This is a stark contrast with arrays, which are available in most languages.
To create a stack, then, you generally have to use one of the built-in data structures to actually hold the data. Here is one way to implement a stack using Ruby, which uses an array under the hood:
| | class Stack |
| | def initialize |
| | @data = [] |
| | end |
| | |
| | def push(element) |
| | @data << element |
| | end |
| | |
| | def pop |
| | @data.pop |
| | end |
| | |
| | def read |
| | @data.last |
| | end |
| | end |
As you can see, our stack implementation stores the data in an array called @data.
Whenever a stack is initiated, ...
Read now
Unlock full access