Skip to main content

Linked List using PYTHON

This is a gentle intro into data structures using python.This is my personal learning experience and to take a note of what i am learning.Please feel free to comment

In this post I have tried to implement a simple Linked List using Python.

First I have created a file named LList.py which will contain the following code.Then i would be using this as a module in my main file to create a implementation using these


#Single node creation class
class Element:
    def __init__(self, x):
        self.data = x
        self.next = None

#Linked List implementation class
class LinkedList:
    def __init__(self):
        self.head = None
        self.tail = None

    def append(self, x):
        e = Element(x)
        if self.head is None:
            self.head = e
            self.tail = e
        else:
            self.tail.next = e
            self.tail = e

    def showlist(self):
        e = self.head
        while e.next is not None:
            print e.data
            e = e.next
        print e.data


Now lets make a new file called ListMain.py and have the following code:

import LList
#Adding 5 elements to list and showing it
if __name__ == '__main__':
    testList = LList.LinkedList()
    testList.append(1)
    testList.append(2)
    testList.append(3)
    testList.append(4)
    testList.append(5)
    testList.showlist()


Try running in command line the : python ListMain.py 


Hope it was useful :)





Comments

  1. Brilliant example to demonstrate the use of "self" in Python.

    Please use a monospaced font for your code block. I think the Classic Blogger template will look much better than Dynamic Views. :)

    ReplyDelete

Post a Comment

Popular posts from this blog

Introduction to GIT and GITHUB

In this series I would be covering the basic operation of  GIT so that even a new comer can easily feel comfortable with the terminologies and usage of git system. The first post is just a brief introduction that will help you clarify the questions raising about this new thing. What is a Git? Git is simply a software that helps you manage your source code.That means each change that you make to a source code is recorded in its history.So it can also be called as Source Code managemnt system. To use git for a particular project all you need is to initialize the current project folder so that it will be tacked by the git system.I will show it in further documents about how to do it. Every Git working directory(or you can say your project folder) is a full-fledged repository with complete history and full version tracking capabilities, not dependent on network access or a central server. What is GitHub ? In layman terms you can think of

PyCache - A simple, yet extensible in memory Python caching library/framework

https://github.com/Abhisar/PyCache A Small library/extensible framework that aims to solve the problem of needing a cache while coding small/medium scale python projects without depending on 3rd party cache systems. PyCache This library aims to solve the problem of generic object(Python) caching without depending on 3rd party caching systems like Memcached or Redis for cases where it isn't really required. Library has been written in such a way it can be extensible by following standards through implementations of Python Abstract Base Classes. pycache folder  : It has all the base classes. One that ensures ensures all cache schemes follow a similar implemntation i.e https://github.com/Abhisar/PyCache/blob/master/pycache/BaseCache.py  and Other one is for the Objects to be cacheable they have to subclass this Base class  https://github.com/Abhisar/PyCache/blob/master/pycache/BaseCacheable.py CacheImplenations folder : This folder contains different caching schemes. Cur

Write a program such that given a BST,return the size of largest subtree that lies within a given range[a,b].

Write a program such that given a BST,return the size of largest subtree that lies within a given range[a,b]. N.B: This  exact question was asked in a Google interview.  This problem when looked at first looks simple and may be for some it it but for me when it wasn't because when I started coding it I got all confused between recursions and the flow the algorithm will take.So i started simple on pen and paper tried for a couple of hours and could make it work . Point to be noted here is that the following approach here I took is not the most  efficient way but yes it does works perfectly. Steps or rather my thoughts : I had to touch each node so that I can calculate the size of the subtree in case the tree below and the node itself lied in the range.And after i calculate it I have maintained a global variable that stores the current max and when new value comes it compares it with existing one and updates it with the max value between them. To do so ,we