Clear
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()

  #=====================================================================
  # Description: Clear the queue.
  #
  # Technical Details: The queue is cleared precisely when both the
  #     input_stack stack and the output_stack stack are cleared.
  #
  #=====================================================================
  def clear( self ):
    self.input_stack = LifoQueue()
    self.output_stack = LifoQueue()

import unittest
from algorithms.QueueWithStack import QueueWithStack

class Test( unittest.TestCase ):

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

  def testClear( 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() )
    queue.clear()

    self.assertTrue( queue.isEmpty() )
    self.assertEquals( 0, queue.size() )