Ordered Singly Linked List

class Node:
    def __init__(self,data):
        self.data = data
        self.next = None
        
    def setData(self,data):
        self.data = data
    
    def getData(self):
        return self.data
    
    def setNext(self, next):
        self.next = next
        
    def getNext(self):
        return self.next
    
    
class LinkedList:
    def __init__(self):
        self.head = None
        
    def isEmpty(self):
        return self.head == None
    
    def length(self):
        current = self.head
        count = 0
        while current != None:
            count += 1
            current = current.getNext()
        return count
    
    def search(self,item):
        found = False
        stop = False
        current = self.head
        while current != None and not found and not stop:
            if current.getData() == item:
                found = True
            else:
                if current.getData() > item:
                    stop = True
                else:
                    current = current.getNext()
                
        return found
    
    def insertion(self,item):
        if self.head == None:
            node = Node(item)
            node.setNext(self.head)
            self.head = node
        else:
            current = self.head
            previous = None
            stop = False
            
            while current != None and not stop:
                if current.getData() > item:
                    stop = True
                else:
                    previous = current
                    current = current.getNext()
            
            node = Node(item)        
            previous.setNext(node)
            node.setNext(current)
            

    def remove(self,item):
        found = False
        current = self.head
        previous = None
        
        while current.getNext() != None and not found:
            if current.getData() == item:
                print current.getData()
                print previous.getData()
                found = True
                previous.setNext(current.next)
            else:
                previous = current
                current = current.getNext()
                
    def printList(self):
        current = self.head
        
        while current != None:
            print current.getData()
            current = current.getNext()        
        
            
        
        
if __name__ == "__main__":
    ll = LinkedList()
    ll.insertion(1)
    ll.insertion(2)
    ll.insertion(3)
    ll.insertion(6)
    ll.insertion(4)
    
    
    ll.printList()

Advertisements

One thought on “Ordered Singly Linked List

  1. Pingback: Linked Lists | codeatsociallywired

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s