代码:
#include
#define M 300
typedef int datatype;
typedef struct sq
{
datatype *data;
int length;
}sq;
sq *create()
{
sq *l;
l=(sq *)malloc(sizeof(sq *));
if(l)
l->length=0;
return l;
}
int init(sq *l)
{
int i=0;
printf("\nplease input the data(end of 0):");
scanf("%d",&l->data[i]);
l->length=1;
while(l->data[i]!=0)
{
i++;
l->length++;
scanf("%2d",&l->data[i]);
}
l->length--;
}
int together(sq *l1,sq *l2,sq *l3)
{
int i=0,j=0,k=0;
l3->length=l1->length+l2->length;
while(i<l1->length&&j<l2->length)
{
if(l1->data[i]<=l2->data[j])
{l3->data[k]=l1->data[i];
k++;i++;
}
else
{l3->data[k]=l2->data[j];
k++;j++;}
}
while(l1->length)
{l3->data[k]=l1->data[i];
k++;i++;}
while(l2->length)
{l3->data[k]=l2->data[j];
k++;j++;}
}
int output(sq *l)
{
int i;
for(i=0;ilength;i++)
printf("%2d",l->data[i]);
}
int reverse(sq *l)
{
int i,j,temp;
i=0;
j=l->length-1;
while(i!=j)
{
temp=l->data[i];
l->data[i]=l->data[j];
l->data[j]=temp;
i++;
j--;
}
}
int main()
{
sq *l1,*l2,*l3;
l3=(sq *)malloc(sizeof(sq *));
l1=create();
init(l1);
printf("\nthe l1 is:");
output(l1);
l2=create();
init(l2);
printf("\nthe l2 is:");
output(l2);
l3=create();
together(l1,l2,l3);
printf("\nthe l3 is:");
output(l3);
printf("\nreverse l3:");
reverse(l3);
output(l3);
getch();
}