Write a C function that reverses the order of elements on stack using 2 additional stacks.


Difficulty level
Video recording
This exercise is mostly suitable for students
void reverse(stack *s)
{
	stack aux1=CreateStack(), aux2=CreateStack();
	element e;
	while(Top(*s,&e))
	{
		Pop(s);
		Push(&aux1,e);
	}
	
	while(Top(aux1,&e))
	{
		Pop(&aux1);
		Push(&aux2,e);
	}

	while(Top(aux2,&e))
	{
		Pop(&aux2);
		Push(s,e);
	}

}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Zig zag level order