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


    #include <bits/stdc++.h>
    
    using namespace std;
    
    struct node
    {
        int data;
        struct node *next;
    };
    struct node *crea(int n)
    {
        int i;
        struct node *head, *p;
        head = (struct node *)malloc(sizeof(struct node));
        head -> next = NULL;
        for(i = 0; i < n; i ++)
        {
            p = (struct node *)malloc(sizeof(struct node));
            scanf("%d", &p -> data);
            p -> next = head -> next;
            head -> next = p;
        }
        return head;
    }; 
    struct node *spilt(struct node *head)
    {
        struct node *head1, *p, *q;
        int a = 0, b = 0; 
        head1 = (struct node *)malloc(sizeof(struct node)); 
        head1 -> next = NULL;
        p = head -> next;
        head -> next = NULL;
        q = p -> next;
        while(p)
        {
            if(p -> data % 2 == 0)
            {
                p -> next = head -> next;
                head -> next = p;
                a++;
            }
            else
            {
                p -> next = head1 -> next;
                head1 -> next = p;
                b ++;
            }
            p = q;
            if(q)
                q = q -> next;
        }
        printf("%d %d
    ",a,b);
        return head1;
    };
    int main()
    {
    
        int n;
        struct node *head, *p, *q, *head1;
        scanf("%d", &n);
        head = crea(n);
        head1 = spilt(head);
        p = head -> next;
        while(p!=NULL)
        {
            if(p -> next != NULL)printf("%d ", p -> data);
            else printf("%d
    ", p ->data);
            p = p -> next;
        }
        q = head1 -> next;
        while(q!=NULL)
        {
            if(q -> next != NULL)printf("%d ", q -> data);
            else printf("%d
    ",q -> data);
            q = q -> next;
        }
        return 0;
    }
    
    
    
  • 相关阅读:
    winform制作自定义控件(入门)
    VB2012读取xml
    VB生成xml
    通宵疯狂积累VB.NET基础知识
    【转】vs2010下创建webservice
    React Native 快速入门之认识Props和State
    mac下 mysql / nginx 问题总汇
    Oracle例外定义
    mac os x 把reids nignx mongodb做成随机启动吧
    Oracle 记录
  • 原文地址:https://www.cnblogs.com/lcchy/p/10139500.html
Copyright © 2020-2023  润新知