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

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

TimeLimit: 1000MS Memory Limit: 65536KB

SubmitStatistic

ProblemDescription

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

Input

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

Output

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

ExampleInput

10

1 3 22 8 15 999 9 44 6 1001

ExampleOutput

4 6

22 8 44 6

1 3 15 999 9 1001

Hint

不得使用数组!

Author

E
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
//#include<dqeue>
using namespace std;

struct node
{

  int data;
  struct node*next;

};
struct  node*creat(struct node*head,int n)
{
      head= (struct node*)malloc(sizeof(struct node));
      head->next =NULL;

      struct node*tail = head;
      for(int i=1;i<=n;i++)
      {
          struct node*p = (struct node*)malloc(sizeof(struct node));
          p->next =NULL;
         scanf("%d",&p->data);
         tail->next = p;
         tail = p;

      }

      return head;



}
/*struct node*guibing(struct node*head1,struct node*head2)
{

    head1= head1->next;
    head2 =head2->next;
    struct node*tail,*p,*mail;
    tail = (struct node*)malloc(sizeof(struct node));
    mail =  tail;
    while(head1&&head2)
    {

        if(head1->data<head2->data)
        {
           mail->next = head1;
           mail = head1;
           p = head1;
           head1 = head1->next;
           p->next =NULL;
        }
        else
        {

          mail->next =head2;
          mail = head2;
          p = head2;
          head2 = head2->next;
          p->next =NULL;




        }
        }
        if(head1)
        {

            while(head1)
            {
                mail->next =head1;
                mail = head1;
                p = head1;
                head1 = head1->next;
                p->next = NULL;

            }
        }
        else if(head2)
        {
          while(head2)
          {
             mail->next =head2;
             mail = head2;
             p = head2;
             head2 = head2->next;
             p->next =NULL;
          }

    }

return tail;

}*/
void show(struct node*head)
{
     struct node*tail;
     tail =head;
     int top = 1;
     while(tail->next)
     {

           if(top)top=0;
           else printf(" ");
           printf("%d",tail->next->data);
           tail = tail->next;

     }

}
int main()
{

        int n,n1=0,n2=0;
        scanf("%d",&n);
        struct node*head1= (struct node*)malloc(sizeof(struct node));
        struct node*head2 = (struct node*)malloc(sizeof(struct node));
        struct node*head3 = (struct node*)malloc(sizeof(struct node));

        head1 = creat(head1,n);
         
        head1 =head1->next;
        head3->next = NULL;
        head2->next = NULL;
      struct node*tail1= head2;
        struct node*tail2=head3;

        while(head1)
        {
             if(head1->data%2==0)
             {
               tail1->next = head1;

              struct node*p;
               p  = head1;
                head1 = head1->next;
                p->next = NULL;
               tail1 = p;
               n1++;

             }
            else
            {
                tail2->next = head1;

                   struct node*p;
                p = head1;
                head1 = head1->next;
                p ->next =NULL;

                tail2 = p;
                n2++;
            }
       }
         cout<<n1<<" "<<n2<<endl;
         show(head2);
         cout<<endl;
         show(head3);
         cout<<endl;
         return 0;
        }







/***************************************************
User name: jk160505徐红博
Result: Accepted
Take time: 0ms
Take Memory: 148KB
Submit time: 2017-01-14 17:08:08
****************************************************/

全部评论

相关推荐

12-08 16:04
门头沟学院 Java
本人本科末9,今年大三。大一大二一直玩,什么都没学到,在大学混日子混了两年,每天不是在打农就是在steam。大三开学时一个和自己玩的好的同学去实习了,才发现自己白白浪费了两年的时间,如果真不冲一下就真去京东,阿里,美团送外卖了今年9月份开始学Java,一开始一直跟着黑马视频看,后面发现看视频效率太低了,时间根本不够,就开始主要看文档和看书了。这几个月一直在学,真的尽力了,希望暑期前能找一份好点的实习。我简历上面的项目大多没有指标,但是实际上我是真没多少时间去做项目,我基本主要是动手只做了外卖和天机,黑马点评和12306我都是只是看了项目。主要是自己的时间真的不多,但是这样子自己的代码能力确实比较差。而且自己也没有做过实际的工程,我顶多用jmeter测试一下接口tps啥的,比如使用Redis管道提升了一点性能,减少Redis交互,这种值得写上去吗?需不需要具体到某些数字求求各位佬给一些建议,看看简历怎么优化?项目介绍是不是不够详细?没有具体到业务方面。项目会不会提到大致实现原理导致面试官一看简历就知道怎么实现就没有问的欲望?专业技能一些字段是不是要加粗,是不是写太啰嗦了?有没有必要压缩内容变成一页?两页的话是不是都要把两页填地满满的。
给秋招一个交代:一页简历最好,网上做的项目放面试官眼里都是玩具,简历上不需要强调有什么难点,记住就行防止真的问。然后背八股,多投多面试就行
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务