#include<stdio.h> #include <stdlib.h> typedef struct CStudent { int m_num; float m_score; struct CStudent* pNext; }STU; STU* CreatLink(int n); void CombineLink(STU* a,STU* b); STU* SortLink(STU* phead); void PrintfLink(STU* phead); void FreeLink(STU* phead); int main() { int N,M; STU* a; STU* b; scanf("%d",&N); scanf("%d",&M); a=CreatLink(N); b=CreatLink(M); CombineLink(a,b); PrintfLink(SortLink(a)); FreeLink(a); return 0; } STU* CreatLink(int n) { STU* phead,*pt,*p; int i; for(i=1;i<=n;i++) { p=(STU*)malloc(sizeof(STU)); scanf("%d",&p->m_num); scanf("%f",&p->m_score); if (i==1) { phead=p; pt=phead; } else { pt->pNext=p; pt=p; } } p->pNext=NULL; return phead; } void CombineLink(STU* a,STU* b) { STU* pend; pend=a; while(pend->pNext!=NULL) pend=pend->pNext; pend->pNext=b; } STU* SortLink(STU* phead) { //1.交换数据域---不用这种 //2.交换结点---选择排序--另创建一个链表 STU* new_phead=NULL,*pend; //新链表的头、尾结点 STU *pbefore,*pmin,*pcur;//保存前结点,最小值的结点,当前结点 while(phead!=NULL) { for(pcur=phead,pmin=phead;pcur->pNext!=NULL;pcur=pcur->pNext) //找出每轮的最小值 { if (pcur->pNext->m_num < pmin->m_num) { pbefore=pcur; pmin=pcur->pNext; } } if (new_phead==NULL) //如果新链表头还是空的 { new_phead=pmin; //头 pend=pmin;//尾,下面用后插法一个个加进来 } else //有了头 { pend->pNext=pmin; pend=pmin; //新的尾巴 } //把最小值从原链表中脱离 if(pmin==phead) //刚好是头 phead=phead->pNext; //原链表头结点前移 else pbefore->pNext=pmin->pNext; //断开 } if(new_phead!=NULL) pend->pNext=NULL; //尾结点指针域为空 phead=new_phead; return phead; } void PrintfLink(STU* phead) { STU* p=phead; //保护链表头; while(p!=NULL) { printf("%d %.0f\n",p->m_num,p->m_score); p=p->pNext; } } void FreeLink(STU* phead) { STU *p; while(phead!=NULL) { p=phead; phead=phead->pNext; free(p); } return; }
0.0分
0 人评分
C二级辅导-同因查找 (C语言代码)浏览:505 |
你的开发任务 (C++代码)写到一半,等有心情回来补全浏览:923 |
小九九 (C语言代码)浏览:884 |
C语言考试练习题_排列 (C++代码)浏览:713 |
多输入输出练习1 (C语言代码)浏览:1219 |
不会做的浏览:954 |
Tom数 (C语言代码)浏览:517 |
链表数据求和操作 (C语言代码)浏览:1035 |
敲七 (C++代码)浏览:1119 |
多输入输出练习2 (C语言代码)浏览:1709 |