m2skills

rotate ll python

Jan 28th, 2018
766
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.42 KB | None | 0 0
  1. # program to rotate Linked List
  2.  
  3. class LinkedList:
  4.     def __init__(self):
  5.         self.head = None
  6.  
  7.     # returns True is LinkedList is Empty
  8.     def isEmpty(self):
  9.         if self.head is None:
  10.             return True
  11.         else:
  12.             return False
  13.  
  14.     # method adds elements to the left of the Linked List
  15.     def addToStart(self, data):
  16.         # create a temporary node
  17.         tempNode = Node(data)
  18.         tempNode.setLink(self.head)
  19.         self.head = tempNode
  20.         del tempNode
  21.  
  22.     # method adds elements to the right of the Linked List
  23.     def addToEnd(self, data):
  24.         start = self.head
  25.         tempNode = Node(data)
  26.         while start.getNextNode():
  27.             start = start.getNextNode()
  28.         start.setLink(tempNode)
  29.         del tempNode
  30.         return True
  31.  
  32.     # method displays Linked List
  33.     def display(self):
  34.         start = self.head
  35.         if start is None:
  36.             print("Empty List!!!")
  37.             return False
  38.  
  39.         while start:
  40.             print(str(start.getData()), end=" ")
  41.             start = start.link
  42.             if start:
  43.                 print("-->", end=" ")
  44.         print()
  45.  
  46.     # rotate a linked list
  47.     # function removes nodes from the beginning and puts them to the end of the linked list;
  48.     def rotate(self, shiftValue):
  49.         current = self.head
  50.         tail = self.head
  51.  
  52.         # make tail point the last element
  53.         while tail.getNextNode() is not None:
  54.             tail = tail.getNextNode()
  55.  
  56.         for i in range(shiftValue):
  57.             tail.setLink(current)
  58.             current = current.getNextNode()
  59.             tail = tail.getNextNode()
  60.             tail.setLink(None)
  61.  
  62.  
  63.         # let head point to the new head node
  64.         self.head = current
  65.         return
  66.  
  67.  
  68. # node class
  69. class Node:
  70.     # default value of data and link is none if no data is passed
  71.     def __init__(self, data=None, link=None):
  72.         self.data = data
  73.         self.link = link
  74.  
  75.     # method to update the data feild of Node
  76.     def updateData(self, data):
  77.         self.data = data
  78.  
  79.     # method to set Link feild the Node
  80.     def setLink(self, node):
  81.         self.link = node
  82.  
  83.     # method returns data feild of the Node
  84.     def getData(self):
  85.         return self.data
  86.  
  87.     # method returns address of the next Node
  88.     def getNextNode(self):
  89.         return self.link
  90.  
  91.  
  92.  
  93. # main method
  94. # creating LinkedList
  95. myList = LinkedList()
  96. myList.addToStart(1)
  97. myList.addToEnd(2)
  98. myList.addToEnd(3)
  99. myList.addToEnd(4)
  100. myList.addToEnd(5)
  101. myList.addToEnd(6)
  102. myList.addToEnd(7)
  103. myList.addToEnd(8)
  104. myList.addToEnd(9)
  105. myList.addToEnd(10)
  106. myList.addToEnd(11)
  107.  
  108. print("Program to rotate linked list elements")
  109. myList.display()
  110.  
  111.  
  112. print("\nRotating linked list by 4 places")
  113. myList.rotate(4)
  114. myList.display()
Add Comment
Please, Sign In to add comment