• 线性表算法设计题2.23


     设线性表A=(a1,a2……,am),B=(b1,b2……,bn),试写一个按下列规则合并A,B为线性表C的算法,即使得

     C=(a1,b1,……am,bm,……bn) 当m<=n时;

     C=(a1,b1,……an,bn,……am) 当m>n时; 

    C code:

     #include<stdio.h>

    #include<stdlib.h>
    #define LIST_INIT_SIZE 10
    #define LIST_INCREMENT 2
    #define ERROR 0
    #define OK 1
    #define OVERFLOW -1
    #define TRUE 1
    typedef 
    int Status;

    struct SqList
    {
         
    int *elem;
         
    int length;
         
    int listsize;
    };

    void InitList(SqList &L)
    {
         L.elem
    =(int*)malloc(LIST_INIT_SIZE*sizeof(int));
         
    if(!L.elem)
           exit(OVERFLOW);
         L.length
    =0;
         L.listsize
    =LIST_INIT_SIZE;
    }

    Status ListInsert(SqList 
    &L,int i,int e)
    {
         
    int *newbase,*q,*p;
         
    if(i<1||i>L.length+1)
           
    return ERROR;
         
    if(L.length==L.listsize)
         {
              newbase
    =(int*)realloc(L.elem,(L.listsize+LIST_INCREMENT)*sizeof(int));
              
    if(!newbase)
                exit(OVERFLOW);
              L.elem
    =newbase;
              L.listsize
    +=LIST_INCREMENT;
             
            }
            q
    =L.elem+i-1;
            
    for(p=L.elem+L.length-1; p>=q; --p)
              
    *(p+1)=*p;
            
    *q=e;
            
    ++L.length;
            
    return OK;
    }

    void CreateList(SqList &L,int len)
    {
         
    int i;
         
    for(i=1; i<=len; i++)
         {
              ListInsert(L,i,rand()
    %100); 
            }
    }

    void CreateList_C(SqList La,SqList Lb,SqList &Lc)
    {
         
    int i,j;
         
    if(Lc.listsize<=La.length+Lb.length)
         {
              Lc.listsize
    +=La.length+Lb.length;
            }
         
    for(i=0,j=1; j<=La.length+Lb.length && i<La.length && i<Lb.length; i++)
         {
              ListInsert(Lc,j,La.elem[i]);
              ListInsert(Lc,j
    +1,Lb.elem[i]);
              j
    +=2;
            }
            
    if(La.length<Lb.length)
            {
                 
    for(; i<Lb.length; i++,j++)
                 {
                      ListInsert(Lc,j,Lb.elem[i]);
                    }
            }
            
    else
            {
                 
    for(; i<La.length; i++,j++)
                 {
                      ListInsert(Lc,j,La.elem[i]);
                    }
            }
    }

    void ListTraverse(SqList L,void(*vist)(int&))
    {
         
    int *p=L.elem;
         
    int i;
         
    for(i=1; i<=L.length; i++)
         {
              vist(
    *p++);
            }
            printf(
    "\n");
    }

    void print1(int &c)
    {
         printf(
    "%d ",c);
    }

    int main()
    {
         SqList La,Lb,Lc;
         InitList(La);
         InitList(Lb);
         InitList(Lc);
         
    int La_len,Lb_len;
         printf(
    "input length of La: ");
         scanf(
    "%d",&La_len);
         printf(
    "input length of Lb: ");
         scanf(
    "%d",&Lb_len);
         CreateList(La,La_len);
         CreateList(Lb,Lb_len);
         printf(
    "List a: ");
         ListTraverse(La,print1);
         printf(
    "List b: ");
         ListTraverse(Lb,print1);
         CreateList_C(La,Lb,Lc);
         printf(
    "List c: ");
         ListTraverse(Lc,print1);
    }
  • 相关阅读:
    贝壳找房魔法师顾问[并查集+DAG判断]
    Ubuntu 18.04 安装 virtualbox
    Ubuntu 编译安装 nDPI
    Ubuntu 16.04 安装WPS
    HDU 5046 Airport【DLX重复覆盖】
    Codeforces 622C Not Equal on a Segment 【线段树 Or DP】
    UVA 10635 Prince and Princess【LCS 问题转换为 LIS】
    LA 2995 Image Is Everything
    LA 3708 Graveyard
    HDU 5212 Code【莫比乌斯反演】
  • 原文地址:https://www.cnblogs.com/cpoint/p/2031299.html
Copyright © 2020-2023  润新知