赞
踩
代码如下:
/***************************************************************** 实现一个函数,功能为输入两个整数数组,A,B 实现A=A+B,也就是将A与B进行合并 并且对数组A里面的数字大小进行排序。 ******************************************************************/ #include<stdio.h> #include<stdlib.h> //创建结构体 struct ptr { int data; struct ptr* next; }; //创建链表头 struct ptr* creat_headlist() { struct ptr*Head_list=(struct ptr*)malloc(sizeof(struct ptr)); Head_list->next=NULL; return Head_list; } //创建结点 struct ptr* creat_conection_polt(int data) { struct ptr*c_list=(struct ptr*)malloc(sizeof(struct ptr)); c_list->data=data; c_list->next=NULL; return c_list; } //插入结点 void in_set(struct ptr * Head_list,int data) { struct ptr*set_list=creat_conection_polt(data); set_list->next=Head_list->next; Head_list->next=set_list; } //找到此链表的最后一个结点 struct ptr* last_list(struct ptr*Head_list) { struct ptr*next_list_last=(struct ptr*)malloc(sizeof(struct ptr)); while(1) { if(Head_list->next==NULL) { return Head_list; break; } else { next_list_last=Head_list->next; Head_list=next_list_last; } } } //对此链表的数据进行从小到大的排序利用冒泡排序法进行操作 void pai_xu(struct ptr* Head_list) { struct ptr*next_ptri=(struct ptr*)malloc(sizeof(struct ptr)); struct ptr*next_ptrj=(struct ptr*)malloc(sizeof(struct ptr)); for(next_ptri=Head_list->next;next_ptri!=NULL;next_ptri=next_ptri->next) { for(next_ptrj=next_ptri->next;next_ptrj!=NULL;next_ptrj=next_ptrj->next) { if(next_ptri->data>next_ptrj->data) { int ptri=next_ptri->data;next_ptri->data=next_ptrj->data;next_ptrj->data=ptri; } } } free(next_ptri); free(next_ptrj); } //打印链表 void print_list(struct ptr* Head_list) { struct ptr* next_list=(struct ptr*)malloc(sizeof(struct ptr)); while(1) { if(Head_list->next==NULL) { return; } else { next_list=Head_list->next; Head_list=next_list; printf("%d",Head_list->data); printf("\n"); } } } //主函数 main() { struct ptr* h_list=creat_headlist(); struct ptr* H_list=creat_headlist(); int shuzi,shuzi_again; while(1) { printf("请输入整数:\n"); scanf("%d",&shuzi); in_set(h_list,shuzi); printf("是否还需要输入?Y/N\n"); setbuf(stdin,NULL); char panduan; scanf("%c",&panduan); if(panduan=='N'||panduan=='n') { break; } } while(1) { printf("请输入整数:\n"); scanf("%d",&shuzi_again); in_set(H_list,shuzi_again); printf("是否还需要输入?Y/N\n"); setbuf(stdin,NULL); char panduan_again; scanf("%c",&panduan_again); if(panduan_again=='N'||panduan_again=='n') { break; } } struct ptr* l_list=last_list(h_list); l_list->next=H_list->next; pai_xu(h_list); print_list(h_list); }
总结下,首先是进行链表的创建,最后是找到第一个链表的最后一个结点,将其与第二个链表的第一个结点连接,组成一个新的链表。
关于排序,采用的是冒泡排序的方法,结点的值进行比较,
第一次循环将最小的放在第一个结点,第二次将第二小的放在第二个结点,以此列推。。。。。。
冒泡排序:如下
void pai_xu(struct ptr* Head_list) { struct ptr*next_ptri=(struct ptr*)malloc(sizeof(struct ptr)); struct ptr*next_ptrj=(struct ptr*)malloc(sizeof(struct ptr)); for(next_ptri=Head_list->next;next_ptri!=NULL;next_ptri=next_ptri->next) { for(next_ptrj=next_ptri->next;next_ptrj!=NULL;next_ptrj=next_ptrj->next) { if(next_ptri->data>next_ptrj->data) { int ptri=next_ptri->data;next_ptri->data=next_ptrj->data;next_ptrj->data=ptri; } } } free(next_ptri); free(next_ptrj); }
最终实现这一结果,谢谢大家,如有更好的意见,欢迎评论留言。
谢谢大家!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。