Write a C function that deletes the kth element of a stack of integers (the top element is considered at position 1).
Difficulty level
Video recording
This exercise is mostly suitable for students
int delete_pos(stack *s, int pos)
{
int count;
stack aux;
element e;
count=1;
aux=CreateStack();
while(Top(*s,&e) && count < pos)
{
count++;
Pop(s);
Push(&aux,e);
}
if(count==pos && Pop(s))
{
while(Top(aux,&e))
{
Pop(&aux);
Push(s,e);
}
return 1;
}
else
{
while(Top(aux,&e))
{
Pop(&aux);
Push(s,e);
}
return 0;
}
return 1;
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Fresnel integral