m2skills

LL python

Apr 7th, 2017
2,130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.24 KB | None | 0 0
  1. # program to implement 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.     # method returns length of linked list
  47.     def length(self):
  48.         start = self.head
  49.         size = 0
  50.         while start:
  51.             size += 1
  52.             start = start.getNextNode()
  53.         # print(size)
  54.         return size
  55.  
  56.     # method returns index of the recieved data
  57.     def index(self, data):
  58.         start = self.head
  59.         position = 1
  60.  
  61.         while start:
  62.             if start.getData() == data:
  63.                 return position
  64.             else:
  65.                 position += 1
  66.                 start = start.getNextNode()
  67.  
  68.     # method removes item passed from the Linked List
  69.     def remove(self, item):
  70.         start = self.head
  71.         previous = None
  72.         found = False
  73.  
  74.         # search element in list
  75.         while not found:
  76.             if start.getData() == item:
  77.                 found = True
  78.             else:
  79.                 previous = start
  80.                 start = start.getNextNode()
  81.  
  82.         # if previous is None then the data is found at first position
  83.         if previous is None:
  84.             self.head = start.getNextNode()
  85.         else:
  86.             previous.setLink(start.getNextNode())
  87.         return found
  88.  
  89.     # method returns max element from the List
  90.     def Max(self):
  91.         start = self.head
  92.         largest = start.getData()
  93.         while start:
  94.             if largest < start.getData():
  95.                 largest = start.getData()
  96.             start = start.getNextNode()
  97.         return largest
  98.  
  99.     # method returns minimum element of Linked list
  100.     def Min(self):
  101.         start = self.head
  102.         smallest = start.getData()
  103.         while start:
  104.             if smallest > start.getData():
  105.                 smallest = start.getData()
  106.             start = start.getNextNode()
  107.         return smallest
  108.  
  109.     # method pushes element to the Linked List
  110.     def push(self, data):
  111.         self.addToStart(data)
  112.         return True
  113.  
  114.     # method removes and returns the last element from the Linked List
  115.     def pop(self):
  116.         if self.isEmpty():
  117.             return False
  118.         start = self.head
  119.         data = start.getData()
  120.         start = start.getNextNode()
  121.         self.head = start
  122.  
  123.         return data
  124.  
  125.     # method returns the element at given position
  126.     def atIndex(self, position):
  127.         start = self.head
  128.         position = int(position)
  129.         pos = 1
  130.         while pos != position:
  131.             start = start.getNextNode()
  132.             pos += 1
  133.  
  134.         data = start.getData()
  135.         return data
  136.  
  137.     # method returns a copy of the current Linked List
  138.     def copy(self):
  139.         temp = LinkedList()
  140.         start = self.head
  141.  
  142.         temp.addToStart(start.getData())
  143.         start = start.getNextNode()
  144.  
  145.         while start:
  146.             temp.addToEnd(start.getData())
  147.             start = start.getNextNode()
  148.  
  149.         return temp
  150.  
  151.     # method to clear LinkedList
  152.     def clear(self):
  153.         self.head = None
  154.         return True
  155.  
  156.     # method returns and removes element at recieved position
  157.     def removePosition(self, position):
  158.         data = self.atIndex(position)
  159.         self.remove(data)
  160.         return data
  161.  
  162.     # method returns string of elements of Linked list
  163.     # the Elements are seperated by seperator if passed else all elements are appended
  164.     def toString(self, seperator=""):
  165.         start = self.head
  166.         finalString = ""
  167.         while start:
  168.             tempString = start.getData()
  169.             finalString += str(tempString)
  170.             start = start.getNextNode()
  171.  
  172.             # if next node exists only the append seperator
  173.             if start:
  174.                 finalString += seperator
  175.  
  176.         return finalString
  177.  
  178.     # method returns count of Element recieved
  179.     def count(self, element):
  180.         start = self.head
  181.         count1 = 0
  182.         while start:
  183.             if start.getData() == element:
  184.                 count1 += 1
  185.             start = start.getNextNode()
  186.         return count1
  187.  
  188.     # method returns builtin List of python consisting of Elements of LinkedList
  189.     def toList(self):
  190.         start = self.head
  191.         tempList = []
  192.         while start:
  193.             tempElement = start.getData()
  194.             tempList.append(tempElement)
  195.             start = start.getNextNode()
  196.         return tempList
  197.  
  198.     # method returns builtin Set of python consisting of Elements of LinkedList
  199.     def toSet(self):
  200.         start = self.head
  201.         tempSet = set()
  202.         while start:
  203.             tempElement = start.getData()
  204.             if tempElement not in tempSet:
  205.                 tempSet.add(tempElement)
  206.             start = start.getNextNode()
  207.         return tempSet
  208.  
  209.     # method reverses the LinkedList
  210.     def reverse(self):
  211.         start = self.head
  212.         tempNode = None
  213.         previousNode = None
  214.  
  215.         while start:
  216.             tempNode = start.getNextNode()
  217.             start.setLink(previousNode)
  218.             previousNode = start
  219.             start = tempNode
  220.  
  221.         self.head = previousNode
  222.         return True
  223.  
  224.     # method that sorts LinkedList
  225.     def sort(self):
  226.         start = self.head
  227.         beginNode = start
  228.         while beginNode:
  229.             tempNode = beginNode
  230.             tempNode2 = beginNode
  231.             smallest = beginNode.getData()
  232.             while tempNode:
  233.                 if smallest > tempNode.getData():
  234.                     smallest = tempNode.getData()
  235.                     tempNode2 = tempNode
  236.                 tempNode = tempNode.getNextNode()
  237.  
  238.             # swap data of beginNode and tempNode2
  239.             temp = beginNode.getData()
  240.             beginNode.updateData(tempNode2.getData())
  241.             tempNode2.updateData(temp)
  242.  
  243.             beginNode = beginNode.getNextNode()
  244.  
  245.     # method returns new instance of the sorted LinkedList without changing original LinkedList
  246.     def sorted(self):
  247.         start = self.head
  248.         tempList = self.copy()
  249.         tempList.sort()
  250.         return tempList
  251.  
  252. # node class
  253. class Node:
  254.     # default value of data and link is none if no data is passed
  255.     def __init__(self, data=None, link=None):
  256.         self.data = data
  257.         self.link = link
  258.  
  259.     # method to update the data feild of Node
  260.     def updateData(self, data):
  261.         self.data = data
  262.  
  263.     # method to set Link feild the Node
  264.     def setLink(self, node):
  265.         self.link = node
  266.  
  267.     # method returns data feild of the Node
  268.     def getData(self):
  269.         return self.data
  270.  
  271.     # method returns address of the next Node
  272.     def getNextNode(self):
  273.         return self.link
  274.  
  275.  
  276. # main method
  277. # creating LinkedList
  278. myList = LinkedList()
  279.  
  280. # adding some elements to the start of LinkedList
  281. myList.addToStart(5)
  282. myList.addToStart(4)
  283. myList.addToStart(3)
  284. myList.addToStart(2)
  285. myList.addToStart(1)
  286.  
  287.  
  288. myList.display()
  289.  
  290. # adding some elements to the End of the LinkedList
  291. myList.addToEnd(12)
  292. myList.addToEnd(13)
  293. myList.addToEnd(3)
  294. myList.display()
  295.  
  296. # printing Length
  297. print(myList.length())
  298.  
  299. # printing index of an element
  300. print(myList.index(3))
  301.  
  302. # printing element at a particular index
  303. print(myList.atIndex(5))
  304.  
  305. # removing an element
  306. print(myList.remove(12))
  307.  
  308. # removing element from a particular position
  309. myList.removePosition(2)
  310.  
  311. myList.display()
  312.  
  313. # printing max and min element
  314. print(myList.Max())
  315. print(myList.Min())
  316.  
  317. # pushing and poping elements
  318. print(myList.push(31))
  319. myList.display()
  320. print(myList.pop())
  321. myList.display()
  322.  
  323. # creating a copy of the Linked List
  324. myList2 = myList.copy()
  325. myList2.display()
  326.  
  327. # removing all elements from the LinkedList
  328. myList2.clear()
  329. myList2.display()
  330.  
  331.  
  332. # printing a string of elements of the LinkedList
  333. print(myList.toString(","))
  334.  
  335. # printing count of particular element in the List
  336. print(myList.count(3))
  337.  
  338. # making a builtIn List from the LinkedList
  339. newList = myList.toList()
  340. print(newList)
  341.  
  342. # making a List from the LinkedList
  343. newSet = myList.toSet()
  344. print(newSet)
  345.  
  346. # reversing the LinkedLkst
  347. myList.reverse()
  348. myList.display()
  349.  
  350. # making a sorted LinkedList out of the Original
  351. myList3 = myList.sorted()
  352. myList3.display()
  353.  
  354. # sorting the LinkedList
  355. myList.sort()
  356. myList.display()
Add Comment
Please, Sign In to add comment