c语言怎么给两个线性表赋值,怎么用链表实现两个线性表的合并

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值