就是因为打错字,调试啊调试,这是在VS下面写的.
#include
#include
#define LIST_INIT_SIZE 100
#define LIST_INC 10
typedef struct list_t {
int *element; /* 线性表的基址 */
int length;
int listsize;
} list_t, *list_p;
/* 初始 */
void initlist(list_p lp)
{
lp->element = (int *)malloc(LIST_INIT_SIZE * sizeof(int));
if (!lp->element)
exit(1);
lp->length = 0;
lp->listsize = LIST_INIT_SIZE;
}
/* 插入 */
int insertlist(list_p l, int i, int e)
{
int *base;
int *q;
int *p;
if (i < 1 || i > l->length + 1)
return 0;
if (l->length >= l->listsize)
{
base = (int *)realloc(l->element,(l->length + LIST_INC) * sizeof(int));
if (!base)
exit(1);
l->element = base;
l->listsize += LIST_INC;
}
q = &(l->element[i - 1]);
for (p = &(l->element[l->length -1]); p >= q; p--)
*(p+1) = *p;
*q = e;
++l->length;
return 1;
}
/* 合并 */
void margelist(list_p la, list_p lb, list_p lc)
{
int i = 0;
int j = 0;
int k = 0;
int ai;
int bj;
int la_len = la->length;
int lb_len = lb->length;
initlist(lc);
while (( i < la_len) && (j < lb_len))
{
ai = la->element[i];
bj = lb->element[j];
if (ai <= bj)
{
insertlist(lc, ++k, ai);
i++;
}
else
{
insertlist(lc, ++k, bj);
j++;
}
}
while (i < la_len)
{
ai = la->element[i++];
insertlist(lc, ++k, ai);
}
while (j < lb_len)
{
bj = lb->element[j++];
insertlist(lc, ++k, bj);
}
}
/* 打印 */
void print(int *arr, int n)
{
int i;
for (i = 0; i < n; i++)
printf("%10d\n", arr[i]);
printf("\n");
}
int main()
{
int i, j, k;
list_t la, lb, lc;
initlist(&la);
initlist(&lb);
initlist(&lc);
for (i = 0; i < 10; i++)
{
la.element[i] = i*i;
la.length++;
}
for (j = 0; j < 15; j++)
{
lb.element[j] = j*(51 + j*j);
lb.length++;
}
printf("The Element of LA\n");
print(la.element, la.length);
printf("The Element of LB\n");
print(lb.element, lb.length);
margelist(&la, &lb, &lc);
print(lc.element, lc.length);
system("pause");
return 0;
}
阅读(914) | 评论(1) | 转发(0) |