数据结构实验之链表五:单链表的拆分

来源:互联网 发布:淘宝哪家手机店靠谱 编辑:程序博客网 时间:2024/04/29 22:37

 ~ ~ ~ ~ ~ ~ ~ ~ 题目

代码:

#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>using namespace std;struct node{int data;struct node *next;};int main(){    int n, i, cnt;    struct node *head, *head2, *tail, *tail2, *p, *q;    head = (struct node *)malloc(sizeof(struct node));    head->next = NULL;    tail = head;    while(~scanf("%d", &n)){cnt = 0;for(i=0; i<n; i++){p = new node;            p->next = NULL;            scanf("%d", &p->data);            tail->next = p;            tail = p;}head2 = new node;head2->next = NULL;tail2 = head2;q = head;p = q->next;while(p!=NULL){if(p->data%2==0){tail2->next = p;tail2 = p;q->next = p->next;tail2->next = NULL;p = q->next;cnt++;}else {q = q->next;p = q->next;}}printf("%d %d\n", cnt, n-cnt);head2 = head2->next;while(head2!=NULL){if(head2->next==NULL)printf("%d\n", head2->data);else printf("%d ", head2->data);head2 = head2->next;}head = head->next;while(head!=NULL){if(head->next==NULL)printf("%d\n", head->data);else printf("%d ", head->data);head = head->next;}}return 0;}

0 0
原创粉丝点击