n0va_sa

Singily Linked List [python]

Jul 18th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.75 KB | None | 0 0
  1. #########################################
  2. # LinkedList Implementation with python #
  3. #########################################
  4. class Node:
  5.     def __init__(self):
  6.         self.value = 0
  7.         self.next = None
  8.  
  9. #==== Main Node ====#
  10. root = Node()
  11. nodeCount = 0
  12. #===================#
  13. def create_new_node():
  14.     return Node()
  15.  
  16. def print_node():
  17.     global nodeCount
  18.     temp = root.next
  19.     while temp is not None:
  20.         print(str(temp.value))
  21.         temp = temp.next
  22.     print("TotalNode count: "+ str(nodeCount))
  23.  
  24. def insertAtEnd(value):
  25.     global nodeCount
  26.     x = create_new_node()
  27.     x.value = value
  28.     if root.next == None:
  29.         root.next = x
  30.     else:
  31.         temp = root
  32.         while temp.next is not None:
  33.             temp = temp.next
  34.  
  35.         temp.next = x
  36.  
  37.     nodeCount += 1
  38.  
  39. def insertAtHead(value):
  40.     global nodeCount
  41.  
  42.     x = create_new_node()
  43.     x.value = value
  44.     if root.next == None:
  45.         root.next = x
  46.     else:
  47.         temp = root.next
  48.         x.next = temp
  49.         root.next = x
  50.  
  51.     nodeCount += 1
  52.  
  53. def find(searchValue):
  54.     current = root
  55.     while current != None:
  56.         if current.value == searchValue:
  57.             break
  58.         else:
  59.             current = current.next
  60.     return current
  61. # ====================== #
  62. def deleteFromFirst():
  63.     global nodeCount
  64.     x = -1
  65.     first = root.next
  66.     x = first.value
  67.     if nodeCount == 0:
  68.         return -9999
  69.     if nodeCount == 1:
  70.         root.next = None
  71.     else:
  72.         root.next = first.next
  73.  
  74.     nodeCount -= 1
  75.     return x
  76. def deleteFromLast():
  77.     global nodeCount
  78.     current = root.next
  79.     if nodeCount == 0:
  80.         return -9999
  81.     if nodeCount == 1:
  82.         root.next = None
  83.     else:
  84.         while current.next.next != None:
  85.             current = current.next
  86.  
  87.         current.next = None
  88.     nodeCount -= 1
  89. def deleteTargetNode(target):
  90.     global nodeCount
  91.     current = root.next
  92.     flag = 0
  93.  
  94.     if find(target) == None:
  95.         print("search value doesn't not present, ",str(target))
  96.         return
  97.  
  98.     # if The User Want to Delete the head #
  99.     if root.next.value == target:
  100.         deleteFromFirst()
  101.         return
  102.  
  103.     # Handels Everything after the head #
  104.     while current != None:
  105.         if target is current.next.value:
  106.             flag = 1
  107.             break
  108.         current = current.next
  109.     # Actual Delete Operations #
  110.     if flag == 1:
  111.         current.next = current.next.next
  112.         nodeCount -= 1
  113. def reverse_ll():
  114.     global root
  115.     q = None
  116.     p = root.next
  117.     r = p.next
  118.     while r is not None:
  119.         p.next = q
  120.         q = p
  121.         p = r
  122.         r = r.next
  123.     p.next = q
  124.  
  125.     root.next = p
  126. def printLastN(nPos):
  127.     count = 0
  128.     contPtr = None #actualContainer
  129.     temp = root.next
  130.     while temp != None:
  131.         if count == nPos:
  132.             contPtr = root.next
  133.         temp = temp.next
  134.         if contPtr != None:
  135.             contPtr = contPtr.next
  136.         count += 1
  137.     if contPtr != None:
  138.         print(str(contPtr.value))
  139.     else:
  140.         return -9999
  141.  
  142. def recursionReverse(current):
  143.     if current == None:
  144.         return
  145.     recursionReverse(current.next)
  146.     print(current.value)
  147.  
  148. def recursionForward(current):
  149.     if current == None:
  150.         return
  151.     print(current.value)
  152.     recursionForward(current.next)
  153.  
  154. def main():
  155.     insertAtEnd(100)
  156.     insertAtEnd(110)
  157.     insertAtEnd(120)
  158.     insertAtEnd(130)
  159.     insertAtEnd(140)
  160.     insertAtEnd(150)
  161.     insertAtEnd(160)
  162.     insertAtEnd(170)
  163.     print("============Print Forward===========")
  164.     recursionForward(root.next)
  165.     insertAtEnd(180)
  166.     print("============Print Reverse===========")
  167.     recursionReverse(root.next)
  168. if __name__ == "__main__":
  169.     main()
Add Comment
Please, Sign In to add comment