The chain list of data structure Experiment five: Splitting of single linked list

Source: Internet
Author: User

The chain list of data structure Experiment five: Splitting of single linked list Time limit:1000ms Memory limit:65536k have questions? Dot here ^_^ Title Description Enter n integer order to create a single linked list, splitting the single list into two sub-linked lists, the first sub-list holds all the even numbers, and the second sub-list holds all the odd number. The relative order of the data in the two sub-lists is consistent with the original list. Enter the first line input integer n;;
The second line enters n integers in turn. Output the first line of the number of even linked list and odd list of elements;
The second line outputs all the data of the even sub-list in turn;
The third line prints all the data of the odd-numbered sub-list in turn. Sample input
101 3 22 8 15 999 9 44 6 1001
Sample output
4 622 8 44 6 1 3 15 999 9 1001
Tip do not use arrays!
#include <stdio.h> #include <stdlib.h>typedef struct node{int data; struct node *next;} Node;    node *creatlist (int n)//reverse-build table, List initialization {Node *head,*p;    int i;    Head= (node *) malloc (sizeof (node));    head->next=null;        for (i=0;i<n;i++) {p= (node *) malloc (sizeof (node));        scanf ("%d", &p->data);        p->next=head->next;    head->next=p; } return head;    Node *split (node *head1)//split of the linked list {node *head2,*p,*q;    int cnt1=0,cnt2=0;    Head2= (node *) malloc (sizeof (node));    head2->next=null;    p=head1->next;    head1->next=null;    q=p->next;            while (p) {if (p->data%2==0) {p->next=head1->next;            head1->next=p;        cnt1++;            } else {p->next=head2->next;            head2->next=p;        cnt2++;        } p=q;    if (q!=null) q=q->next;    } printf ("%d%d\n", cnt1,cnt2); return head2;} VoiD Print (node *head)//traverse the linked list {node *s;    s=head->next;        while (s) {if (s->next==null) printf ("%d\n", s->data);            else printf ("%d", s->data);    s=s->next;    }}void Main () {int n;    Node *head1,*head2,*s;    scanf ("%d", &n);    Head1=creatlist (n);    Head2=split (HEAD1);    Print (HEAD1); Print (head2);}


The chain list of data structure Experiment five: Splitting of single linked list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.