Progress

Python Program for Singly Linked List

Python Program for Singly Linked List

Problem Description: Write a Python program for the Singly linked list which supports insertion and deletion operation.

You may also like Linked list complete guide.

Description: Singly linked list or simply linked list are a collection of nodes which connect to each other by pointers.

Every node in the singly linked list consists of two parts, data and reference to the next node nextNode.

Singly linked list

Here is the Python class of the Node.

node.py
Copy
class Node:
    def __init__(self, data):
        self.nextNode = None
        self.data = data

Here is a detailed explanation on linked list.

Python Program: Singly Linked List

singlyLinkedList.py
Copy
class Node:
    def __init__(self, data):
        self.nextNode = None
        self.data = data


class SinglyLinkedList:
    def __init__(self):
        self.head = None

    def insert(self, data):
        if(self.head is None):
            self.head = Node(data)
        else:
            # Iterate to last node of linked list
            pointer = self.head
            while(pointer.nextNode is not None):
                pointer = pointer.nextNode
            # Set reference of nextNode pointer of last node to the new node.
            pointer.nextNode = Node(data)

    def delete(self, data):
        pointer = self.head
        while(pointer.nextNode.data!=data):
            pointer = pointer.nextNode
            if(pointer.data!=data and pointer.nextNode is None):
                print(pointer.data)
                return 0
        pointer.nextNode = pointer.nextNode.nextNode

    def traverse(self):
        pointer = self.head
        while(1):
            print(pointer.data, end="")
            if(pointer.nextNode is None):
                print()
                break
            print("", end=" => ")
            pointer = pointer.nextNode


# Driver Code
if __name__ == "__main__":
    myList = SinglyLinkedList()
    print("*"*5+" Singly Linked List HolyCoders.com "+5*"*")
    while(True):
        print("*"*50)
        el = int(input("1 to Insert\n2 for Deletion\n3 to traverse\n4 to exit\n"))
        if(el == 1):
            item = input("Enter Element to insert in Linked List\n")
            myList.insert(item)
        if(el == 2):
            item = input("Enter Element to delete in Linked List\n")
            print("Element not found" if myList.delete(item)==0 else "Deleted")
        if(el == 3):
            myList.traverse()
        if(el == 4):
            break
Output
***** Singly Linked List HolyCoders.com *****
**************************************************
1 to Insert
2 for Deletion
3 to traverse
4 to exit
1
Enter Element to insert in Linked List
5
**************************************************
1 to Insert
2 for Deletion
3 to traverse
4 to exit
1
Enter Element to insert in Linked List
7
**************************************************
1 to Insert
2 for Deletion
3 to traverse
4 to exit
1
Enter Element to insert in Linked List
9
**************************************************
1 to Insert
2 for Deletion
3 to traverse
4 to exit
1
Enter Element to insert in Linked List
11
**************************************************
1 to Insert
2 for Deletion
3 to traverse
4 to exit
3
5 => 7 => 9 => 11
**************************************************
1 to Insert
2 for Deletion
3 to traverse
4 to exit
2
Enter Element to delete in Linked List
9
Deleted
**************************************************
1 to Insert
2 for Deletion
3 to traverse
4 to exit
3
5 => 7 => 11
**************************************************
1 to Insert
2 for Deletion
3 to traverse
4 to exit

The insert method inserts the data on the head position if it is empty or at the last of the linked list.

The delete method takes data as an argument. It finds the first occurrence of the element in the linked list and deletes it.

Share the Post ;)

Related Posts