有序顺序表的合并
#include <stdio.h>
#define maxlength 50
typedef struct
{
int elements[maxlength];
int last;
} LIST;
typedef int position;
void Insert(int x,position p,LIST &L)
{
position q;
if(L.last>=maxlength-1)
printf("list is full");
else if((p>L.last+1)||(p<1))
printf("position does't exist");
else
{
for(q=L.last;q>=p;q--)
L.elements[q+1]=L.elements[q];
L.last=L.last+1;
L.elements[p]=x;
}
}
void Merge(LIST L1,LIST L2,LIST &L3)
{
int i=1,j=1,k=1;
while(i<=L1.last&&j<=L2.last)
{
if(L1.elements[i]<L2.elements[j])
{
L3.elements[k]=L1.elements[i];
i++;k++;
}
else
{
L3.elements[k]=L2.elements[j];
j++;k++;
}
}
while (i<=L1.last)
{
L3.elements[k]=L1.elements[i];
i++;k++;
}
while (j<=L2.last)
{
L3.elements[k]=L2.elements[j];
j++;k++;
}
L3.last=k-1;
}
void print(LIST L)
{
int i;
for(i=1;i<=L.last;i++)
printf("%d\t",L.elements[i]);
putchar('\n');
}
void main()
{
LIST L1,L2,L3;
L1.last=0;
L2.last=0;
Insert(1,1,L1);
Insert(3,2,L1);
Insert(5,3,L1);
Insert(2,1,L2);
Insert(4,2,L2);
Insert(6,3,L2);
print(L1);
print(L2);
Merge(L1,L2,L3);
print(L3);
}
链表的复制
#include <stdio.h>
#define N 50
struct celltype
{
int element;
celltype *next;
} ;
typedef celltype *LIST;
typedef celltype *position;
void CreatListR(LIST &L,int a[],int n)
{
position s,r;
int i;
L=new celltype;
r=L;
for(i=0;i<n;i++)
{
s=new celltype;
s->element=a[i];
r->next=s;
r=s;
}
r->next=NULL;
}
void copy(LIST L1,LIST &L2)
{
position p,s,r;
p=L1->next;
r=L2;
while (p!=NULL)
{
s=new celltype;
s->element=p->element;
r->next=s;
r=s;
}
r->next=NULL;
}
void print(LIST L)
{
position p;
p=L->next;
while(p!=NULL)
{
printf("%d",p->element);
p=p->next;
}
}
void main()
{
LIST L1,L2;
int a[N],i,n;
printf("��������������L1Ԫ�صĸ���:");
scanf("%d",&n);
printf("������L1�е�Ԫ��:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
CreatListR(L1,a,n);
print(L1);
copy(L1,L2);
print(L2);
}
链表的合并
#include <stdio.h>
#define N 50
struct celltype
{
int element;
celltype *next;
} ;
typedef celltype* LIST;
typedef celltype* position;
void CreatListR(LIST &L,int a[],int n)
{
position s,r;
int i;
L=new celltype;
r=L;
for(i=0;i<n;i++)
{
s=new celltype;
s->element=a[i];
r->next=s;
r=s;
}
r->next=NULL;
}
void Merge (LIST L1, LIST L2,LIST &L3)
{
position p=L1->next,q=L2->next,r,s;
L3=new celltype;
while(p!=NULL&&q!=NULL)
{
if(p->element<q->element)
{
s=new celltype;
s->element=p->element;
p=p->next;
r->next=s;
r=s;
}
else
{
s=new celltype;
s->element=q->element;
q=q->next;
r->next=s;
r=s;
}
}
if(q!=NULL) p=q;
while (p!=NULL)
{
s=new celltype;
s->element=p->element;
p=p->next;
r->next=s;
r=s;
}
r->next=NULL;
}
void print(LIST L)
{
position p;
p=L->next;
while(p!=NULL)
{
printf("%d",p->element);
p=p->next;
}
}
void main()
{
LIST L1,L2,L3;
int a[N],b[N],i,n1,n2;
printf("��������������L1Ԫ�صĸ���:");
scanf("%d",&n1);
printf("�밴�ǽ�������L1�е�Ԫ��:");
for(i=0;i<n1;i++)
{
scanf("%d",&a[i]);
}
printf("��������������L2Ԫ�صĸ���:");
scanf("%d",&n2);
printf("�밴�ǽ�������L2�е�Ԫ��:");
for(i=0;i<n2;i++)
{
scanf("%d",&b[i]);
}
CreatListR(L1,a,n1);
CreatListR(L2,b,n2);
print(L1);
putchar('\n');
print(L2);
Merge(L1,L2,L3);
print(L3);
}