daily pastebin goal
18%
SHARE
TWEET

Untitled

a guest Feb 13th, 2018 154 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class LinkedQueue :
  2.     class _Node :
  3.  
  4.         def __init__(self, element, next):
  5.             self._element = element
  6.             self._next = next
  7.  
  8.         def get_elements(self):
  9.             return self._element
  10.  
  11.         def set_elements(self, num):
  12.             self._element = num        
  13.  
  14.     def __init__(self) :
  15.         self._head = None
  16.         self._tail = None
  17.         self._size = 0
  18.  
  19.     def display(self):
  20.         tmp = self._head
  21.         while tmp != None :
  22.             print(tmp.get_elements())
  23.             tmp = tmp._next        
  24.  
  25.     def __len__(self) :
  26.         return self._size
  27.  
  28.     def is_empty(self) :
  29.         return self._size == 0
  30.  
  31.     def first(self) :
  32.         if self.is_empty() :
  33.             raise Empty('Queue is empty')
  34.         return self._head._element
  35.  
  36.     def dequeue(self) :
  37.         if self.is_empty():
  38.             raise Empty('Queue is empty')
  39.         answer = self._head._element
  40.         self._head = self._head._next
  41.         self._size -= 1
  42.         if self.is_empty() :
  43.             self._tail = None
  44.         return answer
  45.  
  46.     def enqueue(self, e) :
  47.         newest = self._Node(e,None)
  48.         if self.is_empty() :
  49.             self._head = newest
  50.         else :
  51.             self._tail._next = newest
  52.         self._tail = newest
  53.         self._size += 1
  54.  
  55. class Empty(Exception) :
  56.     pass
  57.    
  58. def menu():
  59.     queue = LinkedQueue()
  60.     while True:
  61.         print('n - to add a number')
  62.         print('q - to quit')
  63.         selection = input('Enter your selection: ')
  64.         if selection == 'n':
  65.             number = int(input("Enter a number: "))
  66.             queue.enqueue(number)
  67.         elif selection == 'q':
  68.             break
  69.     for item in queue.display():
  70.         n = item
  71.  
  72.     counter = 0
  73.     for i in range(len(queue)):
  74.         counter += 1
  75.         print('Entry # ', counter, '=', n)
RAW Paste Data
Top