C/C++ code#include
#include
#define MAX1 10
#define MAX2 6
typedef struct list
{
int num;
struct list *next;
}*link;
int data1[MAX1] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int data2[MAX2] = { 11, 12, 13, 14, 15, 16 };
link create_list(link head, int *data, int MAX)
{
link newnode;
link pointer;
int i;
head = malloc(sizeof(*head));
if(head==NULL)
printf("Memory allocate Failure!\n");
else
{
head->num = data[0];
head->next = NULL;
pointer = head;
for(i=1; i
{
newnode = malloc(sizeof(*newnode));
if(newnode==NULL) break;
newnode->num = data[i];
newnode->next = NULL;
pointer->next = newnode;
pointer = newnode;
}
}
return head;
}
void free_list(link head)
{
link pointer;
while(head!=NULL)
{
pointer = head;
head = head->next;
free(pointer);
}
}
void print_list(link head)
{
if(head==NULL)
printf("empty!");
while(head!=NULL)
{
printf("[%d]", head->num);
head = head->next;
}
putchar('\n');
}
link concat(link head1, link head2)
{
link pointer = head1;
if(head1!=NULL && head2!=NULL)
{
while(pointer->next!=NULL)
pointer = pointer->next;
pointer->next = head2;
}
else if(head1==NULL && head2!=NULL)
head1 = head2;
return head1;
}
int main(void)
{
link head = NULL;
link head1 = NULL;
link head2 = NULL;
head1 = create_list(head1, data1, MAX1);
head2 = create_list(head2, data2, MAX2);
if(head1!=NULL && head2!=NULL)
{
printf("Input data :\n");
print_list(head1);
print_list(head2);
head = concat(head1, head2);
//head = concat(NULL, head2);
//head = concat(head1, NULL);
//head = concat(NULL, NULL); free(head1); free(head2);
printf("After concat:\n");
print_list(head);
free(head1);
}
return 0;
}
------解决方案--------------------C/C++ code根据你的要求写了一个
#include
#include
typedef struct NODE{
int data;
struct NODE *next;
}LNode,*Linklist;
void creatList(Linklist L,int LA[],int len);
int judgeList(Linklist L,int x);
int insertList(Linklist L,int x);
void printList(Linklist L);
int main()
{
int LA[]={3,5,8,11};
int LB[]={2,6,8,9,11,15,20};
LNode L;
int LAlen;
int LBlen;
int i;
LAlen=sizeof(LA)/sizeof(LA[0]);
LBlen=sizeof(LB)/sizeof(LA[0]);
creatList(&L,LA,LAlen);
printList(&L);
for (i=0;i
if(!judgeList(&L,LB[i]))
insertList(&L,LB[i]);
printList(&L);
return 0;
}
void creatList(Linklist L,int LA[],int len)
{
/* 用长度为len的数组LA创建链表L */
Linklist p;
Linklist q;
int i;
p=L;
L->next=NULL;
for (i=0;i
{
q=(int *)malloc(sizeof(int));
q->data=LA[i];
q->next=NULL;
p->next=q;
p=p->next;
}
p=NULL;
}
int judgeList(Linklist L,int x)
{
/* 判断链表中是否已经存在元素x ,若存在返回1,否则返回0 */
Linklist p;
p=L;
while(p->next)
{
if(p->next->data==x)
return 1;
p=p->next;
}
return 0;
}
int insertList(Linklist L,int x)
{
/* 在链表末尾插入元素x */
Linklist p;
Linklist q;
p=L;
while(p->next)
p=p->next;
q=(int *)malloc(sizeof(int));
q->data=x;
q->next=NULL;
p->next=q;
}
void printList(Linklist L)
{
/* 输出链表元素 */
Linklist p;
p=L;
while(p->next)
{
printf("%d ",p->next->data);
p=p->next;
}
printf("\n");
}
3 5 8 11
3 5 8 11 2 6 9 15 20
Press any key to continue