void LList::insertAt(int position,string value) //inserts a new value at the specified position.
{
int count=0;
Node* newN=new Node;
newN->setInfo(value);
if(first==0)
{
first=newN;
first->setLink(0);
}
else
{
if(position<=0)
{
newN->setLink(first);
first=newN;
}
if (position>=getSize())
{
Node*temp=first;
while(temp->getLink()!=0)
temp=temp->getLink();
temp->setLink(newN);
newN->setLink(0);
}
else
{
Node*temp=first;
Node*trail=temp;
while(temp->getLink()!=0&&count<position)
{
trail=temp;
temp=temp->getLink();
++count;
}
trail->setLink(newN);
newN->setLink(temp);
}
}
}
void LList::deleteAt(int pos) //deletes the value at the specified position.
{
if (first!=0)
{
if(first->getLink()!=0)
{
if(pos<=0)
{
Node*temp=first;
first=first->getLink();
delete temp;
}
else
{
int count=0;
Node* temp=first;
Node*trail=temp;
while(count<pos&&temp!=0)
{
trail=temp;
++count;
temp=temp->getLink();
}
if(temp!=0)
trail->setLink(temp->getLink());
else
trail->setLink(0);
delete temp;
}
}
elsedelete first;
}
}
int LList::getSize() //returns size of the list.
{
int count=0;
if(first!=0)
{
Node*temp=first;
while(temp!=0)
{
temp=temp->getLink();
++count;
}
}
return count;
}
it runs but stops working when i use deleteAt and getSize if there was only one node in the list
and i just cant figure out why!!!!
so can anyone please explain to me why that's happening????
i would really appreciate any help i can get ^_^