• 线性表的合并 一


    一般集合的并集问题

    【问题描述】

    已知两个集合A与B,现要求一个新的集合A=AUB;

    例如:A=(1,2,4,6,7),B=(2,4,8,9);

            合并后   A=(1,2,4,6,7,8,9)

    1.创建结点

    1 typedef struct Lnode
    2 {
    3      int data;
    4      struct Lnode * next;
    5 
    6 }Lnode,*PLnode;
    View Code

    2.创建线性表

     1 PLnode createList(PLnode L)
     2 {
     3     L = (PLnode)malloc(sizeof(Lnode));
     4     PLnode Ptail = L;
     5     L->next = NULL;
     6 
     7     printf("input length:
    ");
     8     scanf("%d",&L->data);
     9 
    10     for(int i = 1;i<=L->data;i++)
    11     {
    12         PLnode pnew = (PLnode)malloc(sizeof(Lnode));
    13         printf("input data:
    ");
    14         scanf("%d",&pnew->data);
    15 
    16         Ptail->next = pnew;
    17         Ptail = pnew;
    18         Ptail->next = NULL;
    19 
    20     }
    21     return L;
    22 }
    View Code

    3.从LA中查找元素e

     1 bool locateElem(PLnode LA,int e)
     2 {
     3     PLnode p = LA;
     4     for(int i =1;i<=LA->data;i++)
     5     {
     6         p = p->next;
     7         if(p->data == e)
     8         {
     9             return false;     //如果有相同元素返回false
    10         }
    11     }
    12     return true;
    13 }
    View Code

    4.插入操作

     1 void insertList(PLnode LA,int e)
     2 {
     3     PLnode p = LA;
     4 
     5     for(int i=1;i<=LA->data;i++)
     6     {
     7         p = p->next;
     8     }
     9 
    10     PLnode pnew = new Lnode;
    11     pnew->data = e;
    12     p->next = pnew;
    13     pnew->next=NULL;
    14     LA->data++;            //长度增加
    15 }
    View Code

    5.合并

     1 void mergeList(PLnode LA,PLnode LB)
     2 {
     3     int len2 = LB->data;
     4 
     5     PLnode p = LB;
     6     for(int i=1;i<=len2;i++)
     7     {
     8       p = p->next;
     9       if(locateElem(LA,p->data))    //如果没有相同的元素就插入
    10       {
    11           insertList(LA,p->data);
    12       }
    13     }
    14 }
    View Code

    6.完整代码

      1 #include<stdio.h>
      2 #include<stdlib.h>
      3 
      4 typedef struct Lnode
      5 {
      6      int data;
      7      struct Lnode * next;
      8 
      9 }Lnode,*PLnode;
     10 
     11 PLnode createList(PLnode L);
     12 void traverse(PLnode L);
     13 void insertList(PLnode LA,int e);
     14 bool locateElem(PLnode LA,int e);
     15 void mergeList(PLnode LA,PLnode LB);
     16 int main()
     17 {
     18     PLnode LA;
     19     PLnode LB;
     20 
     21     LA = createList(LA);    //创建线性表LA,再将头指针赋值给LA;
     22     LB = createList(LB);    //创建线性表LA,再将头指针赋值给LA;
     23 
     24     traverse(LA);           //传入头指针
     25     traverse(LB);           //传入头指针
     26     mergeList(LA,LB);       //集合并集
     27     traverse(LA);           //再输出验证一下
     28     return 0;
     29 }
     30 PLnode createList(PLnode L)
     31 {
     32     L = (PLnode)malloc(sizeof(Lnode));
     33     PLnode Ptail = L;
     34     L->next = NULL;
     35 
     36     printf("input length:
    ");
     37     scanf("%d",&L->data);
     38 
     39     for(int i = 1;i<=L->data;i++)
     40     {
     41         PLnode pnew = (PLnode)malloc(sizeof(Lnode));
     42         printf("input data:
    ");
     43         scanf("%d",&pnew->data);
     44 
     45         Ptail->next = pnew;
     46         Ptail = pnew;
     47         Ptail->next = NULL;
     48 
     49     }
     50     return L;
     51 }
     52 
     53 void traverse(PLnode L)
     54 {
     55     PLnode p = L;
     56     while(p->next!=NULL)
     57     {
     58         printf("%d ",p->next->data);
     59         p=p->next;
     60     }
     61     printf("
    ");
     62 }
     63 
     64 void mergeList(PLnode LA,PLnode LB)
     65 {
     66     int len2 = LB->data;
     67 
     68     PLnode p = LB;
     69     for(int i=1;i<=len2;i++)
     70     {
     71       p = p->next;
     72       if(locateElem(LA,p->data))
     73       {
     74           insertList(LA,p->data);
     75       }
     76     }
     77 
     78 
     79 }
     80 
     81 bool locateElem(PLnode LA,int e)
     82 {
     83     PLnode p = LA;
     84     for(int i =1;i<=LA->data;i++)
     85     {
     86         p = p->next;
     87         if(p->data == e)
     88         {
     89             return false;
     90         }
     91     }
     92     return true;
     93 }
     94 
     95 void insertList(PLnode LA,int e)
     96 {
     97     PLnode p = LA;
     98 
     99     for(int i=1;i<=LA->data;i++)
    100     {
    101         p = p->next;
    102     }
    103 
    104     PLnode pnew = new Lnode;
    105     pnew->data = e;
    106     p->next = pnew;
    107     pnew->next=NULL;
    108     LA->data++;
    109 }
    View Code
  • 相关阅读:
    NTFS的交换数据流ADS应用
    解决binwalk运行提示缺少LZMA模块
    蓝牙扫描工具btscanner修复暴力扫描模式
    iOS 11开发教程(二十)iOS11应用视图美化按钮之设置按钮的状态
    Visual Studio 2017 版本 15.5.5
    iOS 11开发教程(十九)iOS11应用视图美化按钮之设置按钮的外观
    jquery的基本api
    vue知识点总结
    历史记录
    http加密原理
  • 原文地址:https://www.cnblogs.com/wwww2/p/11623701.html
Copyright © 2020-2023  润新知