王道2.15

作者: 啦啦啦_9a5f | 来源:发表于2019-06-30 23:39 被阅读0次

//已知两个链表A和B分别表示两个集合,其元素递增排列。

#include <iostream>

#include <stdlib.h>

#include <stdio.h>

using namespace std;

typedef struct Lnode{

    int data;

    struct Lnode *next;

}Lnode,*LinkList;

void init_LinkList(LinkList &head){

    int x;

    head = (LinkList)malloc(sizeof(Lnode));

    head->next= NULL;

    Lnode *p,*r = head;

    cout<<"请输入数字(-1结束):";

    cin>>x;

    while(x != -1){

        p = (Lnode*)malloc(sizeof(Lnode));

        p->next = r->next;

        p->data = x;

        r->next = p;

        r= p;

        cout<<"请输入数字(-1结束):";

        cin>>x;

       }

   } //编制函数,求A与B的交集,并存放于A链表中。

void create_unity(LinkList &L1,LinkList L2){

    Lnode *p = L1,*p1 = L1->next,*p2= L2->next,*temp;

    while(p1!=NULL&&p2!=NULL){

        if(p1->data>p2->data){

                p2 = p2->next;

        }else if(p1->data < p2->data){

            temp = p1;

            p1 = p1->next;

            p->next = p1;

            free(temp);

         }else{

            p->next = p1;

            p = p1;

            p1 = p1->next;

            p2 = p2->next;

            }

        }

        p->next = NULL;

}

int main(){

    LinkList L1,L2;

    init_LinkList(L1);

    init_LinkList(L2);

    Lnode *p; p = L1->next;

    while(p != NULL){

        cout<<p->data<<" ";

        p = p->next;

      }

    cout<<endl;

    p = L2->next;

    while(p != NULL){

        cout<<p->data<<" ";

        p = p->next;

    }

    cout<<endl;

    create_unity(L1,L2);

    p = L1->next;

    while(p != NULL){

        cout<<p->data<<" ";

        p = p->next;

    }

}

相关文章

  • 王道2.15

    //已知两个链表A和B分别表示两个集合,其元素递增排列。#include #include #include ...

  • 《天才闪光》论人的价值观

    Day122 2.15

  • 打卡专用

    2.15任务完成

  • 2.15

    爸爸是个很酷的人!

  • 2.15

    除夕夜的路上,到处都是放烟花爆竹的人,汽车慢悠悠的行驶在乡村公路上,看着烟花在空中绽放,划出一个个美丽的圈圈,有黄...

  • 2.15

    离回去还有10天了,今天吃了很多樱桃,开心果,红棕鱼,面条。好饱,好好看书,不软弱,多思考,

  • 2.15

    看完第九章的简答题,背20个专业词汇。

  • 2.15

    青山绿水之间, 我想牵着你的手, 走过这座桥, 桥上是绿叶红花, 桥下是流水人家, 桥的那头是青丝, 桥的这头是白...

  • 2.15

    刘同有本书名叫《谁的青春不迷茫》,我在它被拍成电影的早几年前就看过了,但是大多内容早已忘得不剩下什么了,想来...

  • 2.15

    我们都将久病成医,带着理解和体贴去劝慰他人。 我们都将豁达知理,懂得彼此的为难并且不在苦苦相逼。

网友评论

    本文标题:王道2.15

    本文链接:https://www.haomeiwen.com/subject/pnbicctx.html