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


     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 struct node
     5 {
     6     int data;
     7     struct node*next;
     8 };
     9 int main()
    10 {
    11     int n,i,j=0,o=0;//o、j分别用来统计偶数和奇数的个数。
    12     scanf("%d",&n);//题目比较简单,但是很繁琐,代码基本是重复的。
    13     struct node*head,*end,*p,*head1,*head2,*end1,*end2;//这里没必要这么繁琐,可以自行简化。
    14     head=(struct node*)malloc(sizeof(struct node));
    15     head->next=NULL;
    16     end=head;
    17     for(i=0; i<n; i++)
    18     {
    19         p=(struct node*)malloc(sizeof(struct node));
    20         scanf("%d",&p->data);
    21         p->next=NULL;
    22         end->next=p;
    23         end=p;
    24     }
    25     head1=(struct node*)malloc(sizeof(struct node));
    26     head1->next=NULL;
    27     end1=head1;
    28     head2=(struct node*)malloc(sizeof(struct node));
    29     head2->next=NULL;
    30     end2=head2;
    31     for(p=head->next; p; p=p->next)
    32     {
    33         if(p->data%2==0)//对数据进行判断。
    34         {
    35             end1->next=p;
    36             end1=p;
    37             o++;
    38         }
    39         else
    40         {
    41             end2->next=p;
    42             end2=p;
    43             j++;
    44         }
    45     }
    46     end1->next=NULL;end2->next=NULL;//保证链表有结尾,否则会输出奇奇怪怪的东西。
    47     printf("%d %d
    ",o,j);//这个地方别漏掉,同时还要注意输出的顺讯。
    48     for(p=head1->next; p; p=p->next)
    49     {
    50 
    51         printf("%d",p->data);
    52         if(p->next!=NULL)printf(" ");
    53     }
    54     printf("
    ");
    55     for(p=head2->next; p; p=p->next)
    56     {
    57 
    58         printf("%d",p->data);
    59         if(p->next!=NULL)printf(" ");
    60     }
    61     return 0;
    62 }
  • 相关阅读:
    NAT(NAPT)地址转换过程
    关于路由、AP、交换机的小总结
    交换机、集线器、路由器区别和作用
    系统调用与API的区别
    课程设计
    Python学习之format函数的使用
    等边三角形
    Hello 2018
    PyCharm idea clion webstorm phpstorm激活
    Educational Codeforces Round 35 (Rated for Div. 2)(ABC)
  • 原文地址:https://www.cnblogs.com/Angfe/p/10449404.html
Copyright © 2020-2023  润新知