1. Write a recursive function that inserts an element \(\texttt{e}\) at the bottom of a stack by using only stack operations.
    \(\textit{Prototype}: \texttt{void InsertAtBottom(Stack *s, element e)}\)
  2. Write a recursive function that reverses the elements of a stack by using only stack operations.
    \(\textit{Prototype}: \texttt{void ReverseStack(Stack *s)}\)
  3. Without performing calculation, give the worst-case time complexity of the above two functions. Justify your answer.


Difficulty level
Video recording
This exercise is mostly suitable for students
void insertAtBottom(stack *s, element e)
{
	element temp;
	if(isEmptyStack(*s))
	{
		Push(s,e);
		return;
	}
	Top(*s,&temp);
	Pop(s);
	insertAtBottom(s,e);
	Push(s,temp);
}

void Reverse(stack *s)
{
	element e;
	if(!isEmptyStack(*s))
	{
		Top(*s,&e);
		Pop(s);
		Reverse(s);
	  	insertAtBottom(s,e);
	}
}

Complexity : 
O(N), O(N^2)

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Graph Edge Property