鱼C论坛

 找回密码
 立即注册
查看: 1981|回复: 2

[已解决]有关于链表的问题

[复制链接]
发表于 2019-4-27 21:56:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
题目大意是处理两个链表取交集;
我遇到的问题主要在于主函数的while循环,不知道为什么不正常跳出
输入样例:
1 2 5 -1
2 4 5 8 10 -1
输出样例:
2 5
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct M{
  4.         int num;
  5.         struct M *next;
  6. }M ;
  7. M *head,*tail;
  8. M *node()
  9. {
  10.         head=tail=NULL;
  11.         M*p=head;
  12.         p=(M*)malloc(sizeof(M));
  13.         scanf("%d",&p->num);
  14.         while(p->num>0)
  15.         {
  16.                 if(head==NULL)
  17.                         head=p;
  18.                 else
  19.                 tail->next=p;
  20.                 tail=p;
  21.                 tail->next==NULL;
  22.                 p=(M*)malloc(sizeof(M));
  23.                 scanf("%d",&p->num);
  24.                
  25.         }
  26.         return head;
  27. }

  28. int main()
  29. {       
  30.         M *p,*q,*s3;
  31.         p=node();
  32.         q=node();
  33.         head=tail=s3=NULL;
  34.         int sum=0;
  35.        
  36.         while((p!=NULL)&&(q!=NULL))//就是这个位置,不知为什么最后链表结束后不正常跳出还会打印出地址。
  37.         {
  38.                 if((p->num)<(q->num))
  39.                 {
  40.                         p=p->next;
  41.                 }
  42.                 else if(p->num>q->num)
  43.                 {
  44.                         q=q->next;
  45.                 }
  46.                 else if(p->num==q->num)
  47.                 {
  48.                 s3=(M*)malloc(sizeof(M));
  49.                 s3->num=q->num;
  50.                 if(head==NULL)
  51.                         head=s3;
  52.                         else
  53.                         tail->next=s3;
  54.                         tail=s3;
  55.                         s3->next=NULL;
  56.                         p=p->next;
  57.                         q=q->next;
  58.                         sum++;
  59.                 }
  60.         }
  61.         printf("%d",head->num);
  62.         head=head->next;
  63.         for(;head!=NULL;head=head->next)
  64.         printf(" %d",head->num);
  65. }
复制代码

我的输出
最佳答案
2019-4-28 10:22:28
本帖最后由 jackz007 于 2019-4-28 10:24 编辑

    代码过于复杂,简单修改了一下,请楼主参考
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. typedef struct M{
  4.         int num         ;
  5.         struct M * next ;
  6. } node                  ;

  7. node * link()
  8. {
  9.         node * p1 , * p2 , * head                                                ;
  10.         int num                                                                  ;

  11.         head = NULL                                                              ;
  12.         for(;;) {
  13.                 scanf("%d" , & num)                                              ;
  14.                 if(num >= 0) {
  15.                         if((p2 = (node *) malloc(sizeof(node))) != NULL) {
  16.                                 p2 -> num = num                                  ;
  17.                                 p2 -> next = NULL                                ;
  18.                                 if(head == NULL) head = p2                       ;
  19.                                 else p1 -> next = p2                             ;
  20.                                 p1 = p2                                          ;
  21.                         } else {
  22.                                 fprintf(stderr , "error: failure of malloc()\n") ;
  23.                                 head = NULL                                      ;
  24.                                 break                                            ;
  25.                         }
  26.                 } else {
  27.                         break                                                    ;
  28.                 }
  29.         }
  30.         return head                                                              ;
  31. }

  32. int main()
  33. {        
  34.         node * ph , * p , * qh , * q                                                     ;
  35.         int c                                                                            ;
  36.         ph = link()                                                                      ;
  37.         qh = link()                                                                      ;
  38.         c = 0                                                                            ;
  39.         for(p = ph ; p != NULL ; p = p -> next) {
  40.                 for(q = qh ; q != NULL ; q = q -> next) {
  41.                         if (p -> num == q -> num) {
  42.                             if(c) printf(" ")                                            ;
  43.                             printf("%d" , p -> num)                                      ;
  44.                             c ++                                                         ;
  45.                         }
  46.                 }
  47.         }
  48.         if(! c) printf("no match data found!\n")                                         ;
  49.         free(ph)                                                                         ;
  50.         free(qh)                                                                         ;
  51. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-4-27 22:01:54 | 显示全部楼层
2 5 加上三个地址12916832 12910928 12934896
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-4-28 10:22:28 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2019-4-28 10:24 编辑

    代码过于复杂,简单修改了一下,请楼主参考
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. typedef struct M{
  4.         int num         ;
  5.         struct M * next ;
  6. } node                  ;

  7. node * link()
  8. {
  9.         node * p1 , * p2 , * head                                                ;
  10.         int num                                                                  ;

  11.         head = NULL                                                              ;
  12.         for(;;) {
  13.                 scanf("%d" , & num)                                              ;
  14.                 if(num >= 0) {
  15.                         if((p2 = (node *) malloc(sizeof(node))) != NULL) {
  16.                                 p2 -> num = num                                  ;
  17.                                 p2 -> next = NULL                                ;
  18.                                 if(head == NULL) head = p2                       ;
  19.                                 else p1 -> next = p2                             ;
  20.                                 p1 = p2                                          ;
  21.                         } else {
  22.                                 fprintf(stderr , "error: failure of malloc()\n") ;
  23.                                 head = NULL                                      ;
  24.                                 break                                            ;
  25.                         }
  26.                 } else {
  27.                         break                                                    ;
  28.                 }
  29.         }
  30.         return head                                                              ;
  31. }

  32. int main()
  33. {        
  34.         node * ph , * p , * qh , * q                                                     ;
  35.         int c                                                                            ;
  36.         ph = link()                                                                      ;
  37.         qh = link()                                                                      ;
  38.         c = 0                                                                            ;
  39.         for(p = ph ; p != NULL ; p = p -> next) {
  40.                 for(q = qh ; q != NULL ; q = q -> next) {
  41.                         if (p -> num == q -> num) {
  42.                             if(c) printf(" ")                                            ;
  43.                             printf("%d" , p -> num)                                      ;
  44.                             c ++                                                         ;
  45.                         }
  46.                 }
  47.         }
  48.         if(! c) printf("no match data found!\n")                                         ;
  49.         free(ph)                                                                         ;
  50.         free(qh)                                                                         ;
  51. }
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
一点都不鱼的鱼 + 5 + 5 + 3

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-2 04:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表