您的位置:首页 > 其它

链表一大堆

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: