• 数据结构实验之链表五:单链表的拆分(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;
    }
    
    
    
  • 相关阅读:
    union
    大端和小端
    迭代器
    STL
    动态内存管理2
    动态内存管理
    关于 FlexBox
    CSS的居中问题
    操作符
    JavaScript介绍
  • 原文地址:https://www.cnblogs.com/lcchy/p/10139498.html
Copyright © 2020-2023  润新知