c++链表中的函数问题
代码要求删除指定结点,remove函数中形参a存储删除结点值编译检查时没报错,但有时警告: warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
有时警告变成Skipping... (no relevant changes detected) 不太清楚是什么原因,求解释
#include<iostream>
using namespace std;
struct Node
{
int content;
Node *next;
};
bool remove(Node *h,int &a,int pos)
{
if(pos<=0)return false;
else
{
Node *p=h;
if(pos==1)
{
h=h->next;
delete p;
}
else
{
int j=1;
while(j<pos-1)
{
if(p->next==NULL)break;
p=p->next;
j++;
}
}
if(p->next!=NULL)
{
Node *q=p->next;
p->next=q->next;
delete q;
return true;
}
else
return false;
}
}
int main()
{
Node *head=NULL,*tail;
int x;
cout<<"为链表赋值,输入-1结束"<<endl;
cin>>x;
while(x!=-1)
{
Node *p=new Node;
p->content=x;
p->next=NULL;
if(head==NULL)
head=p;
else
tail->next=p;
tail=p;
cin>>x;
}
int y,position;
cout<<"输入要删除的数字及其位置"<<endl;
cin>>y>>position;
for(Node *p=head;p!=NULL;p=p->next)
{
if(p->content==y)break;
}
if(p==NULL)cout<<"输入数字不存在!"<<endl;
else
{
Node *q=head;
bool a=(q,y,position);
if(a==true)
{
cout<<"删除成功,删除后的链表为"<<endl;
for(Node *p=head;p!=NULL;p=p->next)
cout<<p->content<<',';
cout<<endl;
}
else
cout<<"位置不正确或没有第"<<position<<"个结点"<<endl;
}
return 0;
}