algorithm_two_seven.cpp

来自「数据结构的的源码」· C++ 代码 · 共 59 行

CPP
59
字号
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define Elemtype int

typedef struct{
   Elemtype *elem;
   int Lenth;
}Sqlist;


char InitList(Sqlist &L,char B)
{
  L.elem=(Elemtype *)malloc(10*sizeof(Elemtype));
  if(L.elem==NULL)return 0;
  L.Lenth=10;
  for(int i=0;i<10;i++)
  {
     L.elem[i]=B++;  
  }
}

void Mergelist(Sqlist La,Sqlist Lb,Sqlist &Lc)
{
 Elemtype *P1,*P2,*P3,*P1_Last,*P2_Last;
 P1=La.elem;
 P2=Lb.elem;
 P1_Last=La.elem+La.Lenth-1;
 P2_Last=Lb.elem+Lb.Lenth-1;

 Lc.Lenth=(La.Lenth+Lb.Lenth);
 P3=Lc.elem=(Elemtype *)malloc(Lc.Lenth*sizeof(Elemtype));
while((P1<=P1_Last)&&(P2<P2_Last))
{
  if(*P1<*P2)*P3++=*P1++;
  else
     *P3++=*P2++; 
}
while(P2<=P2_Last)
    *P3++=*P2++;
while(P1<=P1_Last)
    *P3++=*P1++;  

}

void main()
{
	int i=0;
   Sqlist La,Lb,Lc;
   InitList(La,'A');
   InitList(Lb,'a');
   Mergelist(La,Lb,Lc);
  while(i<Lc.Lenth)
  {printf("%c\n",Lc.elem[i]);
  i++;}
   printf("%d\n",Lc.Lenth);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?