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: linked list python example
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
class SLinkedList:
def __init__(self):
self.head = None
def Atbegining(self, data_in):
NewNode = Node(data_in)
NewNode.next = self.head
self.head = NewNode
# Function to remove node
def RemoveNode(self, Removekey):
HeadVal = self.head
if (HeadVal is not None):
if (HeadVal.data == Removekey):
self.head = HeadVal.next
HeadVal = None
return
while (HeadVal is not None):
if HeadVal.data == Removekey:
break
prev = HeadVal
HeadVal = HeadVal.next
if (HeadVal == None):
return
prev.next = HeadVal.next
HeadVal = None
def LListprint(self):
printval = self.head
while (printval):
print(printval.data),
printval = printval.next
llist = SLinkedList()
llist.Atbegining("Mon")
llist.Atbegining("Tue")
llist.Atbegining("Wed")
llist.Atbegining("Thu")
llist.RemoveNode("Tue")
llist.LListprint()
Example 3: doubly linked list python
class ListNode:
def __init__(self, value, prev=None, next=None):
self.prev = prev
self.value = value
self.next = next
class DoublyLinkedList:
def __init__(self, node=None):
self.head = node
self.tail = node
self.length = 1 if node is not None else 0
def __len__(self):
return self.length
def add_to_head(self, value):
new_node = ListNode(value, None, None)
self.length += 1
if not self.head and not self.tail:
self.head = new_node
self.tail = new_node
else:
new_node.next = self.head
self.head.prev = new_node
self.head = new_node
def remove_from_head(self):
value = self.head.value
self.delete(self.head)
return value
def add_to_tail(self, value):
new_node = ListNode(value, None, None)
self.length += 1
if not self.tail and not self.head:
self.tail = new_node
self.head = new_node
else:
new_node.prev = self.tail
self.tail.next = new_node
self.tail = new_node
def remove_from_tail(self):
value = self.tail.value
self.delete(self.tail)
return value
def move_to_front(self, node):
if node is self.head:
return
value = node.value
if node is self.tail:
self.remove_from_tail()
else:
node.delete()
self.length -= 1
self.add_to_head(value)
def move_to_end(self, node):
if node is self.tail:
return
value = node.value
if node is self.head:
self.remove_from_head()
self.add_to_tail(value)
else:
node.delete()
self.length -= 1
self.add_to_tail(value)
def delete(self, node):
self.length -= 1
if not self.head and not self.tail:
return
if self.head == self.tail:
self.head = None
self.tail = None
elif self.head == node:
self.head = node.next
node.delete()
elif self.tail == node:
self.tail = node.prev
node.delete()
else:
node.delete()
def get_max(self):
if not self.head:
return None
max_val = self.head.value
current = self.head
while current:
if current.value > max_val:
max_val = current.value
current = current.next
return max_val
Example 4: 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()