Introduction
A link list is a data structure, that is a collection of nodes and each node is connected via a link. This is also called a linear data structure.
Every node has two parts
☛Data: It contains a data element
☛Link/Next: It contains the link of the next node
Let's see how does a linked list looks like.
Types Of Linked List
☛Singly linked list
☛Doubly linked list
☛Circular linked list
Operations of Linked List
- Traversal: Traverse all the nodes.
- Insertion: Insert a new node. Insertion can take place at the front or end or after a certain position.
- Deletion: Delete a node from a linked list. Positions may be given.
- Searching: Searching for a value in a linked list.
- Update: Update the existing value by a new value of a node.
What can you learn from this section?
☛Create a linked list in python
☛Traversal operation of a linked list
Code
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def addNode(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
return
temp = self.head
while(temp.next):
temp = temp.next
temp.next = new_node
def printNode(self):
temp = self.head
while(temp):
print(temp.data, end='->')
temp = temp.next
print("\n")
if __name__ == "__main__":
# Declaring a object of LinkedList class
obj = LinkedList()
obj.addNode(10)
obj.addNode(20)
obj.addNode(30)
obj.addNode(40)
obj.addNode(20)
print("The Linked List is: ")
obj.printNode()
Output
The Linked List is:
10->20->30->40->20->
Exercise
☛Create a linked list using python. In this case, take the data from the user.