链表一大堆
2016-04-16 17:09
316 查看
#include <iostream> #include <stdio.h> #include <stdlib.h> using namespace std; struct node { int date; node *next; }; node *ni(node *head)///逆序 { node *pre,*temp,*l,*now; l=head->next; pre=(node*)malloc(sizeof(node)); pre->next=NULL; int i=0; while(l!=NULL) { now=l->next; temp=l; temp->next=pre; pre=temp; if(i==0) temp->next=NULL; l=now; i++; } head->next=pre; return head; } node *insert_(int k,node *head)///插入 { node * temp=(node *)malloc(sizeof(node)); temp->date=k; temp->next=NULL; node *last=head,*it; int flag=0; for(it=head->next;it!=NULL;it=it->next ) { if(temp->date<=it->date) { flag=1; last->next=temp; temp->next=it; break; } else last=it; } if(flag==0) { last->next=temp; } return head; } node *He(node *head1,node*head2)///合并 { node*head; for(node *it=head1->next;it!=NULL;it=it->next) head=insert_(it->date,head2); return head; } void shou(node *head)///显示 { node *l; l=head->next; while(l!=NULL) { cout<<l->date<<" "; l=l->next; } cout<<endl; } void fen(node *head,node *head1,node*head2)///分奇数偶数 { node *h=head->next; node *h1,*h2; h1=head1; h2=head2; for(;h!=NULL;h=h->next) { if(h->date&1) { h1->next=h; h1=h; } else { h2->next=h; h2=h; } } h1->next=NULL; h2->next=NULL; } int main() { int n,k; node *head,*l,*p; while(~scanf("%d",&n)) { head=(node*)malloc(sizeof(node)); node *head0=(node*)malloc(sizeof(node)); for(int i=0; i<n; i++) { p=(node*)malloc(sizeof(node)); cin>>p->date; p->next=NULL; if(i==0) head->next=l=p; else { l->next=p; l=p; } } node* head1=new(node); node* head2=new(node); fen(head,head1,head2); shou(head1); shou(head2); } return 0; }
相关文章推荐
- no grammar constraints (DTD or XML schema) 解决方法
- 用树状数组解决敌兵布阵
- 在Web/Phpstorm中设置连接FTP(附带:文件比较,上传下载,同步等)
- 安卓事件处理机制
- 产品经理FIRST:总结《小米参与感》
- OpenCV中IplImage与Mat代码风格比较
- CreateProcess的用法
- 加密之SHA,MD5
- ShellSort
- 求二叉树的叶节点
- 第四次_Activity传递参数
- 基于Flume的美团日志收集系统(二)改进和优化
- leetcode——40——Combination Sum II
- 网络流二十四题之三 —— 最小路径覆盖问题(PATH)
- linux 下手动安装 gcc
- 基于Flume的美团日志收集系统(一)架构和设计
- TCP/IP(3)
- HDU 2553 N皇后问题(回溯 + 剪枝)
- 序列化二叉树
- 适配器模式的分析、结构图及基本代码