Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #########################################
- # LinkedList Implementation with python #
- #########################################
- class Node:
- def __init__(self):
- self.value = 0
- self.next = None
- #==== Main Node ====#
- root = Node()
- nodeCount = 0
- #===================#
- def create_new_node():
- return Node()
- def print_node():
- global nodeCount
- temp = root.next
- while temp is not None:
- print(str(temp.value))
- temp = temp.next
- print("TotalNode count: "+ str(nodeCount))
- def insertAtEnd(value):
- global nodeCount
- x = create_new_node()
- x.value = value
- if root.next == None:
- root.next = x
- else:
- temp = root
- while temp.next is not None:
- temp = temp.next
- temp.next = x
- nodeCount += 1
- def insertAtHead(value):
- global nodeCount
- x = create_new_node()
- x.value = value
- if root.next == None:
- root.next = x
- else:
- temp = root.next
- x.next = temp
- root.next = x
- nodeCount += 1
- def find(searchValue):
- current = root
- while current != None:
- if current.value == searchValue:
- break
- else:
- current = current.next
- return current
- # ====================== #
- def deleteFromFirst():
- global nodeCount
- x = -1
- first = root.next
- x = first.value
- if nodeCount == 0:
- return -9999
- if nodeCount == 1:
- root.next = None
- else:
- root.next = first.next
- nodeCount -= 1
- return x
- def deleteFromLast():
- global nodeCount
- current = root.next
- if nodeCount == 0:
- return -9999
- if nodeCount == 1:
- root.next = None
- else:
- while current.next.next != None:
- current = current.next
- current.next = None
- nodeCount -= 1
- def deleteTargetNode(target):
- global nodeCount
- current = root.next
- flag = 0
- if find(target) == None:
- print("search value doesn't not present, ",str(target))
- return
- # if The User Want to Delete the head #
- if root.next.value == target:
- deleteFromFirst()
- return
- # Handels Everything after the head #
- while current != None:
- if target is current.next.value:
- flag = 1
- break
- current = current.next
- # Actual Delete Operations #
- if flag == 1:
- current.next = current.next.next
- nodeCount -= 1
- def reverse_ll():
- global root
- q = None
- p = root.next
- r = p.next
- while r is not None:
- p.next = q
- q = p
- p = r
- r = r.next
- p.next = q
- root.next = p
- def printLastN(nPos):
- count = 0
- contPtr = None #actualContainer
- temp = root.next
- while temp != None:
- if count == nPos:
- contPtr = root.next
- temp = temp.next
- if contPtr != None:
- contPtr = contPtr.next
- count += 1
- if contPtr != None:
- print(str(contPtr.value))
- else:
- return -9999
- def recursionReverse(current):
- if current == None:
- return
- recursionReverse(current.next)
- print(current.value)
- def recursionForward(current):
- if current == None:
- return
- print(current.value)
- recursionForward(current.next)
- def main():
- insertAtEnd(100)
- insertAtEnd(110)
- insertAtEnd(120)
- insertAtEnd(130)
- insertAtEnd(140)
- insertAtEnd(150)
- insertAtEnd(160)
- insertAtEnd(170)
- print("============Print Forward===========")
- recursionForward(root.next)
- insertAtEnd(180)
- print("============Print Reverse===========")
- recursionReverse(root.next)
- if __name__ == "__main__":
- main()
Add Comment
Please, Sign In to add comment