possible solution to: reversing a stack using a stack ...


[ Follow Ups ] [ Post Followup ] [ CS1704 Discussion WWWBoard ] [ FAQ ]

Posted by hussein on August 05, 2000 at 17:35:37:

hi

see if you can follow what this does (i didnt quite test it)... comment on whether you think it will work or not ...


Stack S, P; // S is stack, P is temporary
int count=0;

while (!S.Empty ()) // step 1: count elements
P.Push (S.Pop ());
while (!P.Empty ())
{
count++;
S.Push (P.Pop ());
}

// step 2: switch n elements' positions
for ( int i=count; i>1; i++ )
{
itemType item = S.Pop ();
for ( int j=0; j<(i-1); j++ )
P.Push (S.Pop ());
S.Push (item);
while (!P.Empty ())
S.Push (P.Pop ());
}

if this works, why is it not a good solution ?



Follow Ups:



Post a Followup

Name:
E-Mail:

Subject:

Comments:

Optional Link URL:
Link Title:
Optional Image URL:


[ Follow Ups ] [ Post Followup ] [ CS1704 Discussion WWWBoard ] [ FAQ ]