Code For LinkedList and Different Operations on Them.
class Node: def init(self, data = None , next = None): self.data = data self.next = next
class LinkedList: def init(self): self.head = None
def insert_at_beginig(self, data):
node = Node(data, self.head)
self.head =node
def print(self):
if self.head is None:
print("empty")
return
itr = self.head
llstr = ''
while itr:
llstr += str(itr.data) + '-->'
itr = itr.next
print(llstr)
def insert_at_end(self,data):
if self.head is None:
self.head = Node(data, None)
return
itr = self.head
while itr.next:
itr = itr.next
itr.next = Node(data,None)
def inster_values(self , data_list):
self.head = None
for data in data_list:
self.insert_at_end(data)
def get_length(self):
count = 0
itr = self.head
while itr:
count +=1
itr = itr.next
return count
def remove_at(self , index):
if index<0 or index >= self.get_length():
raise Exception("Invalid index")
if index == 0:
self.head = self.head.next
return
count = 0
itr = self.head
while itr:
if count == index - 1:
itr.next = itr.next.next
break
itr = itr.next
count += 1
def insert_at(self , index , data):
if index<0 or index >= self.get_length():
raise Exception("Invalid index")
if index == 0:
self.insert_at_beginig(data)
return
count = 0
itr = self.head
while itr:
if count == index - 1:
node = Node(data, itr.next)
itr.next = node
itr = itr.next
count += 1
if name == 'main': ll = LinkedList() # ll.insert_at_beginig(5) # ll.insert_at_beginig(89) # ll.insert_at_end(79) ll.inster_values(['g','m','o' , 'p']) ll.remove_at(2) ll.print()
linked list is a form of a sequential collection and it does not have to be in order. A linked list is made up of independent nodes that may contain any type of node that may contain any type of data and each node has a reference to the next node in the link.
types of linked list are 1 single linked list 2 circular linked list 3 double linked list 4 circular doubly linked list
class Node: def init(self, value=None): self.value = value self.next = None
class SLinkedList: def init(self): self.head = None self.tail = None def iter(self): node = self.head while node: yield node node = node.next
def insertSLL(self, value, location):
newNode = Node(value)
if self.head is None:
self.head = newNode
self.tail = newNode
else:
if location == 0:
newNode.next = self.head
self.head = newNode
elif location == 1:
newNode.next = None
self.tail.next = newNode
self.tail = newNode
else:
tempNode = self.head
index = 0
while index < location - 1:
tempNode = tempNode.next
index += 1
nextNode = tempNode.next
tempNode.next = newNode
newNode.next = nextNode
def traverseList(self):
if self.head is None:
print("The Singly Linked List does not exist")
else:
node = self.head
while node is not None:
print(node.value)
node = node.next
def searchSLL(self, nodeValue):
if self.head is None:
print("The Singly Linked List does not exist")
else:
node = self.head
while node is not None:
if node.value == nodeValue:
return node.value
node = node.next
return "The node does not exist in this SLL"
def deleteNode(self, location):
if self.head is None:
return "The Singly Linked List does not exist"
else:
if location == 0:
if self.head == self.tail:
self.head = None
self.tail = None
else:
self.head = self.head.next
elif location == 1:
if self.head == self.tail:
self.head = None
self.tail = None
else:
node = self.head
while node is not None:
if node.next == self.tail:
break
node = node.next
node.next = None
self.tail = node
else:
tempNode = self.head
index = 0
while index < location-1:
tempNode = tempNode.next
index += 1
nextNode = tempNode.next
tempNode.next = nextNode.next
def deleteEntireSLL(self):
if self.head is None:
print("SLL does not exist")
else:
self.head = None
self.tail = None
singlyLinkedList = SLinkedList() singlyLinkedList.insertSLL(44,6) print([node.value for node in singlyLinkedList])
singlyLinkedList.insertSLL(3,1) singlyLinkedList.insertSLL(4,1) print([node.value for node in singlyLinkedList]) singlyLinkedList.insertSLL(5,3) print([node.value for node in singlyLinkedList])
#for input in linked list