Last active
October 8, 2020 11:03
-
-
Save Abhayparashar31/ac685d4ce1c88f4c10c95d10e899fc34 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A single node of a singly linked list | |
class Node: | |
# constructor | |
def __init__(self, data = None, next=None): | |
self.data = data | |
self.next = next | |
# A Linked List class with a single head node | |
class LinkedList: | |
def __init__(self): | |
self.head = None | |
############################################# | |
############ INSERT NODES ################### | |
############################################# | |
######### AT THE END (APPEND) ############### | |
def append(self, data): | |
newNode = Node(data) ## Taking the data in the as a newNode | |
if(self.head): ## if self.head is not None then means :=>> Linked list is not empty | |
current = self.head ## define current node as head node | |
while(current.next):## While current node is not None | |
current = current.next# increase the current node by one node | |
current.next = newNode ## current node next is equal to newNode | |
else: | |
self.head = newNode ## linked list is empty then simply add the new node | |
######### AT THE START (PREPAND) ############# | |
def prepand(self,data): | |
newNode = Node(data) ## Create a node | |
newNode.next = self.head ## Ponit the next of the new node to the head | |
self.head = newNode ## paas the head to the new node | |
######### AFTER A NODE ####################### | |
def add(self,prev_node,data): | |
if not prev_node: | |
print("Previous node is not in the list") | |
return | |
else: | |
newNode = Node(data) | |
newNode.next = prev_node.next | |
prev_node.next = newNode | |
######### PRINTING ALL THE ELEMENTS ############## | |
def printLL(self): | |
current = self.head ## current node as head | |
while(current): ## While current node is not None | |
print(current.data) ## Print the data part of the node |data|next| | |
current = current.next ## Increase the current node with one node | |
################################################## | |
############ DELETE NODES ######################## | |
################################################## | |
def delete_node(self,key): | |
current = self.head | |
######### AT THE START ########################## | |
if current and current.data == key: | |
self.head = current.next | |
current = None | |
return | |
######### IN THE MIDDLE AND THE END ############ | |
prev = None | |
while current and current.data != key: | |
prev = current | |
current = current.next | |
if current is None: | |
return | |
else : | |
prev.next = current.next | |
current = None | |
################################################## | |
######## DELETE NODE AT A GIVEN POSITION ####### | |
################################################## | |
def delete_node_at_pos(self,pos): | |
current = self.head | |
if pos == 0: | |
self.head = current.next | |
current = None | |
count = 1 | |
prev = None | |
while current and count != pos: | |
prev = current | |
current = current.next | |
count+=1 | |
if current is None: | |
return | |
else: | |
prev.next = current.next | |
current = None | |
################################################## | |
###### CALCULATE THE LENGTH OF THE LINKED LIST ### | |
################################################## | |
def length(self): | |
current = self.head | |
length = 0 | |
while(current): | |
length+=1 | |
current = current.next | |
return length | |
# Singly Linked List with insertion and print methods | |
LL = LinkedList() | |
LL.prepand("E") | |
LL.append("A") | |
LL.add(LL.head.next,"F") | |
LL.append("B") | |
LL.append("C") | |
LL.printLL() | |
print("\n") | |
LL.delete_node("B") | |
LL.printLL() | |
print("\n") | |
LL.delete_node_at_pos(0) | |
LL.printLL() | |
print("\n") | |
print(LL.length()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment