将链表链接并且对链表排序(通过插入和删除函数实现
来源:互联网 发布:苹果怎么打开php文件 编辑:程序博客网 时间:2024/05/20 12:47
#include<stdio.h>#include<stdlib.h>//#pragma pack (1)struct stu{ int ID; int score; struct stu *next;};struct stu *SortList(struct stu *pHead,int sum);struct stu *pDelete(struct stu* pHead,struct stu *pNow);struct stu *pInsert(struct stu* pHead,int n,struct stu*pt);void scan(struct stu *pHead);int main(int argc,const char *argv[]){ stu *p1,*p2,*head1,*head2,*mid1,*mid2; int sum; int N,M; int t=1; scanf("%d%d",&N,&M); p1=(struct stu*)malloc(sizeof(struct stu)); while(t<=N) { if (t==1) { scanf("%d%d",&p1->ID,&p1->score); head1=p1; head1->next=NULL; } else { mid1=p1; p1=(struct stu*)malloc(sizeof(struct stu)); mid1->next=p1; scanf("%d%d",&p1->ID,&p1->score); p1->next=NULL; } t++; } p2=(stu*)malloc(sizeof(stu)); t=1; while(t<=M) { if (t==1) { scanf("%d%d",&p2->ID,&p2->score); head2=p2; head2->next=NULL; } else { mid2=p2; p2=(struct stu*)malloc(sizeof(struct stu)); mid2->next=p2; scanf("%d%d",&p2->ID,&p2->score); p2->next=NULL; } t++; } p1->next=head2; sum=N+M; head1=SortList(head1,sum); scan(head1); return 0;}struct stu *SortList(struct stu *pHead,int sum)//节点排序 返回一个头节点指针{ int min,t=1,i; struct stu *temp,*pt,*p; while (sum) { temp=pHead; i=1; while (i<t) { temp=temp->next; i++; } p=temp; min=p->ID; pt=temp; while (p!=NULL) { if(min>p->ID) { min=p->ID; pt=p; } p=p->next; } if (pt==temp) { break; } else { pHead=pDelete(pHead,pt); pHead=pInsert(pHead,t,pt); } t++; sum--; } return pHead;}struct stu *pDelete(struct stu* pHead,struct stu *pNow)//删除节点{ struct stu *p,*forw; p=pHead; while(p!=NULL) { if(pHead==pNow) { pHead=p->next; break; } else if(p==pNow&&p->next==NULL) { forw->next=NULL; } else if(p==pNow) { forw->next=p->next; break; } forw=p; p=p->next; } return pHead; }struct stu *pInsert(struct stu * pHead,int n,struct stu *pt)//需要插入的节点的位置{ int i=1; struct stu*p,*forw; p=pHead; forw=pHead; while (i<=n) { if(n==1) { pt->next=pHead; pHead=pt; break; } else if(i==n) { forw->next=pt; pt->next=p; break; } forw=p; p=p->next; i++; } return pHead; }void scan(struct stu *pHead)//遍历节点{ struct stu *p; p=pHead; while (p!=NULL) { printf("%d %d\n",p->ID,p->score); p=p->next; }}
0 0
- 将链表链接并且对链表排序(通过插入和删除函数实现
- 线性表链接实现--双循环链表
- C实现链表一般接口函数(插入,删除,排序,等等)
- Java实现链表的插入,删除,排序,输出
- 双向链表链表的实现
- 链表的创建、插入、删除、排序和逆置
- 对链表排序,用插入排序
- 动态链表链表
- 双向链表链表
- 【数据结构】线性表的顺序表示和实现,实现插入和删除算法,并且实现顺序表的合并
- 链表的插入 删除 排序 倒叙
- 链表插入,删除,排序,反转
- 链表的删除,插入,查找,排序
- 双向链表排序,插入、删除
- 链表的插入删除和查找_C++实现
- 数据结构Java实现 --单向链表的插入和删除
- C++链表的创建和插入删除的实现
- C语言实现链表节点的插入和删除
- 面试系列之二:C艹翻转双向链表
- HOOK Engine (nthookengine.mhook)
- PHP调试跟踪之XDebug使用总结
- JS截取字符串substr和substring方法的区别
- Atitit .h5文件上传
- 将链表链接并且对链表排序(通过插入和删除函数实现
- 一个很好的仿微博消息浏览功能的Demo
- 求Sn=a+aa+aaa+aaaa+...+aaaaaa...a的值
- 在Java的JDBC使用中设置事务回滚的保存点的方法
- C#反射实用技术
- android------关于API 23的权限问题
- Android 开发之集成百度地图的定位与地图展示
- uva10943 How do you add?
- Leetcode-1.Two Sum