Learn How to Code
  • Welcome
  • Foundations
    • Introduction
      • Becoming a web developer
      • Motivation and mindset
      • Join a supportive community
      • How does the web work?
    • Requirements
      • Prerequisites
      • Text editors
      • Command line basics
      • Setting up Git
      • Setting up Node
    • Git
      • Git basics
      • Project: Practicing Git
    • Frontend
      • HTML and CSS
      • Developer Tools
      • Project: Create a web page
    • JavaScript
      • Strings and Conditionals
      • Using Developer Tools
      • Functions
      • Problem solving
      • Project: Rock paper scissors
      • Writing clean code
      • Arrays and Loops
      • The DOM
      • Project: Etch-A-Sketch
      • Objects and More Arrays
      • Project: Calculator
    • Backend
      • Frameworks
    • Next steps
  • Deep dives
    • Computer Science
      • Pseudocode and algorithms
      • Recursion and algorithms
        • Project: Fibs and sorting
        • More on algorithms
        • Big O
        • Project: Practicing Big O
      • Data structures
        • Maps, Stacks and Queues
        • Project: Stacks and Queues
        • Nodes, Linked Lists and Trees
        • Project: Linked Lists
        • Project: Trees
        • Next steps
    • Databases
      • Databases and SQL
      • Project: SQL Zoo
    • Design / UX
      • Fonts and typography
      • Grids
      • Project: Teardown
      • Responsive design
      • Project: Mobile friendly
      • CSS frameworks
      • Project: Bootstrapping
    • HTML / CSS
      • HTML Basics
        • Linking
        • Images and media
        • Project: Embedding media
        • HTML5
        • Tables
        • Lists
        • Forms
        • Project: Make a form
      • CSS Basics
        • Box model
        • Floats and positioning
        • Flexbox
        • Grids
        • Project: Breaking news
        • Best practices
        • Backgrounds and gradients
        • Project: One more thing
        • CSS3
        • Preprocessors
        • Project: Your own framework
      • Next steps
    • JavaScript
      • Refresher
      • Organization
      • Objects and constructors
      • Project: Library
      • Factory functions and module patterns
      • Project: Tic Tac Toe
      • Classes
      • ES6 Modules
      • Project: Restaurant
      • Object Oriented Programming
      • Project: Todo list
      • Linting
      • Menus and sliders
      • Forms
      • ES6 features
      • JSON
      • Callbacks and promises
      • Using APIs
      • Async and Await
      • Project: Weather
      • Testing
      • Project: Testing 1-2-3
      • Advanced Testing
      • Project: Battleship
      • Backends
      • Project: Where's Waldo?
      • Project: All-Star
      • Next steps
    • NodeJS
      • Project: Going to school
      • Project: Passing the test
      • Express
        • Templates and middleware
        • CRUD and MVC
        • Project: Message board
        • Routes
        • Displaying data
        • Forms and deployment
        • Project: Inventory
      • Authentication
      • Security
      • Project: Clubhouse
      • APIs
      • Securing an API
      • Project: Blog
      • Testing
      • Testing with a database
      • Project: Social network
    • React
      • Props and State
      • Render lists and handle inputs
      • Project: CV
      • Lifecycle methods
      • Hooks
      • Project: Memory card
      • Router
      • Project: Shopping cart
      • Advanced concepts
    • Ruby
      • Installation
      • Data types
      • Variables
      • Input and Output
      • Conditionals
      • Loops
      • Arrays
      • Hashes
      • Methods
      • Enumerables
      • More enumerables
      • Nested collections
      • Blocks
      • Pattern matching
      • Debugging
      • Project: Caesar cipher
      • Project: Substrings
      • Project: Stock picker
      • Project: Bubble sort
      • Object oriented programming
      • Project: Tic Tac Toe
      • Project: Mastermind
      • Serialization
      • Project: Event manager
      • Project: Hangman
      • Computer Science
        • Recursion
        • Project: Merge Sort
        • Data structures and algorithms
        • Project: Linked Lists
        • Project: Binary Search Trees
        • Project: Knight Travails
      • Testing
      • RSpec
      • Project: Four in a row
      • Git
      • Project: Open Source
      • Project: Chess
      • Next steps
    • Ruby on Rails
      • Using Heroku
      • Installing Rails
      • Basics
        • Routing
        • Controllers
        • Views
        • Asset pipeline
        • Deployment
        • Project: Blog
      • Active Record
        • Project: Upvote
      • Forms
        • Cookies, sessions, and authentication
        • Project: Password
      • Advanced forms and Active Record
        • Associations
        • Project: Private Events
        • Callbacks
        • Menus, helpers and nested forms
        • Project: Ticket agent
      • APIs
        • External APIs
        • Project: Animals
        • Project: Photo widget
      • Mailers
        • Project: Confirmation
      • Advanced topics
        • Action Cable
      • Project: Social network
      • Next steps
  • Getting hired
    • Preparing to find a job
      • Plan a strategy
      • What companies want
      • Get yourself together
      • How to prepare
      • Project: Make your website
    • Applying and interviewing
      • Qualifying leads
      • Project: Make your resume
      • Applying for jobs
      • Preparing for an interview
      • Handling an offer
      • Final words
  • Maintained by
    • wbnns
  • License
    • CC BY-NC-SA 4.0 © 2022
