using stack and queue