【PTA】6-4 奇数值结点链表
生活随笔
收集整理的這篇文章主要介紹了
【PTA】6-4 奇数值结点链表
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題樣
?
struct ListNode *readlist() {struct ListNode * head=NULL;struct ListNode * p=NULL;struct ListNode * p2=NULL;while(1){p=(struct ListNode *)malloc(sizeof(struct ListNode ));scanf("%d",&p->data);if(p->data==-1)break;else{if(head==NULL){head=p;p2=p;}else{p2->next=p;p2=p;}}p2->next=NULL;}return head; } int isjishu(int a) {return a%2; } int * jishu(struct ListNode * head) {struct ListNode *p=head;static int arr[5000]={0};int i=0;while(p){if(isjishu(p->data )>0){arr[i++]=p->data ;}p=p->next ;} return arr; } int *shenyu(struct ListNode *head) {static int arr2[5000]={0};int j=0;struct ListNode *p=head;while(p){if(isjishu(p->data)<=0){arr2[j++]=p->data; }p=p->next ;}return arr2; } struct ListNode *getodd( struct ListNode **L ) {struct ListNode * head1=NULL;struct ListNode *p=NULL;struct ListNode *p2=NULL;struct ListNode * head2=NULL;struct ListNode *p3=NULL;struct ListNode *p4=NULL;int * arr=jishu( *L);//存放奇數(shù)的數(shù)組 int i=0;//訪問奇數(shù)數(shù)組 int *arr2=shenyu(*L);int j=0;while(arr[i]!=0){p=(struct ListNode *)malloc(sizeof(struct ListNode ));p->data =arr[i++];if(head1==NULL){head1=p;p2=p;}else{p2->next =p;p2=p;}}if(arr[0]!=0)p2->next =NULL;//奇數(shù)鏈表建立結(jié)束while(arr2[j]!=0){p3=(struct ListNode *)malloc(sizeof(struct ListNode ));p3->data =arr2[j++];if(head2==NULL){head2=p3;p4=p3;}else{p4->next =p3;p4=p3;}}if(arr2[0]!=0)p4->next =NULL;//第二條建立完畢f(xié)ree(*L);*L=head2;return head1; } //總結(jié),先用簡單思路,兩個數(shù)組來做,別想太遠(yuǎn)把自己繞暈總結(jié)
以上是生活随笔為你收集整理的【PTA】6-4 奇数值结点链表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 习题11-7 奇数值结点链表 (20分)
- 下一篇: 人工智能的必须的数学基础和编程基础