why to bother this much...? just count the elements when popping and output the middle one . while(!s.empty()){ e= s.pop() count++ q.enq(e); }
count <<= 2; while(count){ e = q.deq(); s.push(e); count --; } output s.top() while(!q.empty()){ e = q.deq(); s.push(e); } On Aug 22, 4:27 pm, Shravan Kumar <shrava...@gmail.com> wrote: > Pop each element and en-queue it twice and de-queue it once. When stack is > empty the front of the queue will be middle element. > > > > > > > > On Mon, Aug 22, 2011 at 4:01 PM, Ankur Garg <ankurga...@gmail.com> wrote: > > Find the middle of the stack..(Time complexity should be minimum) > > > Stack is not implemented as Linked List ...u have normal stack with > > push,pop and top > > > How to do this ?? > > > -- > > You received this message because you are subscribed to the Google Groups > > "Algorithm Geeks" group. > > To post to this group, send email to algogeeks@googlegroups.com. > > To unsubscribe from this group, send email to > > algogeeks+unsubscr...@googlegroups.com. > > For more options, visit this group at > >http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.