Ejemplo n.º 1
0
class Queue:
   def __init__(self):
      self.list = Linked_list()
      self.size = 0
   
   def poll(self):
      if self.isEmpty():
         return None
      else:
         data = self.peek()
         # linked list should store last element and delete node in O(1)
         self.list.delete_node(data)
         self.size -= 1
         return data

   def peek(self):
      if self.isEmpty():
         return None
      else:
         return self.list.get_head().data

   def add(self,data):
      self.list.add_node(data)
      self.size += 1

   def isEmpty(self):
      if self.size == 0:
         return True
      else:
         return False
   
   def print_queue(self):
      self.list.list_print()
Ejemplo n.º 2
0
class Stack:
   def __init__(self):
      self.list = Linked_list()
      self.size = 0

   def push(self,data):
      self.list.add_node_head(data)
      self.size += 1

   def pop(self):
      if self.isEmpty():
         return None
      else :
         data = self.top()
         self.list.delete_node(data)
         self.size -= 1
         return data

   def top(self):
      if self.isEmpty():
         return None
      else:
         return self.list.get_head().data

   def isEmpty(self):
      if self.size == 0:
         return True
      else:
         return False