Linked List 5 of the data structure experiment: splitting a single-chain table and single-chain Data Structure

Source: Internet
Author: User

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;}


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.