Friday, February 2, 2018

Python Singly Linke List

Python Link list implementation Tutorial.

Python version of the singly link list implementation
As we remember in singly link list we must have head and rear.
Therefore in our schema we are going to be creating a variable named data which will be our head,
and our pointer will be pointing to null which is our next_node.

C++ Schema :

    struct Node {
        int data;
        node * next = null / node* next;
    }


Then :

    int main (){
        node* rear = null;
        node* head = null;
    }

So the transition is very simple and straight forward in the cpp example the we needed add few other additional variables such temporary variables
to point at the next node. Althought in our case we dont have a pointers in python language we just need to construct that with our constructor (self fucntion)
and initilize it to a variable so we can make it callable in our code.

class Node(object):

    def __init__(self, data=None, next_node=None):
        self.data = data
        self.next_node = next_node

    def get_data(self):
        return self.data

    def get_next(self):
        return self.next_node

    def set_next(self, new_next):
        self.next_node = new_next

Here is another way of implementing of singly link list.
Either way, it will work with no problem.

class Node:
    def __init__(self,val):
        self.val = val
        self.next = None
        # the pointer initially points to nothing

In our simple implementation :

    Insert: inserts a new node into the list

    Size: returns size of list

    Search: searches list for a node containing the requested data and returns that node if found, otherwise raises an error

    Delete: searches list for a node containing the requested data and removes it from list if found, otherwise raises an error

    :::::Resources & Credits LinkedList Source + LinkedList Tests ::::::

import sys

# When we are creating our node, you can think of our node as our basket so we can store data inside. In our case, our object is our basket so we can store data in.

class Node(object):

    def __init__(self, head=None):
        self.head = head

    # our insert method
    def insert(self, data):
        new_node = Node(data)
        new_node.set_next(self.head)
        self.head = new_node

    #Size Method
    def size(self):
        #defining our currenct is very important so we can understand where our current pointer at.

        current = self.head
        count = 0

        while current:
            count += 1
            current = current.get_next()

        return count

    def search(self, data):
        current = self.head
        found = False
        while current and found is False:
            if current.get_data() == data:
                found = True
            else:
                current = current.get_next()
        if current is None:
            raise ValueError("Data not in list")
        return current

    def delete(self, data):
        current = self.head
        previous = None
        found = False
        while current and found is False:
            if current.get_data() == data:
                found = True
            else:
                previous = current
                current = current.get_next()
        if current is None:
            raise ValueError("Data not in list")
        if previous is None:
            self.head = current.get_next()
        else:
            previous.set_next(current.get_next())



def main():
    node3 = Node(3)
    node2 = Node(2)
    node1 = Node(1)

if __name__ == '__main__':
    main()

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Data Structures : Tree Traversals

(Week 2) Tree traversals are one of the most important algorithm in computer science because sometimes we don't have lin...