Powered by GitBook
On this page
  • Introduction
  • Learning outcomes
  • Maps
  • Stacks
  • Queues
  • Assignment
  1. Deep dives
  2. Computer Science
  3. Data structures

Maps, Stacks and Queues

In this lesson, we look at some of the more simple data structures that don't come in layers. We will be looking at Maps, Stacks and Queues.

PreviousData structuresNextProject: Stacks and Queues

Last updated 4 years ago

Introduction

Last lesson, we glossed over what a data structure is, and covered The Array as our classic example of a data structure. But simply picking out indexes might not be the best, or most structured way of playing around with our data. In this lesson, we look at some of the more simple data structures that don't come in layers. We will be looking at Maps, Stacks and Queues.

Learning outcomes

Look through these now and then use them to test yourself after doing the assignment:

  • What are Maps, Stacks and Queues?

  • What are their differences, and how may each be used?

  • What features would you expect each of these to have?

Maps

Sometimes known as an Associative Array, Dictionary or a Map depending on the language. Maps are used like an array, but instead of using an index number like:

arr = [4, 9, 45]
arr[0] #This would return 4

You use a key to identify where you want to look:

people = { 'Billy Smiggins' => 'male', 'Jane Doe' => 'female' }
people['Jane Doe'] #This would return female

This data structure allow us to store values with keys that have some meaning, or otherwise map values to objects other than integers. In the cases of Ruby and Javascript, this site's languages of choice, Ruby calls these and Javascript calls them

Stacks

Stacks should allow users to use two methods to manipulate data. #push, which puts it's input on the top, or the head of the stack; and #pop, which removes the element from the head of the stack and returns it. Some implementations may have #peek, which allow users to see the head of the stack without removing it. Here is an example of what Stack use may look like

simple_stack.push(3) #[3]
simple_stack.push(1) #[3,1]
simple_stack.push(9) #[3,1,9]

simple_stack.pop # Returns 9, Stack: [3,1]
simple_stack.pop # Returns 1, Stack: [3]
simple_stack.pop # Returns 3, Stack: []

To conceptualize this idea; think of a stack of plates. Removing a plate from the middle would be a silly idea. Instead, you'd take the plates from the top.

An alternative name for a stack is a LIFO (Last-In-First-Out) queue.

Queues

You will be familiar with the concept of queuing for something. Say you're buying groceries, and there's a line of people waiting to purchase their goods; and you join the back of this line. You have just joined a Queue.

To go back to our grocery analogy; you joining the queue would be #enqueue, and the individual finally buying and bagging their food would be #dequeue'd. Here is an example of what a Queue may look like:

grocery_queue.enqueue('Billy Smiggins') #["Billy Smiggins"]
grocery_queue.enqueue('Jane') #["Billy Smiggins", "Jane"]
grocery_queue.enqueue('QChai') #["Billy Smiggins", "Jane", "QChai"]

grocery_queue.dequeue # Returns "Billy Smiggins", Queue: ["Jane", "QChai"]
grocery_queue.dequeue # Returns "Jane",           Queue: ["QChai"]
grocery_queue.dequeue # Returns "QChai",          Queue: ["QChai"]

An alternative name for a queue is a FIFO (First-In-First-Out) queue.

Assignment

  1. Use pop, push and shift in your language of choice and get a feel for how these work! If you're not using Ruby or Javascript for this course, you might need to google around to find Queue implementation.

  2. View the docs of your chosen language and find how maps work in your language. It could be under a different name, like 'Hash' or 'Dictionary'.

You may recall that The Stack was mentioned in when we were learning about recursion, a concept that functions and parameters are stacked on top of each other in memory, then taken from the top down when they are needed. [Stacks]()) as a data structure follow the same methodology of stacking their input on top of remaining data, and taking data off the top of the stack.

Both and have Stack functionality built into their Array object. Be sure to read on what their pop and push methods do!

By definition, a [Queue]()), like a Stack, should allow uses to use two methods: #enqueue, which places it's input at the back of the queue, and #dequeue which removes an element from the front of the queue and returns it.

Some languages have builtin queues ready for use; but Ruby and Javascript do not explicitly have queues or either of the mentioned functions. However, both languages and have the #shift method which, when used with #push; allows the easy implementation of queues.

Hashes
Maps
https://en.wikipedia.org/wiki/Stack_(abstract_data_type
Ruby
Javascript
https://en.wikipedia.org/wiki/Queue_(abstract_data_type
Ruby
Javascript