Bridging Music and Coding

Introduction

Welcome to Bridging Music and Coding! I’m excited to share a unique approach to understanding data structures by drawing parallels with musical concepts. With over 20 years of experience teaching music theory, I’ve always been fascinated by the structured beauty of scales, chords, and harmonic progressions. Now, as I dive into the world of coding, I see similar patterns that resonate with the concepts I teach.

In this series, I’ll connect data structures with musical analogies to make learning coding both intuitive and fun. Let’s start with a fundamental concept in coding—linked lists—and explore how they relate to something familiar in music: the C Major scale.


Linked Lists and the C Major Scale

A linked list is a data structure in coding where each element (called a “node”) points to the next one in line. This setup is much like a sequence of notes in a melody, where each note leads to the next. In this post, we’ll create a linked list in Python to represent the C Major scale and see how we can play it note by note.

Linked List in Music Terms

Imagine each note in the C Major scale (C, D, E, F, G, A, B) as a “node” in our linked list:

  • Each note knows only the next note in the sequence.
  • To play the scale, we start at the beginning and move from one note to the next until we reach the end.

In Python, we can create this linked list structure by defining two classes: Note (each individual note) and CMajorScale (the full scale as a linked list).


Python Code for the C Major Scale Linked List

Here’s the code to build a C Major scale using linked lists. Feel free to copy and paste this into your coding environment to see it in action!

#PYTHON
# Each note in the C Major scale becomes a node in our linked list
class Note:
    def __init__(self, value):
        self.value = value    # The note (C, D, E, etc.)
        self.next = None      # Link to the next note

class CMajorScale:
    def __init__(self):
        self.head = None      # The start of the scale (first note)

    def add_note_to_scale(self, value):
        new_note = Note(value)
        if not self.head:     # If the scale is empty, start with this note
            self.head = new_note
        else:
            current = self.head
            while current.next:  # Move to the end of the scale
                current = current.next
            current.next = new_note  # Add the new note at the end

    def play_scale(self):
        current = self.head
        while current:
            print(current.value)  # "Play" each note in order
            current = current.next

# Creating the C Major scale
c_major_scale = CMajorScale()
c_major_scale.add_note_to_scale("C")
c_major_scale.add_note_to_scale("D")
c_major_scale.add_note_to_scale("E")
c_major_scale.add_note_to_scale("F")
c_major_scale.add_note_to_scale("G")
c_major_scale.add_note_to_scale("A")
c_major_scale.add_note_to_scale("B")

# Playing the scale
c_major_scale.play_scale()

Explanation of the Code

  1. Note Class: Represents a single note in our linked list, with a value (the pitch) and next (the link to the following note).
  2. CMajorScale Class: This represents the full scale.
    • The add_note_to_scale method adds each note in the order of the C Major scale.
    • The play_scale method “plays” the scale, starting at the head and moving through each note until the end.

When we run play_scale, it outputs each note in the order: C, D, E, F, G, A, B, just like playing through the C Major scale.


Final Thoughts

I hope this introduction to linked lists through music makes the concept easier to grasp. Just as each note flows to the next, data in a linked list connects in sequence, making it ideal for situations where we want to maintain order with flexibility in adding or removing items.

Stay tuned for the next post, where we’ll explore Stacks and Queues and see how these structures are like building chords or rhythmic patterns. Happy coding and music-making!

How might you modify this linked list to allow for reverse playback of the C Major scale?

  • Hint: Consider adding a previous pointer to each Note, turning the linked list into a doubly linked list.

#LydiaBandy #LydiasPianoStudio #MusicandCoding #LinkedLists #CMajorScale #MusicTheory #DataStructures #Tech #SoftwareEngineer #TechInterview

Published by Lydia Bandy

Harpist, Pianist, Award Winning Music Teacher, and Software Engineer🎼👨‍💻 Bridging music and technology with 25 years experience as a harpist, pianist, and award-winning instructor! Having 3-4 years as a software engineer and web developer, my goal is to teach coding and music theory to the world🌎🎵 Facebook/Instagram/YouTube: @LydiasPianoStudio

Leave a comment