m2skills

stackll py

Apr 9th, 2017
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.57 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()))
  41.             start = start.link
  42.         print()
  43.  
  44.     # method pushes element to the Linked List
  45.     def push(self, data):
  46.         self.addToStart(data)
  47.         return True
  48.  
  49.     # method removes and returns the last element from the Linked List
  50.     def pop(self):
  51.         if self.isEmpty():
  52.             return False
  53.         start = self.head
  54.         data = start.getData()
  55.         start = start.getNextNode()
  56.         self.head = start
  57.  
  58.         return data
  59.  
  60. # node class
  61. class Node:
  62.     # default value of data and link is none if no data is passed
  63.     def __init__(self, data=None, link=None):
  64.         self.data = data
  65.         self.link = link
  66.  
  67.     # method to update the data feild of Node
  68.     def updateData(self, data):
  69.         self.data = data
  70.  
  71.     # method to set Link feild the Node
  72.     def setLink(self, node):
  73.         self.link = node
  74.  
  75.     # method returns data feild of the Node
  76.     def getData(self):
  77.         return self.data
  78.  
  79.     # method returns address of the next Node
  80.     def getNextNode(self):
  81.         return self.link
  82.  
  83.  
  84. # main method
  85. # creating LinkedList
  86. myList = LinkedList()
  87.  
  88. # adding some elements to the start of LinkedList
  89. myList.push(1)
  90. myList.push(2)
  91. myList.push(3)
  92. myList.push(4)
  93. myList.push(5)
  94. myList.push(6)
  95.  
  96. print("The Contents of the stack are : ")
  97. myList.display()
  98.  
  99. # adding some elements to the End of the LinkedList
  100. print("Removing 3 elements from the stack\n")
  101. myList.pop()
  102. myList.pop()
  103. myList.pop()
  104.  
  105. print("The Contents of the stack are : ")
  106. myList.display()
Add Comment
Please, Sign In to add comment