注册 登录
编程论坛 C++教室

为什么这个程序第一个结点删不掉啊

邱超凡 发布于 2013-04-08 16:19, 333 次点击
#include<iostream>
#include<string>
using namespace std;
struct student
{
    int num;
    float score;
    char name;
    struct student *next;
};
void del(student *head,int x,int n)
{
     student *p1,*p2;
     p1=head->next;
     p2=head;
     int i;
     if(head->num==x)
     {
         head=p1;
     }
     else
     {
     for(i=1;i<n;i++)
     {
         if(p1->num==x)
             p2->next=p1->next;
         else
         {
             p1=p1->next;
             p2=p2->next;
         }
     }
     }
}
int main()
{
    student *p1,*p2,*head,a;
    head=p2=p1=&a;
    cin>>a.name>>a.num>>a.score;
    int i,n=1,x;
    for(;p1->num!=0;n++)
    {
        if(n==1)
        {
            head=&a;
        }
        else
        {
            p1=new student;
            cin>>p1->name>>p1->num>>p1->score;
            p2->next=p1;
            p2=p1;
        }
    }
        cin>>x;
    del(head,x,n);
    for(i=0;i<n-1;i++)
    {
        if(head->num==0)
        {
            break;
        }
        cout<<head->name<<"  "<<head->num<<"  "<<head->score;
        cout<<endl;
        head=head->next;
    }
    return 0;
}
3 回复
#2
peach54602013-04-08 17:01
没看懂你在说什么
#3
邱超凡2013-04-18 17:23
回复 2楼 peach5460
就是创建一个动态链表,然后再删除节点,可是第一个节点删不掉。。
#4
邱超凡2013-04-18 17:24
回复 2楼 peach5460
p1=head->next;
     p2=head;
     int i;
     if(head->num==x)
     {
         head=p1;
     }
但是这个head不是已经=head->next了么;输出时还是有第一个
1