GeeksforGeeks » Interview Questions

implement a stack using one queue

(5 posts)
  1. suresh
    Member
    Posted 6 months ago #

    implement a stack using one queue

  2. kartik
    Moderator
    Posted 6 months ago #

    Doesn't seem possible. We need two queues. Let me know your thoughts

  3. techcoder
    Member
    Posted 6 months ago #

    i also think . not possible even by using recursion.

    implementing a queue by using a stack is possible, but implementing a stack using a queue is not possible

  4. NameLess
    guest
    Posted 6 months ago #

    Please Check this Solution(not efficient)

    int getSize(){
         return sizeOfQueue();
        }
    
    void push( int data){
         if(emptyQueue(Q)
        enQueue(Q,data);
      else
         int size=getSize();
         enqueue(data)
        while(size){
          int val=dequeue();
         enQueue(val); size--}
    }
    
    int pop(){
      if(emptyQueue())
        assert(0);
    else
       return dequeue();
    }
    
  5. vijay
    guest
    Posted 5 months ago #

    above code is almost correct(that one way ......) ......................


Reply

You must log in to post.

RSS feed for this topic