singly linked list using python code example

Example 1: linked list in python

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

    def get_data(self):
        return self.data

    def set_data(self, data):
        self.data = data

    def get_nextNode(self):
        return self.nextNode

    def set_nextNode(self, nextNode):
        self.nextNode = nextNode


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


    def add_Node(self, data):
        # if empty
        if self.head == None:
            self.head = Node(data)


        # not empty
        else:
            curr_Node = self.head
            
            # if node added is at the start
            if data < curr_Node.get_data():
                self.head = Node(data, curr_Node)
                
            # not at start
            else:
                while data > curr_Node.get_data() and curr_Node.get_nextNode() != None:
                    prev_Node = curr_Node
                    curr_Node = curr_Node.get_nextNode()

                # if node added is at the middle
                if data < curr_Node.get_data():
                    prev_Node.set_nextNode(Node(data, curr_Node))
                

                # if node added is at the last
                elif data > curr_Node.get_data() and curr_Node.get_nextNode() == None:
                    curr_Node.set_nextNode(Node(data))



    def search(self, data):
        curr_Node = self.head
        while curr_Node != None:
            if data == curr_Node.get_data():
                return True

            else:
                curr_Node = curr_Node.get_nextNode()

        return False


    def delete_Node(self, data):
        if self.search(data):
            # if data is found

            curr_Node = self.head
            #if node to be deleted is the first node
            if curr_Node.get_data() == data:
                self.head = curr_Node.get_nextNode()

            else:
                while curr_Node.get_data() != data:
                    prev_Node = curr_Node
                    curr_Node = curr_Node.get_nextNode()
                    
                #node to be deleted is middle
                if curr_Node.get_nextNode() != None:
                    prev_Node.set_nextNode(curr_Node.get_nextNode())

                # node to be deleted is at the end
                elif curr_Node.get_nextNode() == None:
                    prev_Node.set_nextNode(None)

        else:
            return "Not found."

    def return_as_lst(self):
        lst = []
        curr_Node = self.head
        while curr_Node != None:
            lst.append(curr_Node.get_data())
            curr_Node = curr_Node.get_nextNode()

        return lst

    def size(self):
        curr_Node = self.head
        count = 0
        while curr_Node:
            count += 1
            curr_Node = curr_Node.get_nextNode()
        return count

      
## TEST CASES #
test1 = LinkedList()
test2 = LinkedList()
test1.add_Node(20)
test1.add_Node(15)
test1.add_Node(13)
test1.add_Node(14)
test1.delete_Node(17)
print(test1.return_as_lst())
print(test2.size())

Example 2: singly linked list in python

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


class Linkedlist:
    def __init__(self):
        self.head = None
        self.size = 0

    def length(self):
        return self.size

    def is_empty(self):
        return self.size == 0

    def insert_at_the_beginning(self, data):
        self.insert_with_index(0, data)

    def insert_at_the_ending(self, data):
        self.insert_with_index(self.size, data)

    def insert_with_index(self, index, data):
        if index > self.size or index < 0:
            print("check given", index, "index value and enter again")
            return False
        if index == 0:
            self.head = Node(data, self.head)
        else:
            current = self.head
            for i in range(index - 1):
                current = current.next
            current.next = Node(data, current.next)
        self.size += 1

    def peek_top(self):
        return self.peek_index(0)

    def peek_bottom(self):
        return self.peek_index(self.size - 1)

    def peek_index(self, index):
        if index >= self.size or index < 0:
            print("check given", index, "index value and enter again")
            return False
        current = self.head
        for i in range(index):
            current = current.next
        return current.data

    def peek_element(self, data):
        current = self.head
        while current.data != data:
            if current.next is None:
                print("element", data, "not found")
                return False
            current = current.next
        print("element", data, "is found")
        return True

    def delete_top_element(self):
        return self.delete_with_index(0)

    def delete_bottom_element(self):
        return self.delete_with_index(self.size - 1)

    def delete_with_index(self, index):
        if index >= self.size or index < 0:
            print("check given", index, "index value and enter again")
            return False
        self.size -= 1
        if index == 0:
            temp = self.head
            self.head = self.head.next
            return temp.data
        current = self.head
        for i in range(index - 1):
            current = current.next
        temp = current.next
        current.next = current.next.next
        return temp.data

    def delete_with_value(self, data):
        current = self.head
        previous = current
        while current.data != data:
            if current.next is None:
                print("element", data, "not found")
                return False
            previous = current
            current = current.next
        temp = previous.next
        previous.next = current.next
        print("element", data, "is found and deleted")
        self.size -= 1
        return temp.data

    def print_val(self):
        current = self.head
        while current:
            print(current.data, "\b--->", end="")
            current = current.next
        print()


linked_list = Linkedlist()


def trail1():
    linked_list.insert_at_the_beginning(45)
    linked_list.insert_at_the_beginning(65)
    linked_list.insert_at_the_beginning(34)
    linked_list.insert_at_the_beginning(56)
    linked_list.insert_at_the_beginning(78)
    linked_list.insert_at_the_beginning(98)
    linked_list.insert_at_the_beginning(63)
    linked_list.insert_at_the_beginning(31)
    linked_list.print_val()


def trail2():
    linked_list.insert_at_the_beginning(78)
    linked_list.insert_at_the_ending(67778)
    linked_list.insert_at_the_ending(899)
    linked_list.insert_at_the_ending(99)
    linked_list.print_val()
    trail1()


def trail3():
    linked_list.insert_at_the_beginning(34)
    linked_list.insert_at_the_beginning(56)
    linked_list.insert_at_the_beginning(78)
    linked_list.insert_at_the_beginning(31)
    linked_list.insert_at_the_ending(12)
    linked_list.insert_at_the_ending(14)
    linked_list.insert_at_the_ending(56)
    linked_list.insert_with_index(90, 345)
    linked_list.insert_with_index(5, 23)
    print(linked_list.peek_index(2))
    print(linked_list.peek_bottom())
    print(linked_list.peek_top())
    linked_list.peek_element(16)
    linked_list.peek_element(33)
    linked_list.insert_at_the_beginning(128)
    linked_list.insert_at_the_beginning(784)
    linked_list.insert_at_the_beginning(314)
    linked_list.print_val()
    print(linked_list.delete_with_index(5))
    linked_list.print_val()
    print(linked_list.delete_top_element())
    linked_list.print_val()
    print(linked_list.delete_bottom_element())
    linked_list.print_val()
    linked_list.delete_with_value(12)
    linked_list.print_val()
    # trail2()

    # this is siva
    # signing off


if __name__ == "__main__":
    trail3()

Tags:

Cpp Example