c语言实现两个顺序表的合并

将共同拥有的元素只存其一... 将共同拥有的元素只存其一 展开
 我来答
townsin
推荐于2017-09-01 · TA获得超过452个赞
知道小有建树答主
回答量:594
采纳率:50%
帮助的人:550万
展开全部

一个算法给你(假如是升序,并且不重复)

while(表1不结束 && 表2不结束) {

    if (表1结束 || 表1.当前值>表2.当前值) {表2.当前值插入新表;表2.当前值向后移动}

    else if (表2结束 || 表1.当前值<表2.当前值) {表1.当前值插入新表;表1.当前值向后移动}

    else if (表1.当前值=表2.当前值) {表1.当前值插入新表;表1.当前值和表2.当前值向后移动}

}

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct student {
    int num;
    struct student  *next;
};
void print(struct student *head) {
    struct student *p;
    p=head;
    char s=' ';
    if(head==NULL) {
        printf("该链表为空");
    }
    if(head!=NULL) {
        do {
            printf("%c%c%d",s,s,p->num);
            p=p->next;
        } while(p!=NULL);
        printf("\n");
    }
}
struct student *creatb() {
    struct student *head;
    struct student *p1,*p2;
    int n=0;
    p1=p2=(struct student*)malloc(sizeof(struct student));
    scanf("%d",&p1->num);
    head=NULL;
    while(p1->num!=0) {
        n=n+1;
        if(n==1)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
        p1=(struct student*)malloc(sizeof(struct student));
        scanf("%d",&p1->num);
    }
    p2->next=NULL;
    return head;
}
int main() {
    struct student *head1,*head2,*head3;
    head1=NULL;
    head2=NULL;
    head3=NULL;
    printf("请输入单链表La,输入0表示输入结束:\n");
    head1=creatb();
    printf("输入的链表为:");
    print(head1);
    printf("请输入单链表Lb,输入0表示输入结束:\n");
    head2=creatb();
    printf("输入的链表为:");
    print(head2);
    struct student *a,*b,*c, *tmpNode;
    a=head1->next;
    b=head2;//
    head3=c=head1;
    while(a != NULL || b != NULL) {
        if(b == NULL || (a != NULL && a->num < b->num)) {
            c->next=a;
            c=a;
            a=a->next;
        } else if(a == NULL || (b != NULL && a->num > b->num)) {
            c->next=b;
            c=b;
            b=b->next;
        } else if (a != NULL && b != NULL) {
            c->next=a;
            c=a;
            a=a->next;
            tmpNode = b;
            b = b->next;
            free(tmpNode);
        }
    }
    c->next=NULL;
    printf("合并后的有序链表为:");
    print(head3);
    c = head3;
    while(c) {
        tmpNode = c;
        c = c->next;
        free(tmpNode);
    }
    return 0;
}
更多追问追答
追问
不能假设他不重复啊,要考虑他有重复的情况的啊
追答
我假设的是各自里面没有重复
百度网友f7fa04b
2013-09-24
知道答主
回答量:22
采纳率:0%
帮助的人:11.8万
展开全部
两层for循环,第一层表示第一个集合,第二层表示第二个集合,用一个变量flag标记有没有出现过,如果出现了不管,如果没有出现就增加到第一个集合中。最后输出第一个集合的数就好了..
追问
要的是代码诶。思路我也是这样的
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
奇闻轶史
2013-09-24 · TA获得超过134个赞
知道小有建树答主
回答量:400
采纳率:25%
帮助的人:119万
展开全部
思路,先合并在排序,合并时候注意一个不变,你一个插入,检查有,就pass,没有再插入,在排序,排序方法很多自己选。
更多追问追答
追问
这个思路我也知道啊,要求代码
追答
。。。。最好有个具体的描述,顺序表存的是数字吗?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
将离川
2017-10-26
知道答主
回答量:19
采纳率:0%
帮助的人:3.6万
引用townsin的回答:
一个算法给你(假如是升序,并且不重复)
while(表1不结束 && 表2不结束) {
if (表1结束 || 表1.当前值>表2.当前值) {表2.当前值插入新表;表2.当前值向后移动}
else if (表2结束 || 表1.当前值<表2.当前值) {表1.当前值插入新表;表1.当前值向后移动}
else if (表1.当前值=表2.当前值) {表1.当前值插入新表;表1.当前值和表2.当前值向后移动}
}
#include<stdio.h>#include<malloc.h>#include<stdlib.h>struct student { int num; struct student *next;};void print(struct student *head) { struct student *p; p=head; char s=' '; if(head==NULL) { printf("该链表为空"); } if(head!=NULL) { do { printf("%c%c%d",s,s,p->num); p=p->next; } while(p!=NULL); printf("\n"); }}struct student *creatb() { struct student *head; struct student *p1,*p2; int n=0; p1=p2=(struct student*)malloc(sizeof(struct student)); scanf("%d",&p1->num); head=NULL; while(p1->num!=0) { n=n+1; if(n==1) head=p1; else p2->next=p1; p2=p1; p1=(struct student*)malloc(sizeof(struct student)); scanf("%d",&p1->num); } p2->next=NULL; return head;}int main() { struct student *head1,*head2,*head3; head1=NULL; head2=NULL; head3=NULL; printf("请输入单链表La,输入0表示输入结束:\n"); head1=creatb(); printf("输入的链表为:"); print(head1); printf("请输入单链表Lb,输入0表示输入结束:\n"); head2=creatb(); printf("输入的链表为:"); print(head2); struct student *a,*b,*c, *tmpNode; a=head1->next; b=head2;// head3=c=head1; while(a != NULL || b != NULL) { if(b == NULL || (a != NULL && a->num < b->num)) { c->next=a; c=a; a=a->next; } else if(a == NULL || (b != NULL && a->num > b->num)) { c->next=b; c=b; b=b->next; } else if (a != NULL && b != NULL) { c->next=a; c=a; a=a->next; tmpNode = b; b = b->next; free(tmpNode); } } c->next=NULL; printf("合并后的有序链表为:"); print(head3); c = head3; while(c) { tmpNode = c; c = c->next; free(tmpNode); } return 0;}
展开全部
这是链表傻X
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
dengshengli123
2013-09-24
知道答主
回答量:30
采纳率:0%
帮助的人:19.2万
展开全部
你可以好好去看一下《数据结构》中链表这一章,介绍的比较详细,合并、翻转、增加、删除、更新等都有
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 2条折叠回答
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式