Write a C function that returns the Nth element of a stack. The top element is element number 1.


Difficulty level
Video recording
This exercise is mostly suitable for students
int nth(stack s, int n)
{
	element e;
	int count=1;
	while(Top(s,&e) && count<n)
	{
		count++;
		Pop(&s);
	}
	if(isEmptyStack(s) || n<0)
		return -1;
	return e;
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
GCD and LCM