首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

小弟我写的链表,错哪了啊删除链表的时候提示了异常

2013-01-05 
我写的链表,哪里错了啊,删除链表的时候提示了错误#include iostreamusing namespace stdclass lian{pub

我写的链表,哪里错了啊,删除链表的时候提示了错误
#include <iostream>
using namespace std;
class lian
{
public:
int shu;
lian *next;
};
lian *head;
lian *chuangjian()
{
lian *ls;
lian *le;
ls=new lian;
head=NULL;
le=ls;
cin>>ls->shu;
for(int n=0;n<=10;n++)
{
if(head=NULL)
{
head=ls;
}
else{le->next=ls;}
le=ls;
ls=new lian;
cin>>ls->shu;
}
le->next=NULL;
delete ls;
return head;
}
lian *del(lian *&head,int shu)
{
lian *p1;
lian *p2;
lian *bao;
p1=head;
p2=p1->next;
while(p2->shu!=shu)
{
if(head=NULL)
{cout<<"链表为空,不能删除!";return 0;}
if(head->shu=shu)
{
cout<<"删除第一个节点"<<endl;
bao=head;
head=head->next;
free(bao);
return 0;
}
bao=head;
for(p1,p2;p2->next;p1=p2,p1->next=p2)
{
if(p1->shu=shu)
{
p1->next=p2->next;
free(p2);
return 0;
}
}
}
}
lian *show(lian *head)
{
cout<<"输出:";
while(head->next)
{cout<<head->shu;head=head->next;}
return 0;
}
int main()
{
cout<<"请输入10个数"<<endl;
lian *head=chuangjian();
cout<<"你想要删除哪一个?"<<endl;
int n;cin>>n;
del(head,n);
show(head);
}
[解决办法]
if里面怎么会有这种错误if(p1->shu=shu), if(head=NULL),不明白=和==的不同。代码里面竟然用拼音,哥们,这些习惯得改改
[解决办法]

#include <iostream>
using namespace std;
class lian
{
public:
    int shu;
    lian *next;
};
int show(lian *head);
lian *chuangjian()
{
lian *head;
    lian *ls;
    lian *le;
    ls=new lian;
le=new lian;
    head=NULL;
    le=ls;  
    for(int n=0; n<10; n++)
    {
cin>>ls->shu;
        if(head==NULL)
        {
            head=ls;
head->next=le;
        }
        else
        {
            le->next=ls;
le=ls;
        }
        ls=new lian;
    }
    le->next=NULL;
    delete ls;
    return head;
}


lian *del(lian *head,int shu)
{
    lian *p1;
    lian *p2;
    p1=head;
if(head==NULL)
    {
            cout<<"链表为空,不能删除!";
            return 0;


        }
        if(head->shu==shu)
        {
            cout<<"删除第一个节点"<<endl;
            head=head->next;
            return head;
        }
    while(p1->next!=NULL&&p1->shu!=shu)
    {
p2=p1;
p1=p1->next;
}
if(p1->shu==shu)
            {
p2->next=p1->next; 
return head;
            }
return head;
}

int show(lian *head)
{
    cout<<"输出:";
    while(head)
    {
        cout<<head->shu;
        head=head->next;
    }
    return 0;
}
void main()
{
    cout<<"请输入10个数"<<endl;
    lian *head=chuangjian();
show(head);
    cout<<"你想要删除哪一个?"<<endl;
    int n;cin>>n;
    head=del(head,n);
show(head);

}

热点排行