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.