| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 259 人关注过本帖
标题:如何删除第二个链表中与第一个链表中的相同num?
只看楼主 加入收藏
henji
Rank: 1
等 级:新手上路
帖 子:227
专家分:0
注 册:2009-4-19
结帖率:38.67%
收藏
已结贴  问题点数:10 回复次数:1 
如何删除第二个链表中与第一个链表中的相同num?
#include "stdafx.h"
#include "stdio.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 *del_1(struct student *head1,struct student *head2)//代码是这样写的吗?
{
    struct student *p1,*p2,*p3;
    for(p1=head1;p1!=NULL;p3=p1,p1=p1->next)
    {
        if(p2->num==p1->num)
        {
            if(p2==head2)
            {
                head2=p2->next;
            }
            else
            {
                p2->next=p2;
            }
        }
    }
    return (head2);
}

int main(int argc, char* argv[])
{
    struct student a,b,c,d,e,f,*head1,*head2,*p,*q,*w;
    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;
    p=head1;
    printf("第一个链表为:\n");
    while(p!=NULL)
    {
        printf("%ld%5.1f\n",p->num,p->score);
        p=p->next;
    }
    d.num=99103;d.score=92;
    e.num=99104;e.score=93;
    f.num=99105;f.score=94;
    head2=&d;
    d.next=&e;
    e.next=&f;
    f.next=NULL;
    q=head2;
    printf("第二个链表为:\n");
    while(q!=NULL)
    {
        printf("%ld%5.1f\n",q->num,q->score);
        q=q->next;
    }
    head1=del(head1,head2);
    printf("删除后的第一个链表为:\n");
    for(p=head1;p!=NULL;p=p->next)
    {
       printf("%ld%5.1f\n",p->num,p->score);
       
    }
    
    

    return 0;
}
搜索更多相关主题的帖子: 删除 
2009-08-15 10:09
soler
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:181
专家分:1077
注 册:2005-7-16
收藏
得分:10 
我也刚看到链表那一节,^_^,学写了一下。
程序代码:
#include <stdio.h>
#include <malloc.h>

struct student
{
    char name[10];
    long num;
    float score;
    struct student *next;
};
typedef struct student stu;

stu *creatlist(int n);
void show(stu *p);
stu *deletelist(stu *head1,stu *head2);

int main()
{
    stu *head1 = NULL,*head2 = NULL;
    int n,m;

    printf("pls enter the total number of  each list: n/m:\n");
    scanf("%d%d",&n,&m);
    head1 = creatlist(n);
    head2 = creatlist(m);
    show(head1);
    show(head2);

    head2 = deletelist(head1,head2);
    show(head1);
    show(head2);

    return 0;

}

stu *creatlist(int n)
{
    stu *head,*pf,*pb;
    int i;

    head = (stu *)malloc(sizeof(stu));
    head->next = NULL;
    pf = head;
    for(i = 1;i <= n;i++)
    {
        pb = (stu *)malloc(sizeof(stu));
        printf("pls input the data of one student.\n");
        scanf("%s%ld%f",pb->name,&pb->num,&pb->score);
        pf->next = pb;
        pb->next = NULL;
        pf=pb;
    }
    printf("creat list successfully.\n");

    return head;
}

void show(stu *p)
{
    p = p->next;
    while(p != NULL)
    {
        printf("\n%s\t%-7ld%f\n",p->name,p->num,p->score);
        p = p->next;
    }
}

stu *deletelist(stu *head1,stu *head2)
{
    stu *pf,*pb;

    head1 = head1->next;

    while(head1 != NULL)
    {
        pf = head2;
        pb = head2->next;

            while(pb != NULL && (pb->num != head1->num))
            {
                pf = pb;
                pb = pb->next;
            }
            if(pb != NULL)
            {
                pf->next = pb->next;
                printf("delete node(name=%s) successfully~!\n",pb->name);
                free(pb);
            }
            
        head1 = head1->next;
    }


    return head2;

}



[ 本帖最后由 soler 于 2009-8-15 12:59 编辑 ]
2009-08-15 12:50
快速回复:如何删除第二个链表中与第一个链表中的相同num?
数据加载中...
 
   



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

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