Skip to content
Araz Shah
Menu
  • Home
  • About me
  • Contact me
  • CV
  • Online Courses
    • Apply Now !
    • In-Depth
    • Courses
      • Concepts
      • Python Course
      • GIS Developer Course
    • Price
Menu

Python Linked lists

Posted on October 3, 2023 by admin

Linked lists are a fundamental data structure in computer science and can be used in various real-world scenarios. In this tutorial, I’ll provide a simple example of implementing and using a singly linked list in Python, along with best practices.

Scenario: Imagine you’re building a music playlist application, and you want to create a playlist of songs. Each song will be represented as a node in a singly linked list. We’ll focus on the basic operations of adding songs to the playlist, removing songs, and displaying the playlist.

Here’s how you can create a linked list and implement these operations:

# Define a Node class to represent a song in the playlist
class Node:
    def __init__(self, title, artist):
        self.title = title
        self.artist = artist
        self.next = None  # Reference to the next song in the playlist

# Define a Playlist class to manage the linked list of songs
class Playlist:
    def __init__(self):
        self.head = None  # Reference to the first song in the playlist

    def add_song(self, title, artist):
        new_song = Node(title, artist)
        if not self.head:
            self.head = new_song
        else:
            current_song = self.head
            while current_song.next:
                current_song = current_song.next
            current_song.next = new_song

    def remove_song(self, title):
        if not self.head:
            return  # Playlist is empty
        if self.head.title == title:
            self.head = self.head.next
            return
        current_song = self.head
        while current_song.next:
            if current_song.next.title == title:
                current_song.next = current_song.next.next
                return
            current_song = current_song.next

    def display_playlist(self):
        current_song = self.head
        if not current_song:
            print("Playlist is empty.")
            return
        while current_song:
            print(f"{current_song.title} by {current_song.artist}")
            current_song = current_song.next

# Create a playlist
my_playlist = Playlist()

# Add songs to the playlist
my_playlist.add_song("Song1", "Artist1")
my_playlist.add_song("Song2", "Artist2")
my_playlist.add_song("Song3", "Artist3")

# Display the playlist
print("Current Playlist:")
my_playlist.display_playlist()

# Remove a song from the playlist
my_playlist.remove_song("Song2")

# Display the updated playlist
print("\nUpdated Playlist:")
my_playlist.display_playlist()

Output:

Current Playlist:
Song1 by Artist1
Song2 by Artist2
Song3 by Artist3

Updated Playlist:
Song1 by Artist1
Song3 by Artist3

In this example, we define a Node class to represent individual songs and a Playlist class to manage the linked list of songs. The Playlist class includes methods to add, remove, and display songs. This is a simplified real-world example, but it illustrates the fundamental concepts of linked lists in Python.

Category: python, Tutorials

Post navigation

← group the iterable
iterating over dates in Python →

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Geospatial Risk Assessment: A Python Approach
  • Analyzing Employee Arrival Patterns and Delays Using Geospatial Data
  • Real-Time GPS Tracking on a Web Map using FastAPI & Leaflet
  • How to Create a Simple WebGIS with FastAPI, PostGIS, and Leaflet.js
  • Graph Coloring: How Many Colors Do You Need?

Archives

  • May 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • September 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • September 2023
  • August 2023
  • April 2023

Categories

  • Courses
  • Events
  • GIS
  • Linux
  • News
  • programming
  • python
  • Tutorials
  • Videos
  • May 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • September 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • September 2023
  • August 2023
  • April 2023
  • Courses
  • Events
  • GIS
  • Linux
  • News
  • programming
  • python
  • Tutorials
  • Videos

Araz Shahkarami

I’m a software enthusiast with a deep love for crafting robust and efficient solutions. My journey into the world of programming began several years ago when I was introduced to the world of code. Since then, I’ve been on an exhilarating ride of learning, problem-solving, and continuous improvement.

© 2025 Araz Shah | Powered by Minimalist Blog WordPress Theme