m2skills

queueLL python

May 31st, 2017
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.40 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 removes item passed from the Linked List
  47.     def remove(self, item):
  48.         start = self.head
  49.         previous = None
  50.         found = False
  51.  
  52.         # search element in list
  53.         while not found:
  54.             if start.getData() == item:
  55.                 found = True
  56.             else:
  57.                 previous = start
  58.                 start = start.getNextNode()
  59.  
  60.         # if previous is None then the data is found at first position
  61.         if previous is None:
  62.             self.head = start.getNextNode()
  63.         else:
  64.             previous.setLink(start.getNextNode())
  65.         return found
  66.  
  67.     # method to queue element to the Linked List
  68.     def enqueue(self, data):
  69.         if self.isEmpty():
  70.             self.addToStart(data)
  71.         else:
  72.             self.addToEnd(data)
  73.         return True
  74.  
  75.     # method to dequeue element from the Linked List
  76.     def dequeue(self):
  77.         start = self.head
  78.         element = start.getData()
  79.         self.remove(element)
  80.         return element
  81.  
  82.  
  83. # node class
  84. class Node:
  85.     # default value of data and link is none if no data is passed
  86.     def __init__(self, data=None, link=None):
  87.         self.data = data
  88.         self.link = link
  89.  
  90.     # method to update the data feild of Node
  91.     def updateData(self, data):
  92.         self.data = data
  93.  
  94.     # method to set Link feild the Node
  95.     def setLink(self, node):
  96.         self.link = node
  97.  
  98.     # method returns data feild of the Node
  99.     def getData(self):
  100.         return self.data
  101.  
  102.     # method returns address of the next Node
  103.     def getNextNode(self):
  104.         return self.link
  105.  
  106.  
  107. # main method
  108. # creating LinkedList
  109. myList = LinkedList()
  110.  
  111. # adding some elements to the start of LinkedList
  112. myList.enqueue(1)
  113. myList.enqueue(2)
  114. myList.enqueue(3)
  115. myList.enqueue(4)
  116. myList.enqueue(5)
  117.  
  118. print("The contents of the queue are : ")
  119. myList.display()
  120.  
  121. # adding some elements to the End of the LinkedList
  122. print("\nDequeue 3 elements from the Queue \n")
  123. myList.dequeue()
  124. myList.dequeue()
  125. myList.dequeue()
  126.  
  127. print("The contents of the queue are : ")
  128. myList.display()
  129.  
  130. '''
  131.  
  132. The contents of the queue are :
  133. 1 --> 2 --> 3 --> 4 --> 5
  134.  
  135. Dequeue 3 elements from the Queue
  136.  
  137. The contents of the queue are :
  138. 4 --> 5
  139.  
  140. '''
Add Comment
Please, Sign In to add comment