<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Michael Lin on Medium]]></title>
        <description><![CDATA[Stories by Michael Lin on Medium]]></description>
        <link>https://medium.com/@devmichaellin?source=rss-8c357f0fa61d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*w81F8jvHXfdcq92Q</url>
            <title>Stories by Michael Lin on Medium</title>
            <link>https://medium.com/@devmichaellin?source=rss-8c357f0fa61d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 07:31:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@devmichaellin/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Implementing (Elementary) Abstract Data Types In Python]]></title>
            <link>https://medium.com/@devmichaellin/implementing-elementary-abstract-data-types-in-python-fd26cf6aee7e?source=rss-8c357f0fa61d------2</link>
            <guid isPermaLink="false">https://medium.com/p/fd26cf6aee7e</guid>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[python3]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Michael Lin]]></dc:creator>
            <pubDate>Mon, 13 Aug 2018 17:31:40 GMT</pubDate>
            <atom:updated>2018-09-16T15:56:37.008Z</atom:updated>
            <content:encoded><![CDATA[<p>When I first heard the term “abstract data type,” I let out a very audible and over-exasperated sigh. Great, another theoretical concept I would probably never use again.</p><p>I couldn’t have been more wrong.</p><p>Abstract data types, or ADTs for short, are practical ways to model data. Personally, I struggle to appreciate theory when I don’t know about its real world applications, but it wasn’t long before I realized that some of them are already parts of everyday life.</p><p>Queues, for example, are just ways of organizing data more efficiently. You might see an example of a queue in real life while lining up to get a drink at the bar. It would be chaotic to have everybody fighting to get the bartender’s attention. (Which, unfortunately, happens relatively often.) One solution to this could be to have everybody line up. This way, it’s easier for the bartender to see who’s at the front of the line and for the customers to see where they should get in line. A customer might also find it helpful to see the length of the line, to gauge if he or she even wants to wait in line.</p><p>Here’s a GIF of an unlucky mouse and two queues.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F100mhETqKYJNf2%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F100mhETqKYJNf2%2Fgiphy.gif&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F100mhETqKYJNf2%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="348" frameborder="0" scrolling="no"><a href="https://medium.com/media/d563bbccda9e3f5774d23ea9df84c5d8/href">https://medium.com/media/d563bbccda9e3f5774d23ea9df84c5d8/href</a></iframe><p>To implement a queue in Python, we create a class and initialize it with an empty list. One of the properties of a queue is that the first elements queued are the first ones out. This makes sense if we return to our bar example. People who get in line earlier will be served earlier. We can define an “add” method on our class to represent enqueueing, which will add somebody to the back of the line. In this example, we will be defining the back of the line to be toward the left and the front of the line to be toward the right. It doesn’t really matter which is which as long as you are consistent in making them on the opposite ends. To add somebody to the front of the line, we use the list insert() method at the 0th index.</p><pre>class Queue:</pre><pre>    def __init__(self):</pre><pre>        self.people = []</pre><pre>    def add(self, person):</pre><pre>        self.people.insert(0, person)</pre><p>It’s also good to note that the people who leave the line do so from the front, after they’ve been served. It wouldn’t make sense for somebody from the back to leave before they’ve been served. Since we defined our front to be the left, our dequeue method will remove from the right. We can do this easily utilizing the list pop() method.</p><pre>class Queue:</pre><pre>    def __init__(self):</pre><pre>        self.people = []</pre><pre>    def add(self, person):</pre><pre>        self.people.insert(0, person)</pre><pre>    def remove(self):</pre><pre>        return self.people.pop()</pre><p>Another thing our customers can do is check the size of the line. Thankfully, Python has a built in function, len(), to find the length of a list!</p><pre>class Queue:</pre><pre>    def __init__(self):</pre><pre>        self.people = []</pre><pre>    def add(self, person):</pre><pre>        self.people.insert(0, person)</pre><pre>    def remove(self):</pre><pre>        return self.people.pop()</pre><pre>    def size(self):</pre><pre>        return len(self.people)</pre><p>There are more methods we can add on the class, but we have the main ones covered. A use case for queues that might interest gamers is in chaining skills. In a game where players can activate skills more quickly than they animate, a queue could be used to remember the order of activation. (For example, Monster Hunter probably uses some kind of skill queue)</p><p>Another abstract data type we often see is the stack.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2Fpu44TP3JoqdDa%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2Fpu44TP3JoqdDa%2Fgiphy.gif&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2Fpu44TP3JoqdDa%2Fgiphy-downsized-large.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="435" frameborder="0" scrolling="no"><a href="https://medium.com/media/6b25b7b15a5ad65fac8f315a2d988325/href">https://medium.com/media/6b25b7b15a5ad65fac8f315a2d988325/href</a></iframe><p>A stack can be easily visualized with circular disc magnets like in the GIF above but it could be created with a pile of any objects. In a stack, the last element added must be the first one removed. There’s no way to get to the bottom disc unless the ones on top are removed first. In Python, a stack class is fairly simple to create. We just initialize it with an empty list and add methods that allow the addition and removal of an element from one side.</p><pre>class Stack:</pre><pre>    def __init__(self):</pre><pre>        self.elements = []</pre><pre>    def push(self, element):</pre><pre>        self.elements.append(element)</pre><pre>    def pop(self):</pre><pre>        return self.elements.pop()</pre><p>The implementation of a stack might seem trivial, but there are some cool use cases for it. For example, the classic balancing parenthesis problem could be solved with a stack. Stacks could also be used to convert decimals to binary numbers.</p><p>The next abstract data type I’ll introduce is the linked list. Unfortunately, there isn’t much practical use for linked lists in Python. They’re useful to know for the theory, mainly to be used as a stepping stone to learn other data structures. For the purposes of this post, I’ll be referring to a singly linked list as a linked list.</p><p>To begin with the implementation of a linked list, we’ll create a Node class.</p><pre>class Node:</pre><pre>    def __init__(self, data):</pre><pre>        self.data = data</pre><pre>        self.next = None</pre><pre>    def getData(self):</pre><pre>        return self.data</pre><pre>    def getNext(self):</pre><pre>        return self.next</pre><pre>    def setNext(self, next):</pre><pre>        self.next = next</pre><p>By default, a node will have the data passed through and no reference to the next node until it’s assigned. It’ll also have three methods on it, which we’ll use when implementing the linked list. We can check the value of the data with getData, check the reference to the next node with getNext, and assign a reference to the next node with setNext.</p><p>A linked list is essentially a collection of nodes where each node points to the next. (Except for the last one) Some people like to visualize it like a chain.</p><pre>class LinkedList:</pre><pre>    def __init__(self):</pre><pre>        self.head = None</pre><pre>    def add(self, value):</pre><pre>        temp = Node(value)</pre><pre>        temp.setNext(self.head)</pre><pre>        self.head = temp</pre><pre>    def length(self):</pre><pre>        current = self.head</pre><pre>        count = 0</pre><pre>        while current != None:</pre><pre>            count += 1</pre><pre>            current = current.getNext()</pre><pre>            return count</pre><p>By default, a linked list shouldn’t have anything inside it, so we initialize its “head” property to be “None.” When we add to our linked list, we’re adding onto the tail. We create a new node, set the next node reference to the current head, then reassign the head property to be the new node.</p><p>Suppose we wanted to search the linked list. How would we do that?</p><pre>class LinkedList:</pre><pre>    def __init__(self):</pre><pre>        self.head = None</pre><pre>    def add(self, value):</pre><pre>        temp = Node(value)</pre><pre>        temp.setNext(self.head)</pre><pre>        self.head = temp</pre><pre>    def length(self):</pre><pre>        current = self.head</pre><pre>        count = 0</pre><pre>        while current != None:</pre><pre>            count += 1</pre><pre>            current = current.getNext()</pre><pre>            return count</pre><pre>    def search(self, value):</pre><pre>        current = self.head</pre><pre>        found = False</pre><pre>        while current != None and not found:</pre><pre>            if current.getData() == value:</pre><pre>                found = True</pre><pre>            else :</pre><pre>                current = current.getNext()</pre><pre>                return found</pre><p>To search a linked list, we just need to follow the next node references until we either find what we’re looking for, or until we reach the end of the linked list, in which case the “next” property on the node would be “None.” We can do this with a while loop by keeping track of our current location and status of if we found the element or not. Each iteration of the loop would traverse the linked list using its reference to the next node. To find the length of the linked list, we would keep moving on to the next node until we find the one with “None,” as its “next” property.</p><p>While adding to or searching a linked list might be straightforward, removing a node might be a little tricky. If you remove a reference to the next node, it’s true that you’re removing that next node, but you’re also effectively removing that next node’s reference to any potential nodes further down the chain. To prevent the problem, we temporarily hold a reference to the previous node, so we can change its reference from the current node to the next node.</p><pre>class LinkedList:</pre><pre>    def __init__(self):</pre><pre>        self.head = None</pre><pre>    def add(self, value):</pre><pre>        temp = Node(value)</pre><pre>        temp.setNext(self.head)</pre><pre>        self.head = temp</pre><pre>    def length(self):</pre><pre>        current = self.head</pre><pre>        count = 0</pre><pre>        while current != None:</pre><pre>            count += 1</pre><pre>            current = current.getNext()</pre><pre>            return count</pre><pre>    def search(self, value):</pre><pre>        current = self.head</pre><pre>        found = False</pre><pre>        while current != None and not found:</pre><pre>            if current.getData() == value:</pre><pre>                found = True</pre><pre>            else :</pre><pre>                current = current.getNext()</pre><pre>                return found</pre><pre>    def remove(self, value):</pre><pre>        current = self.head</pre><pre>        previous = None</pre><pre>        found = False</pre><pre>        while not found:</pre><pre>            if current.getData() == value:</pre><pre>                found = True</pre><pre>            else :</pre><pre>                previous = current</pre><pre>                current = current.getNext()</pre><pre>        if previous == None:</pre><pre>            self.head = current.getNext()</pre><pre>        else:</pre><pre>            previous.setNext(current.getNext())</pre><p>We traverse the linked list similar to how we did when we were searching it, except we’re holding a reference to the previous node. After finding the item we want to remove, we’ll just set the previous node’s “next,” property to be the current node’s “next,” property, so we don’t lose the next references. (We’re making the assumption that what we want removed is already in the linked list, otherwise why would we try to remove it?)</p><p>Earlier, I mentioned that this linked list was specifically a singly linked list. It can easily be made into a doubly linked list by keeping a reference to the previous node in the node itself, rather than just in a temporary variable when looking to remove it. Technically, this takes up more memory, but whether the trade off is worth it should be determined on a case by case basis. Additionally, removing elements is more efficient than the built in lists, because the built in lists basically create an entirely new list without the element that needs to be removed.</p><p>Thank you for your time! Hopefully I contributed at least a little to your understanding of data structures, which are implementations of abstract data types. I appreciate and welcome all feedback, questions, and concerns.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fd26cf6aee7e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>