• 单链表的拆分


    数据结构实验之链表五:单链表的拆分

    Time Limit: 1000MS Memory limit: 65536K

    题目描述

    输入N个整数顺序建立一个单链表,将该单链表拆分成两个子链表,第一个子链表存放了所有的偶数,第二个子链表存放了所有的奇数。两个子链表中数据的相对次序与原链表一致。

    输入

    第一行输入整数N;;
    第二行依次输入N个整数。

    输出

    第一行分别输出偶数链表与奇数链表的元素个数; 
    第二行依次输出偶数子链表的所有数据;
    第三行依次输出奇数子链表的所有数据。

    示例输入

    10
    1 3 22 8 15 999 9 44 6 1001

    示例输出

    4 6
    22 8 44 6 
    1 3 15 999 9 1001
     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 struct vode
     4 {
     5     int date;
     6     struct vode *next;
     7 };
     8 int main()
     9 {
    10     struct vode *head,*head1,*head2,*tail,*tail1,*tail2,*p,*q;
    11     head=(struct vode *)malloc(sizeof(struct vode ));
    12     head->next=NULL;
    13     tail=head;
    14     int i,n;
    15     scanf("%d",&n);
    16     for(i=1;i<=n;i++)
    17     {
    18         p=(struct vode *)malloc(sizeof(struct vode ));
    19         p->next=NULL;
    20         scanf("%d",&p->date);
    21         tail->next=p;
    22         tail=p;
    23     }
    24     head1=(struct vode *)malloc(sizeof(struct vode ));
    25     head1->next=NULL;
    26     tail1=head1;
    27     head2=(struct vode *)malloc(sizeof(struct vode ));
    28     head2->next=NULL;
    29     tail2=head2;
    30     p=head->next;
    31     int sum1=0,sum2=0;
    32     while(p)
    33     {
    34         if(p->date%2==0)
    35         {
    36             tail1->next=p;
    37             tail1=p;
    38             sum1++;
    39         }
    40         if(p->date%2!=0)
    41         {
    42             tail2->next=p;
    43             tail2=p;
    44             sum2++;
    45         }
    46         p=p->next;
    47     }
    48     printf("%d %d
    ",sum1,sum2);
    49     p=head1->next;
    50     int s=0,j=0;
    51     while(p)
    52     {
    53         if(j>=sum1)break;
    54         if(s==0)
    55         {
    56             printf("%d",p->date);
    57             s=1;
    58         }
    59         else
    60         {
    61             printf(" %d",p->date);
    62         }
    63         p=p->next;
    64         j++;
    65     }
    66     printf("
    ");
    67     p=head2->next;
    68     s=0;
    69     j=0;
    70     while(p)
    71     {
    72         if(j>=sum2)break;
    73         if(s==0)
    74         {
    75             printf("%d",p->date);
    76             s=1;
    77         }
    78         else
    79         {
    80             printf(" %d",p->date);
    81         }
    82         p=p->next;
    83         j++;
    84     }
    85     printf("
    ");
    86     return 0;
    87 }
    View Code
  • 相关阅读:
    数组字典
    字符串创建
    不可变数组
    数组
    纯净控制台
    可变字典
    数组Array
    id NSObject instanceType 区别
    可变字符串 插入,删除,替换,赋值
    查询字符串中某部分的字符串
  • 原文地址:https://www.cnblogs.com/kuangdaoyizhimei/p/3264609.html
Copyright © 2020-2023  润新知