The chain list of data structure Experiment five: Splitting of single linked list Time limit:1000ms Memory limit:65536k 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! Source
#include <stdio.h> #include <stdlib.h>struct node {int data; struct node *next;}; struct node *creat (int n) {struct node *head, *tail, *p; int i; head= (struct node *) malloc (sizeof (struct node)); head->next=null; for (i=1;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 * Split (struct node * head1) {struct node *head2, *p, *q; int a=0, b=0; head2= (struct node *) malloc (sizeof (struct node)); head2->next=null; p=head1->next; head1->next=null; q=p->next; while (P!=null) {if (p->data%2==0) {p->next=head1->next; head1->next=p; a++; } else {p->next=head2->next; head2->next=p; b++; } p=q; if (q!=null) q=q->next; } printf ("%D%d\n ", A, b); return head2;}; int main () {int n; struct node * head1, *head, *p, *q; scanf ("%d", &n); Head=creat (n); Head1=split (head); p=head->next; while (P!=null) {if (p->next!=null) printf ("%d", p->data); else printf ("%d\n", p->data); p=p->next; } q=head1->next; while (Q!=null) {if (q->next!=null) printf ("%d", q->data); else printf ("%d\n", q->data); q=q->next; } return 0;}
The chain list of data structure Experiment five: Splitting of single linked list