开发者社区> 问答> 正文

数据结构与算法,有序链表,求解答!

数据结构与算法,有序链表,求解答!

展开
收起
知与谁同 2018-07-18 14:28:45 1288 0
1 条回答
写回答
取消 提交回答
  • 静静的看着你们
    #include <cstdio>
    struct node{
    int val;
    node *next;
    node(){val=0;next=NULL;}
    };
    node *head1,*head2,*t1,*t2;
    int main(){
    int i;
    t1 = head1;
    t2 = head2;
    while (true){
    scanf("%d",&i);
    if(i==-1){
    break;
    }
    node *temp = new node();
    temp->val = i;
    temp->next = NULL;
    t1->next = temp;
    t1 = t1->next;
    }
    while(true){
    scanf("%d",&i);
    if(i==-1){
    break;
    }
    node *temp = new node();
    temp->val = i;
    temp->next = NULL;
    t2->next = temp;
    t2 = t2->next;
    }
    t1 = head1->next;
    t2 = head2->next;
    while(true){
    if(t1==NULL || t2==NULL){
    break;
    }
    if(t1->val>t2->val){
    t2 = t2->next;
    }else if(t1->val < t2->val){
    t1 = t1->next;
    }else{
    printf("%d ",t1->val);
    t1 = t1->next;
    t2 = t2->next;
    }
    }
    printf("\n");
    return 0;
    }



    真佩服楼主,没花一点财富值就敢提这种问题

    2019-07-17 22:53:32
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
“大数据+算法”助力B2B未来商业 立即下载
数据+算法定义新世界 立即下载
Apache Flink 流式应用中状态的数据结构定义升级 立即下载