Size
by Isai Damier

#=====================================================================
# 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()

  #=====================================================================
  # Description: Indicate the number of elements in the queue.
  #
  # Technical Details: The number of elements in the queue is the
  #    total number of elements in the input_stack stack plus the total
  #    number of elements in the output_stack stack.
  #
  #=====================================================================
  def size( self ):
    return self.input_stack.qsize() + self.output_stack.qsize()
import unittest
from algorithms.QueueWithStack import QueueWithStack

class Test( unittest.TestCase ):

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

  def testSize( 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() )