您的位置:首页 > 其它

实现双向链表删除一个节点P,在节点P后插入一个节点

2009-02-15 03:23 633 查看
双向链表删除一个节点P

template<class type> void list<type>::delnode(int p)
{
int k=1;
listnode<type> *ptr,*t;
ptr=first;
while(ptr->next!=NULL&&k!=p)
{
ptr=ptr->next;
k++;
}
t=ptr->next;
cout<<"你已经将数据项"<<t->data<<"删除"<<endl;

ptr->next=ptr->next->next;
length--;
delete t;
}

在节点P后插入一个节点:

template<class type> bool list<type>::insert(type t,int p)
{
listnode<type> *ptr;
ptr=first;
int k=1;
while(ptr!=NULL&&k<p)
{
ptr=ptr->next;
k++;
}
if(ptr==NULL&&k!=p)
return false;
else
{
listnode<type> *tp;
tp=new listnode<type>;
tp->data=t;
tp->next=ptr->next;
ptr->next=tp;
length++;
return true;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