求问,这个代码要怎么写
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 fluent的在模拟压强时使用希望得到一些建议
- ¥15 STM32驱动继电器
- ¥15 Windows server update services
- ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
- ¥15 模糊pid与pid仿真结果几乎一样
- ¥15 java的GUI的运用
- ¥15 Web.config连不上数据库
- ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
- ¥15 怎么配置广告联盟瀑布流
- ¥15 Rstudio 保存代码闪退