标题:谁能帮我解决下链表插入运算的问题啊,谢谢
取消只看楼主
yangyuluke
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-6-19
 问题点数:0 回复次数:0 
谁能帮我解决下链表插入运算的问题啊,谢谢
构造两个链表LA,LB,把存在于LB的数据元素而不存在于LA的插入到LA中
我两个表的 构造好了,但是插入的时候就出问题了,谁能 帮我解决 下啊
以下是源码:
//归并2个链表LA,LB,将存在于LB中而不存在于LA中的数据元素插入到LA中去
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct LinkList
{
       int data;
       struct LinkList *next;
       };
       struct LinkList *head_a=NULL;
       struct LinkList *head_b=NULL;
       struct LinkList *la;
       struct LinkList *lb;
       struct LinkList *pa;
       struct LinkList *pb;
       struct LinkList *e;
       struct LinkList *p;
       struct LinkList *q;
main()
{
      int i;
      int j;
      //建立第一个链表
      printf("请输入第一个链表的5个数字:\n");
      pa=(struct LinkList *)malloc(sizeof(struct LinkList));
      for(i=5;i>0;i--)
      {
                      la=(struct LinkList *)malloc(sizeof(struct LinkList));
                      scanf("%d",&la->data);
                      if(head_a==NULL)
                      {  
                                  head_a=la;
                                  }
                                  else
                                  {
                                      pa->next=la;
                                      }
                                      pa=la;
                      }
                      //输出第一个链表
                      pa->next=NULL;
                      la=head_a;
                      while(la!=NULL)
                      {
                                     printf("%d\t",la->data);
                                     la=la->next;
                                     }
                                     printf("第一个链表输入完成!\n");
               
      //建立第二个链表   
      printf("请输入第二个链表的5个数字:\n");
      pb=(struct LinkList *)malloc(sizeof(struct LinkList));
      for(j=5;j>0;j--)
      {
                      lb=(struct LinkList *)malloc(sizeof(struct LinkList));
                      scanf("%d",&lb->data);
                      if(head_b==NULL)
                      {
                            head_b=lb;        
                                    }
                                    else
                                    {
                                        pb->next=lb;
                                        }
                                        pb=lb;
                      }
                      //输出第二个链表
                      pb->next=NULL;
                      lb=head_b;
                      while(lb!=NULL)
                      {
                                     printf("%d\t",lb->data);
                                     lb=lb->next;
                                     }
                                     printf("第二个链表输入完成!\n");
      //归并两个链表
      q=head_a;
      e=lb->data;
      if(head_a==NULL)
                      {
                      head_a=e;
                      e->next=NULL;
                      }
                      else if(p==0)
                      {
                      e->next=head_a;
                      e=head_a;
                      }
                      else
                      {
                         while(e!=la->data&&q!=NULL)
                         {
                         e->next=q->next;
                         q->next=e;
                         }
                         la=la->next;
                          
                      }            
                      while(la!=NULL)
                      {
                                     printf("%d\t",la->data);
                                     la=la->next;
                                     }
                                     printf("已经将两个链表归并.\n");
                       system("pause");
}
搜索更多相关主题的帖子: 链表 LinkList struct 运算 include 
2008-06-19 01:01



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-219934-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.429759 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved