You need to define a flag which will be set after the first find.
void delete (int s[] , int &top , int v)
{
bool found = false;
int ts[size] ;
int ttop=-1;
while(top!=-1)
{
if (s[top]!=v)
{
push(ts,ttop,s[top]);
}
else
{
if (found == false)
{
found = true;
push(ts,ttop,s[top]);
}
}
pop(top);
}
while(ttop!=-1)
{
push(s,top,ts[ttop]);
pop(ttop);
}
}
As long as found
is false, it will also check to see if the element matches. Once found
is true, it will always include the item, even if it matches.
Note that this is not the most effective way to do this, merely a means of modifying your code so that it does the trick.
2
solved How to delete first occurrence of value in array? [closed]