我写了一个,你参考参考!
#include<stdio.h>
#include<stdlib.h>
typedef
struct
node
{
int data;
struct node
*next;
}linknode;
typedef
linknode *linklist;
/*头插法建表*/
linklist creat1()
{
linklist head,s;
int x;
head->next=NULL;
printf("Please input a list and end by 0!\n");
scanf("%d",&x);
while(x)
{
s=(linklist)malloc(sizeof(linknode));
s->data=x;
s->next=head->next;
head->next=s;
scanf("%d",&x);
}
return head;
}
/*尾插法建表*/
linklist creat2 ()
{
linklist head,r,s;
int x;
head->next=NULL;
r=head;
printf("Please int a list and end by 0!\n");
scanf("%d",&x);
while(x)
{
s=(linklist)malloc(sizeof(linknode));
s->data=x;
r->next=s;
r=s;
scanf("%d",&x);
}
r->next=NULL;
return
head;
}
/*打印带头结点单链表*/
void print (linklist head)
{
linklist p;
p=head->next;
while(p)
{
printf("%4d",p->data);
p=p->next;
}
printf("\n");
}
void separation(linklist head)
{
linklist head1,head2,p,r,s;
head1->next=NULL;
head2->next=NULL;
r=head1;
s=head2;
p=head->next;
while(p)
{
if(p->data%2!=0)
{
r->next=p;
r=p;
p=p->next;
}
else
{
s->next=p;
s=p;
p=p->next;
}
}
r->next=NULL;
s->next=NULL;
print(head1);
print(head2);
}
/*主函数*/
main()
{
linklist head;
head=creat2();
print(head);
separation(head);
getch();
}