Linked List 5 of the data structure experiment: splitting a single-chain table and single-chain Data Structure
Linked List of Data Structure Experiment 5: splitting a single-chain table Time Limit: 1000 MS Memory limit: 65536 K Enter N Integers to create a single-chain table. Split the single-chain table into two sublinked lists. The first sublinked list stores all the even numbers, and the second sublinked list stores all the odd numbers. The data in the two sublinked lists is in the same order as that in the original linked list. Enter an integer N in the first line ;;
Enter N integers in the second row. The first line outputs the number of elements in the even and odd linked lists respectively;
The second row outputs all data of the even sublinked list in sequence;
The third row outputs all data of the odd sub-linked list in sequence. 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
An array is not allowed! 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;}