Hi, How about the following approach. lets take stack is stack1. lets take 2 pointers , p1 and p2 for every pop , keep increment p1 , so that p1 will point to latest pop'ed out element. increment p2 for every odd count of pop's...
increment p2 , for 1 , 3 , 5 , pop'ings etc.. so at the end , p2 will point to middle element of the stack. Thx, --Gopi On Mon, Aug 22, 2011 at 8:04 PM, muthu raj <[email protected]> wrote: > Sorry i dint read the question properly :) > > *Muthuraj R > IV th Year , ISE > PESIT , Bangalore* > > > > On Mon, Aug 22, 2011 at 7:24 AM, sukran dhawan <[email protected]>wrote: > >> count is required since its not implemented as LL >> >> >> On Mon, Aug 22, 2011 at 7:47 PM, shady <[email protected]> wrote: >> >>> i wish u had read the question... it is simple.. push to new stack and >>> then pop back... number of elements count need to be there >>> >>> >>> On Mon, Aug 22, 2011 at 7:44 PM, muthu raj <[email protected]> wrote: >>> >>>> No need to count the number of nodes. Since its implemented as a linked >>>> list traverse the list with two two pointers one incremented one node next >>>> and other incremented two nodes next simultaneously. >>>> >>>> void delete_MiddleStack(node **h) >>>> { >>>> >>>> if(*h==NULL) >>>> return; >>>> node *p,*q; >>>> p=*h; >>>> q=*h; >>>> while(q->next!=NULL) >>>> { >>>> p=p->next; >>>> if(q->next==NULL) >>>> q=q->next; >>>> else q=q->next->next; >>>> } >>>> p->ele=p->next->ele; >>>> q=p->next; >>>> p->next=p->next->next; >>>> free(q); >>>> } >>>> >>>> >>>> >>>> >>>> >>>> >>>> >>>> *Muthuraj R >>>> IV th Year , ISE >>>> PESIT , Bangalore* >>>> >>>> >>>> >>>> >>>> On Mon, Aug 22, 2011 at 5:08 AM, vikas <[email protected]>wrote: >>>> >>>>> 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 <[email protected]> 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 <[email protected]> >>>>> 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 [email protected]. >>>>> > > To unsubscribe from this group, send email to >>>>> > > [email protected]. >>>>> > > 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 [email protected]. >>>>> To unsubscribe from this group, send email to >>>>> [email protected]. >>>>> 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 [email protected]. >>>> To unsubscribe from this group, send email to >>>> [email protected]. >>>> 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 [email protected]. >>> To unsubscribe from this group, send email to >>> [email protected]. >>> 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 [email protected]. >> To unsubscribe from this group, send email to >> [email protected]. >> 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 [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- Thx, --Gopi -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
