isEmpty
by Isai Damier, Android Engineer @ Google

#=====================================================================
# Author: Isai Damier
# Title: QueueWithStack
# Project: geekviewpoint
# Package: datastructure
#
# Description: To implement a Queue using Stack datastructure,
#    two stacks are sufficient: a stack for enqueuing and a stack
#    for dequeuing. Recall that stacks are LIFO objects (i.e. Last
#    In First Out), whereas queues are normally FIFO objects (i.e.
#    First In First Out). Hence although adding elements to a stack
#    is similar to adding elements to a queue, the removal process
#    for each datastructure is different.
#
#    Notice how in the following implementation queue.enqueue() is
#    the same as stack.put(). However, two stacks are used for
#    proper dequeuing.
#=====================================================================
 
 from Queue import LifoQueue
class QueueWithStack( object ):

  def __init__( self ):
    self.input_stack = LifoQueue()
    self.output_stack = LifoQueue()

  #=====================================================================
  # Statement:
  #   Indicate whether the queue is empty.
  #
  # Time Complexity of Solution:
  #   Best = Average = Worst = const.
  #
  # Technical Details: The queue is empty precisely when both the
  #     input_stack stack and the output_stack stack are empty.
  #
  #     Note that the input_stack stack may occasionally be empty
  #     while the output_stack stack is not empty vice versa.
  #
  #=====================================================================
  def isEmpty( self ):
    return self.input_stack.empty() and self.output_stack.empty()
import unittest
from algorithms.QueueWithStack import QueueWithStack

class Test( unittest.TestCase ):

  #=====================================================================
  # Test of isEmpty method, of class QueueWithStack.
  #=====================================================================

  def testIsEmpty( self ):
    queue = QueueWithStack()
    data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
    self.assertTrue( queue.isEmpty() )
    self.assertEquals( 0, queue.size() )
    for  d in data:
      queue.enqueue( d )

    self.assertFalse( queue.isEmpty() )
    self.assertEquals( len( data ), queue.size() )