Advertisement
AyanUpadhaya

Intro to Data Structure Queue

Jul 23rd, 2021
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.88 KB | None | 0 0
  1. # Queue data structure - First in first out (FIFO)
  2.  
  3. """
  4. The queue data structure also means the same where the data elements are arranged in a queue.
  5. The uniqueness of queue lies in the way items are added and removed.
  6. The items are allowed at end but removed form the other end. So it is a First-in-First out method.
  7.  
  8. We can add elements by using insert() and remove elements by using pop method
  9.  
  10. """
  11.  
  12. class Queue:
  13.     def __init__(self):
  14.         self.queue = list()
  15.  
  16.     def add(self,data):
  17.         if data not in self.queue:
  18.             self.queue.insert(0,data)
  19.             return True
  20.         return False
  21.  
  22.     def showelements(self):
  23.         for elm in self.queue:
  24.             print(elm)
  25.  
  26.     def remove(self):
  27.         if len(self.queue)>0:
  28.             return self.queue.pop()
  29.         return "No element found"
  30.  
  31. theQueue = Queue()
  32.  
  33. theQueue.add('mon')
  34. theQueue.add('tue')
  35. theQueue.add('wed')
  36.  
  37. theQueue.remove()
  38. theQueue.showelements()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement