Computer Science‎ > ‎

Basic Data Structures in Ruby - Stack




Programming With Ruby


The Stack data structure is described in detail over here : Data Structures: Stacks ( with C Program source code) .
Ruby doesn't provide an explicit 'stack' but for all practical purposes, the Array provided by Ruby can be treated as a full-fledged stack.

# Based on the ideas from : http://macdevelopertips.com/ruby/using-a-stack-with-ruby.html
# Stacks are a Last In First Out Data Structure
class Stack
    def initialize
        @elements = []
    end
    
    def length
        @elements.length
    end

    # Return self
    def push(x)
        @elements.push x
        self 
    end
    
    def pop
        @elements.pop
    end
    
    def peek
        @elements[-1]   
    end

    def display
        puts "(LeftMost : Oldest Element) " + @elements.join("<-") +" (RightMost : Newest Element)"
    end
end

testStack = Stack.new()
# Display initial (empty stack)
testStack.display
testStack.push(3).push(5).push(10)
# Now display stack
testStack.display
# Check the value at the top of the head
popped = testStack.pop
puts "Popped the value : " + popped.to_s
# Now the stack is ...
testStack.display
popped = testStack.pop
puts "Popped the value : " + popped.to_s
# Now the stack is ...
testStack.display


=begin

Sample Output :
~/work/ruby_tutorials$ ruby stack.rb
(LeftMost : Oldest Element)  (RightMost : Newest Element)
(LeftMost : Oldest Element) 3<-5<-10 (RightMost : Newest Element)
Popped the value : 10
(LeftMost : Oldest Element) 3<-5 (RightMost : Newest Element)
Popped the value : 5
(LeftMost : Oldest Element) 3 (RightMost : Newest Element)
=end

 

Check out some of our other Ruby Tutorials :

Introduction to Ruby

 Introduction to Ruby and some playing around with the Interactive Ruby Shell (irb) Introduction to Ruby - Conditional statements and Modifiers: If-then, Unless, Case Introduction to Ruby Comments - Single and Multi-Line comments Introduction to Ruby Loops - Using While, Until, For, Break, Next , Redo, Retry
 Introduction to Ruby - Arrays - Sorting, Filtering (Select), Transforming, Multi-Dimensional Arrays Introduction to Ruby - Strings Introduction to Ruby - Making a Script Executable Introduction to Ruby - Regular Expressions, Match, Scan
 Introduction to Ruby - Computing Factorials Recursively : An Example of Recursion Introduction to Ruby - Binomial Coefficients (nCr) : An Example of Recursion Introduction to Ruby - Computing a Power Set : An Example of Recursion Introduction to Ruby - Towers of Hanoi : An Example of Recursion
 Introduction to Ruby - Strings: Substitution, Encoding, Built-In Methods 



Basic Data Structures With Ruby




Programming With Ruby

 Introduction to Ruby and some playing around with the Interactive Ruby Shell (irb)

Introduction to Ruby - Conditional statements and Modifiers: If-then, Unless, Case

Introduction to Ruby Comments - Single and Multi-Line comments

Introduction to Ruby Loops - Using While, Until, For, Break, Next , Redo, Retry

Introduction to Ruby - Arrays - Sorting, Filtering (Select), Transforming, Multi-Dimensional Arrays 

Introduction to Ruby - Strings

Introduction to Ruby - Making a Script Executable

Introduction to Ruby - Regular Expressions, Match, Scan

Introduction to Ruby - Computing Factorials Recursively : An Example of Recursion

Introduction to Ruby - Binomial Coefficients (nCr) : An Example of Recursion

Introduction to Ruby - Computing a Power Set : An Example of Recursion

Introduction to Ruby - Towers of Hanoi : An Example of Recursion

 Introduction to Ruby - Strings: Substitution, Encoding, Built-In Methods

Basic Data Structures in Ruby - Insertion Sort

Basic Data Structures in Ruby - Selection Sort

Basic Data Structures in Ruby - Merge Sort

Basic Data Structures in Ruby - Quick Sort

Functional Programming with Ruby

Basic Data Structures in Ruby - Stack

Basic Data Structures in Ruby - The Queue

Basic Data Structures in Ruby - Linked List - ( A Simple, Singly Linked List)

Basic Data Structures in Ruby - Binary Search Tree