链表F 单链表的拆分

来源:互联网 发布:java获取文件共享路径 编辑:程序博客网 时间:2024/05/18 18:19

Problem Description
输入N个整数顺序建立一个单链表,将该单链表拆分成两个子链表,第一个子链表存放了所有的偶数,第二个子链表存放了所有的奇数。两个子链表中数据的相对次序与原链表一致。
Input
第一行输入整数N;;
第二行依次输入N个整数。
Output
第一行分别输出偶数链表与奇数链表的元素个数;
第二行依次输出偶数子链表的所有数据;
第三行依次输出奇数子链表的所有数据。
Example Input
10
1 3 22 8 15 999 9 44 6 1001
Example Output
4 6
22 8 44 6
1 3 15 999 9 1001

注意拆分函数的返回只能返回一个值,可以在这个函数里面调用show函数,调用两次就可以显示拆分两个后的两个了

#include <stdio.h>#include <stdlib.h>struct node{    int data;    struct node *next;};struct node *creat(int t)       //按照题意来 顺序 建表{    int i;    struct node *head,*p,*tail;    head = (struct node*)malloc(sizeof(struct node));    head->next = NULL;    tail = head;    for(i=0; i<t; i++)    {        p = (struct node*)malloc(sizeof(struct node));        scanf("%d",&p->data);        p->next = NULL;        tail->next = p;                   //易错点。注意与逆序建表区别开.                                                  p->next = head->next ;                                                   head->next = p;         tail = p;    }    return head;};void show(struct node *head){    struct node *p;    p = head->next;    printf("%d",p->data);    p = p->next;    while(p!=NULL)    {        printf(" %d",p->data);        p = p->next;    }    printf("\n");}struct node *split(struct node *head1){    struct node *p,*q,*head2,*tail1,*tail2;    int num1=0,num2=0;    head2 = (struct node*)malloc(sizeof(struct node));    head2->next = NULL;    p = head1->next;    head1->next = NULL;               //顺序 插入!!    q = p->next;    tail1 = head1;    tail2 = head2;    while(p!=NULL)    {        if(p->data%2==0)        {            tail1->next = p;            p->next = NULL;            tail1 = p;            num1++;        }        else        {            tail2->next = p;            p->next = NULL;            tail2 = p;            num2++;        }        p = q;     //p后移        if(q!=NULL)         //此处不能写while。While是在里面循环,直到条件不满足才出来。而if是选择,条件满足,执行一次!        {            q = q->next;        }    }    printf("%d %d\n",num1,num2);return head2;        //虽然只返回了head2,但head1也已经改变了。函数只能返回一个值。如需返回两个值,用两个函数哦~}int main(){    int n;    struct node *head1,*head2;    scanf("%d",&n);    head1 = creat(n);        //此处head1还是最开始那个。head2 = split(head1);    //此处进行拆分函数。虽然拆分函数返回的是head2,但head1也已经改变!    show(head1);    //此处的head1已经改变!    show(head2);    return 0;}正确2:        //此方法与上一种区别是:拆分函数里。               定义拆分函数为无返回值,然后再其内部最后调用show函数#include <stdio.h>#include <stdlib.h>struct node{    int data;    struct node *next;};struct node *creat(int t){    int i;    struct node *head,*p,*tail;    head = (struct node*)malloc(sizeof(struct node));    head->next = NULL;    tail = head;    for(i=0; i<t; i++)    {        p = (struct node*)malloc(sizeof(struct node));        scanf("%d",&p->data);        p->next = NULL;        p->next = tail->next;        tail->next = p;        tail = p;    }    return head;};void split(struct node *head1){    struct node *p,*q,*head2,*tail1,*tail2;    int num1=0,num2=0;    head2 = (struct node*)malloc(sizeof(struct node));    head2->next = NULL;    p = head1->next;    head1->next = NULL;    q = p->next;    tail1 = head1;    tail2 = head2;    while(p)    {        if(p->data%2==0)        {            tail1->next = p;            p->next = NULL;          //插入为 顺序插入,不改变顺序            tail1 = p;            num1++;        }        else        {            tail2->next = p;            p->next = NULL;            tail2 = p;                               num2++;        }        p = q;        if(q)        {            q = q->next;        }    }    printf("%d %d",num1,num2);    show(head1);    show(head2);};void show(struct node *head){    struct node *p;    p = head->next;    printf("%d",p->data);    p = p->next;    while(p)    {        printf(" %d",p->data);        p = p->next;    }    printf("\n");};int main(){    int n;    struct node *head;    scanf("%d",&n);    head = creat(n);    split(head);    return 0;}