求问,这个代码要怎么写
1条回答 默认 最新
- bostonAlen 2021-09-15 16:43关注
#include <stdio.h> #include <stdlib.h> #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<MAX; ++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; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用 1
悬赏问题
- ¥15 微信小程序协议怎么写
- ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
- ¥20 怎么用dlib库的算法识别小麦病虫害
- ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
- ¥15 java写代码遇到问题,求帮助
- ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
- ¥15 有了解d3和topogram.js库的吗?有偿请教
- ¥100 任意维数的K均值聚类
- ¥15 stamps做sbas-insar,时序沉降图怎么画
- ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看