7-5 两个有序链表序列的合并 (20 分)
生活随笔
收集整理的這篇文章主要介紹了
7-5 两个有序链表序列的合并 (20 分)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
已知兩個非降序鏈表序列S1與S2,設計函數構造出S1與S2合并后的新的非降序鏈表S3。
輸入格式:
輸入分兩行,分別在每行給出由若干個正整數構成的非降序序列,用?1表示序列的結尾(?1不屬于這個序列)。數字用空格間隔。
輸出格式:
在一行中輸出合并后新的非降序鏈表,數字間用空格分開,結尾不能有多余空格;若新鏈表為空,輸出NULL。
輸入樣例:
1 3 5 -1 2 4 6 8 10 -1輸出樣例:
1 2 3 4 5 6 8 10 #include<stdio.h> #include<string.h> #include<stdlib.h> typedef struct node{int data;struct node* next; }; typedef struct node* ptr;int main() {int n;ptr head=(ptr)malloc(sizeof(struct node));head->data=0;head->next=NULL;ptr temp,con;temp=head;con=head;while(scanf("%d",&n),n!=-1){ptr t=(ptr)malloc(sizeof(struct node));t->data=n;t->next=NULL;head->next=t;head=head->next;}while(scanf("%d",&n),n!=-1){while(temp->next!=NULL){if(temp->next->data>=n){ptr tmp=(ptr)malloc(sizeof(struct node));tmp->data=n;tmp->next=temp->next;temp->next=tmp;break;}else temp=temp->next;}if(temp->next==NULL){ptr x=(ptr)malloc(sizeof(struct node));x->data=n;x->next=NULL;temp->next=x;} }if(con->next==NULL) printf("NULL");else{con=con->next;while(con->next!=NULL){printf("%d ",con->data);con=con->next;}printf("%d",con->data);}return 0; }總結
以上是生活随笔為你收集整理的7-5 两个有序链表序列的合并 (20 分)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信如何撤回消息、撤销消息
- 下一篇: 如何修改dns设置