7-53 兩個有序序列的中位數
阿新 • • 發佈:2019-02-03
有序 type bre 兩個 stdlib.h main c++ bsp next
錯誤:
#include <stdio.h> #include <stdlib.h> typedef struct note { int data; struct note *next; }note; note* CreatList(int n) { note *head,*q,*p; head=(note*)malloc(sizeof(note)); head->next=NULL; q=head; int a; while(n--) { scanf("%d",&a); p=(note*)malloc(sizeof(note)); p->data=a; p->next=NULL; q->next=p; q=p; } return head; } int c=0; note *Different(note *head1,note *head2) { head1=head1->next;head2=head2->next; note *head,*q,*p; head=(note*)malloc(sizeof(note)); head->next=NULL; q=head; while(head1!=NULL&&head2!=NULL) { if(head1->data<head2->data) { c++; //printf("%d\n",head1->data); p=(note*)malloc(sizeof(note)); p->data=head1->data; p->next=NULL; q->next=p; q=p; head1=head1->next; } else if(head1->data>head2->data) { //printf("%d\n",head2->data); c++; p=(note*)malloc(sizeof(note)); p->data=head2->data; p->next=NULL; q->next=p; q=p; head2=head2->next; } else { c++; p=(note*)malloc(sizeof(note)); p->data=head1->data; p->next=NULL; q->next=p; q=p; //printf("%d\n",head1->data); head1=head1->next; head2=head2->next; } } if(head1==NULL) { while(head2!=NULL) { c++; q->next=head2; q=head2; head2=head2->next; } } if(head2==NULL) { q->next=head1; while(head1!=NULL) { c++; q->next=head1; q=head1; head1=head1->next; } } return head; } void Print(note *head) { head=head->next; c=c/2; int k=0; while(1) { k++; if(k==c) { printf("%d\n",head->data); break; } head=head->next; } } int main() { int n; scanf("%d",&n); note *p1=CreatList(n); note *p2=CreatList(n); note *p=Different(p1,p2); Print(p); }
7-53 兩個有序序列的中位數