Pomah3

Untitled

Nov 26th, 2020
533
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class MyNode:
  2.     def __init__(self, contained_object):
  3.         self.contained_object = contained_object
  4.         self.next = None
  5.  
  6. class MyQueue:
  7.     def __init__(self):
  8.         self.head = None
  9.    
  10.     def add(self, elem):
  11.         if self.head is None:
  12.             self.head = MyNode(elem)
  13.             return
  14.        
  15.         last_elem = self.head
  16.         while last_elem.next is not None:
  17.             last_elem = last_elem.next
  18.        
  19.         last_elem.next = MyNode(elem)
  20.        
  21.     def remove(self):
  22.         if self.head is None:
  23.             raise Exception("queue is empty")
  24.        
  25.         obj = self.head.contained_object
  26.         self.head = self.head.next
  27.         return obj
  28.        
  29.     def clear(self):
  30.         self.head = None
  31.        
  32.     def to_list(self):
  33.         arr = []
  34.         elem = self.head
  35.        
  36.         while elem is not None:
  37.             arr.append(elem.contained_object)
  38.             elem = elem.next
  39.        
  40.         return arr
  41.        
  42.  
  43. class Country:
  44.     def __init__(self, name: str, capital: str):
  45.         self.name = name
  46.         self.capital = capital
  47.        
  48.     def __str__(self):
  49.         return f"'{self.name}' country, capital: {self.capital}"
  50.    
  51.     def __repr__(self):
  52.         return f"<{self.__str__()}>"
  53.        
  54. numbers_queue = MyQueue()
  55.  
  56. numbers_queue.add(1)
  57. numbers_queue.add(2)
  58. numbers_queue.add(3)
  59.  
  60. print(numbers_queue.to_list())
  61.  
  62. print(numbers_queue.remove())
  63. print(numbers_queue.remove())
  64. print(numbers_queue.remove())
  65.  
  66. countries_queue = MyQueue()
  67.  
  68. countries_queue.add(Country("Russia", "Moscow"))
  69. countries_queue.add(Country("USA", "Washington"))
  70. countries_queue.add(Country("UK", "London"))
  71.  
  72. print(countries_queue.to_list())
  73.  
  74. print(countries_queue.remove())
  75. print(countries_queue.remove())
  76. print(countries_queue.remove())
  77.        
RAW Paste Data