填空题 以下程序的功能是:先产生一条带头结点(链表的第一个结点不存储数据,而是存储链表的表长,即结点个数)的无序链表,每一个结点包含一个整数。然后将该链表分成两条带头结点的链表:_条链表上的数据均为偶数,另一条链表上的数据均为奇数。函数Cleate1创建了一条带有头结点的单链表。函数Print2输出链表上各结点的值。函数Split3把链表分割成两条链表,值为奇数的结点保留在原链表上,值为偶数的结点移到另一个链表中,并将指向偶数链表的头指针返回。 [程序](4分) #include struct Node{ int data; struct Node *next; }; Node *Create(void) //创建一条带有头结点的单向链表 { Node *p1,*head; int a; //创建头结点,头结点的数据域储存链表的结点个数 head=new Node; head->data=0;head->next=0: cout<<"创建一条无序链表,请输人数据,以-l结束,/n"; cin>>a; while(a!=-1){ p1=new Node; pl->data=a;pl->next=head->next; 4(26) 5; head->data++; cin>>a: } 6(27) 7; } void Print(Node *h) { h=h->next; while(h){eoutnext;} cout< } Node *Split(Node *&link) //link是-个带头结点的单链表 {Node *pl=link,*p2=link->next,*head; head=new Node; head->data=0;head->next=0; while(p2){ if(p2->data%2==O){ p1->next=p2->next;link->data--; p2->next=head->next; 8(28) 9 ; head->data++; p2=p1->next; } else{pl=p2; 10(29) 11;} } return(head); } void main(void) { Node *h1,*h2; hl=Create12; cout<<"输入的链表为:"< Print(h1); h2=Split(h1); cout<<"分割后的奇数链表为:"< Print(h1); cout<<"分割后的偶数链表为:"< Print(h2); }
【正确答案】
【答案解析】