Write a C function that interchanges the top two numbers on the stack.


Difficulty level
Video recording
This exercise is mostly suitable for students
int interchange(stack *s)
{
    element e1, e2;
     if(Top(*s, &e1) && Pop(s))
     {
         if(Top(*s, &e2) && Pop(s))
         {
            Push(s,e1);
            Push(s,e2);
         }
         else
            Push(s,e1);
     }
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Binary heaps