You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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