• 数据结构实验之链表五:单链表的拆分(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;
    }
    
    
    
  • 相关阅读:
    Vue3 从入门到入土(TypeScript入门)
    go select 原理解析
    Golang 内存分配
    Vue3 从入门到入土(TypeScript数据类型)
    Vue3 从入门到入土(TypeScript接口)
    总结一下 linux 中的线程模型
    Golang Channel 实现原理
    身份证号码缩小方案
    sync.waitGroup 原理分析
    go 语言的 slice 注意点
  • 原文地址:https://www.cnblogs.com/lcchy/p/10139499.html
Copyright © 2020-2023  润新知