| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 952 人关注过本帖
标题:如何删除两个链表中相同的num?
只看楼主 加入收藏
henji
Rank: 1
等 级:新手上路
帖 子:227
专家分:0
注 册:2009-4-19
结帖率:38.67%
收藏
已结贴  问题点数:10 回复次数:4 
如何删除两个链表中相同的num?
#include "stdafx.h"
#include "stdio.h"
#include "string.h"

struct student
{
    long num;
    float score;
    struct student *next;
};


struct student *del(struct student *head1,struct student *head2)
{
    struct student *p1,*p2,*p3;
    for(p2=head2;p2!=NULL;p2=p2->next)
    {
        for(p1=head1;p1!=NULL;p3=p1,p1=p1->next)
        {
            if(p1->num==p2->num)               
            {
                if(p1==head1)
                    head1=p1->next;     
                else
                    p3->next=p3->next->next;  
            }
        }
    }
    return(head1);
}
struct student *del1(struct student *head1,struct student *head2)
{
    struct student *p1,*p2,*p3,*p4;
    for(p1=head1;p1!=NULL;p1=p1->next)
    {
        for(p2=head2;p2!=NULL;p4=p2,p2=p2->next)
        {
            if(p1->num==p2->num)
            {
                if(p2==head2)
                    head2=p2->next;
                else
                    p4->next=p4->next->next;
            }
        }
      
    }
     return (head2);
}

int main(int argc, char* argv[])
{   
    struct student a,b,c,d,e,f,*head1,*head2,*p,*q,*w;
    a.num=99103;a.score=89;
    b.num=99104;b.score=90;
    c.num=99103;c.score=91;
    head1=&a;
    a.next=&b;
    b.next=&c;
    c.next=NULL;
    p=head1;
    printf("Linklist 1:\n");
    while(p!=NULL)
    {
        printf("%ld%5.1f\n",p->num,p->score);
        p=p->next;
    }
    printf("\n");
    d.num=99103;d.score=92;
    e.num=99104;e.score=93;//这行代码为什么不能同时删除啊?
    f.num=99102;f.score=94;
    head2=&d;
    d.next=&e;
    e.next=&f;
    f.next=NULL;
    q=head2;
    printf("Linklist2:\n");
    while(q!=NULL)
    {
        printf("%ld%5.1f\n",q->num,q->score);
        q=q->next;
    }
    printf("\n");

    head1=del(head1,head2);
    printf("delete Linklist 1:\n");
    for(p=head1;p!=NULL;p=p->next)
    {
       printf("%ld%5.1f\n",p->num,p->score);
      
    }

    a.num=99101;a.score=89;
    b.num=99102;b.score=90;
    c.num=99103;c.score=91;
    head1=&a;
    a.next=&b;
    b.next=&c;
    c.next=NULL;

    head2=del1(head1,head2);
    printf("delete Linklist 2:\n");
    for(q=head2;q!=NULL;q=q->next)
    {
        printf("%ld%5.1f\n",q->num,q->score);
    }
    return 0;
}
搜索更多相关主题的帖子: 删除 num 链表 
2009-09-29 18:17
henji
Rank: 1
等 级:新手上路
帖 子:227
专家分:0
注 册:2009-4-19
收藏
得分:0 
当第二个节点的num=99105时,为什么第一个链表的相同节点可以删除,第二个就只剩下99105不能删除?
#include "stdafx.h"
#include "stdio.h"
#include "string.h"
 
struct student
{
    long num;
    float score;
    struct student *next;
};
struct student *del(struct student *head1,struct student *head2)
{
    struct student *p1,*p2,*p3;
    for(p2=head2;p2!=NULL;p2=p2->next)
    {
        for(p1=head1;p1!=NULL;p3=p1,p1=p1->next)
        {
            if(p1->num==p2->num)                 
            {
                if(p1==head1)  
                    head1=p1->next;      
                else  
                    p3->next=p1->next;   
            }
        }
    }
    return  (head1);
}
struct student *del1(struct student *head1,struct student *head2)
{
    struct student *p1,*p2,*p3,*p4;
    for(p1=head1;p1!=NULL;p1=p1->next)
    {
        for(p2=head2;p2!=NULL;p4=p2,p2=p2->next)
        {
            if(p1->num==p2->num)
            {
                if(p2==head2)
                    head2=p2->next;
                else
                    p4->next=p4->next->next;
            }
        }
        
    }
     return (head2);
}
 
 
 
int main(int argc, char* argv[])
{
    struct student a,b,c,d,e,f,*head1,*head2,*p,*q,*w;
    a.num=99103;a.score=89;
    b.num=99104;b.score=90;
    c.num=99105;c.score=91;
    head1=&a;
    a.next=&b;
    b.next=&c;
    c.next=NULL;
    p=head1;
    printf("Linklist 1:\n");
    while(p!=NULL)
    {
        printf("%ld%5.1f\n",p->num,p->score);
        p=p->next;
    }
    printf("\n");
    d.num=99103;d.score=92;
    e.num=99103;e.score=93;
    f.num=99105;f.score=94;
    head2=&d;
    d.next=&e;
    e.next=&f;
    f.next=NULL;
    q=head2;
    printf("Linklist:\n");
    while(q!=NULL)
    {
        printf("%ld%5.1f\n",q->num,q->score);
        q=q->next;
    }
    printf("\n");
    head1=del(head1,head2);
    printf("delete Linklist 1:\n");
    for(p=head1;p!=NULL;p=p->next)
    {
       printf("%ld%5.1f\n",p->num,p->score);  
        
    }
 
    a.num=99101;a.score=89;
    b.num=99102;b.score=90;
    c.num=99103;c.score=91;
    head1=&a;
    a.next=&b;
    b.next=&c;
    c.next=NULL;
    head2=del1(head1,head2);
    printf("delete Linklist 2:\n");
    for(q=head2;q!=NULL;q=q->next)
    {
        printf("%ld%5.1f\n",q->num,q->score);
    }
    return 0;
}
2009-09-29 18:38
henji
Rank: 1
等 级:新手上路
帖 子:227
专家分:0
注 册:2009-4-19
收藏
得分:0 
麻烦大家帮我改一下程序
2009-09-29 19:54
m456m654
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:3
帖 子:783
专家分:2806
注 册:2009-9-17
收藏
得分:5 
高手还没来。呵呵。等等吧。
2009-09-29 19:54
刘暮哲
Rank: 2
来 自:江苏
等 级:论坛游民
帖 子:75
专家分:83
注 册:2009-9-25
收藏
得分:5 
struct student *del(struct student *head1,struct student *head2)
{
    struct student *p1,*p2,*p3;
    for(p2=head2;p2!=NULL;p2=p2->next)
    {
        for(p1=head1;p1!=NULL;p3=p1,p1=p1->next)
        {
            if(p1->num==p2->num)                 
            {
                if(p1==head1)  
                    head1=p1->next;      
                else  
                    p3->next=p3->next->next;   
            }
        }
    }
    return(head1);
}
你看看你函数的功能   就是两个链表中   head1只要与head2中的数有相等的  就删掉  所以第一次head1中的数都被删了 是空链表

做最好的自己
2009-09-29 20:38
快速回复:如何删除两个链表中相同的num?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.044671 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved