Skip to content

Instantly share code, notes, and snippets.

@hasinur1997
Last active April 30, 2021 21:12
Show Gist options
  • Save hasinur1997/099f0f443a0a1c9ff74f999161c136d5 to your computer and use it in GitHub Desktop.
Save hasinur1997/099f0f443a0a1c9ff74f999161c136d5 to your computer and use it in GitHub Desktop.

Implementation of linked list

Node Class

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

LinkedList Class

class LinkedList:
    # Initialize
    def __init__(self):
        self.head = None
        self.counter = 0
    
    # Add node top of the list
    def append(self, data):
        newNode = Node(data)

        if self.head is None:
            self.head = newNode
            self.counter = self.counter + 1
        else:
            currentNode = self.head

            while currentNode.next is not None:
                self.counter = self.counter + 1
                currentNode = currentNode.next

            currentNode.next = newNode
            
    # Add node at the end of the list
    def prepend(self, data):
        newNode = Node(data)

        if self.head is None:
            self.counter = self.counter + 1
            self.head = newNode
        else:
            self.counter = self.counter + 1
            temp = self.head
            self.head = newNode
            newNode.next = temp
    
    # Insert node at any position of the list
    def insert(self, insert_at, data):
        newNode = Node(data)
        
        if self.head is None:
            print("The list is empty")
        elif insert_at == 0 or insert_at == 1:
          temp = self.head
          self.head = newNode
          newNode.next = temp
        else:
            destination = insert_at - 1
            current_position = 1
            currentNode = self.head

            while current_position is not destination:
                current_position = current_position + 1
                currentNode = currentNode.next

            temp = currentNode.next
            currentNode.next = newNode
            newNode.next = temp
    
    # Update node at any position
    def update(self, position, data):
        if self.head is None:
            print("The list is empty")

        else:
            currentNode = self.head
            current_position = 1
            
            while current_position is not position:
                current_position = current_position + 1
                currentNode = currentNode.next
            currentNode.data = data
    
    # Remove node from the end of the list
    def pop(self):
        if self.head is None:
            print("The list is empty. Nothing to delete")
        else:
            if self.head.next is None:
                self.head = None
                return
            currentNode = self.head
            while currentNode.next.next is not None:
                currentNode = currentNode.next

            currentNode.next = None
    
    # Remove node from top of the list
    def shift(self):
        if self.head is None:
            print("The list is empty")
        else:
            self.head = self.head.next
    
    # Remove node from at any position of the list        
    def remove(self, position):
        if self.head is None:
            print("The list is empty. Nothing to print")
        elif position == 1:
            self.shift()
        elif position == self.counter:
            self.pop()
        else:
            current_position = 1
            destination = position - 1

            currentNode = self.head
            while current_position is not destination:
                currentNode = currentNode.next
                current_position = current_position + 1

            temp = currentNode.next.next
            currentNode.next = temp
    
    # Get total numbers of node of the list
    def count(self):
        return self.counter
    
    # Find node using the value
    def find(self, item):
        if self.head is None:
            print("This is empty, Nothing to search")
            return
        else:
            currentNode = self.head
            foundNode = None
            while currentNode is not None:
                if item == currentNode.data:
                    foundNode = currentNode
                    break
                currentNode = currentNode.next

        return foundNode
    
    # Print the list
    def display(self):
        if self.head is None:
            print("The list is empty, Nothing to print")
        else:
            currentNode = self.head

            while currentNode is not None:
                print(currentNode.data)
                currentNode = currentNode.next
                

Basic Usages

mylist = LinkedList()

mylist.append(10)
mylist.append(20)
mylist.append(30)
mylist.append(50)

mylist.display()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment