无头结点,头指针为 head
void LinkList<DataType>::Insert(int i, DataType x)
{
p = head;
int count = 1;
if(i == 1)
{
s = new Node;
s->data = x;
s->next = head;
head = s;
}
else
{
while(p != NULL && count < i - 1)
{
p = p->next;
count++;
}
if(p == NULL)
cout<<"你输入的位置超出了范围!";
else
{
s = new Node;
s->data = x;
s->next = p->next;
p->next = s;
}
}
}
有头结点 first
void LinkList<DataType>::Insert(int i, DataType x)
{
p = first;
int count = 0;
while(p != NULL && count < i - 1)
{
p = p->next;
count++;
}
if(p == NULL)
cout<<"你输入的位置超出了范围!";
else
{
s = new Node;
s->data = x;
s->next = p->next;
p->next = s;
}
}