首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

各位朋友能不能进来帮忙看一下小弟我的这个程序哪里有有关问题

2013-11-18 
各位朋友能不能进来帮忙看一下我的这个程序哪里有问题?#includestdio.h#includestdlib.h#includemall

各位朋友能不能进来帮忙看一下我的这个程序哪里有问题?
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define NULL 0

typedef struct node
{
  int data;
  struct node *next;
}LNode,*LinkList;

LinkList create_LinkList()
{
  int x;
  LinkList L;
  LNode *r;
  L=(LinkList)malloc(sizeof(LNode));
  L->next=NULL;
  r=L;
  printf("input the value x:\n");
  scanf("%d",&x);
  while(x)
  {
    r->next=(LNode *)malloc(sizeof(LNode));
    r->next->data=x;
    r=r->next;
    scanf("%d",&x);
  }
  r->next=NULL;
  return L;
}

LinkList In_LinkList(LinkList A,LinkList B)
{
  LinkList C;
  LNode *p,*q,*m;
  C=(LinkList)malloc(sizeof(LNode));
  C->next=NULL;
  m=C;
  p=A->next;
  q=B->next;
  while(p&&q)
  {
    if(p->data==q->data)
    {
      m->next=p;
      m=p;
      m->next=NULL;
      p=p->next;
      q=q->next;
    }
    else
      q=q->next;

  }
  return C;
}

void Print_LinkList(LinkList p)
{
  LNode *q=p->next;
  while(q)
  {
    printf("%3d",q->data);
    q=q->next;
  }
  printf("\n");
}

void main()
{
  LinkList A,B,C;
  C->next=NULL;
  A=create_LinkList();
  B=create_LinkList();
  C=In_LinkList(A,B);
  printf("A: ");
  Print_LinkList(A);
  printf("\nB: ");
  Print_LinkList(B);
  printf("\nC: ");
  Print_LinkList(C);
  getch();
}

题目是:两个递增单链表A、B,取另一个单链表C,使其为A、B的交集,且仍保持递增。。

运行后,C总是显示不了,改了很多次也不行。。 这种问题,要自己学会设置断点,逐步调试,看哪里出了问题,这是程序员必须会的
[解决办法]


#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define NULL 0

typedef struct node
{
  int data;
  struct node *next;
}LNode,*LinkList;

LinkList create_LinkList()
{
  int x;
  LinkList L;
  LNode *r;
  L=(LinkList)malloc(sizeof(LNode));
  L->next=NULL;
  r=L;
  printf("input the value x:\n");
  scanf("%d",&x);
  while(x)
  {
    r->next=(LNode *)malloc(sizeof(LNode));
    r->next->data=x;
    r=r->next;
    scanf("%d",&x);
  }
  r->next=NULL;
  return L;
}

LinkList In_LinkList(LinkList A,LinkList B)
{
  LinkList C;
  LNode *p,*q,*m;
  C=(LinkList)malloc(sizeof(LNode));
  C->next=NULL;
  m=C;
  p=A->next;
  q=B->next;
  while(p&&q)
  {
    if(p->data==q->data)
    {
      m->next=p;
      m=p;
      m->next=NULL;
      p=p->next;
      q=q->next;
    }
    else
      q=q->next;

  }
  return C;
}

void Print_LinkList(LinkList p)
{
  LNode *q=p->next;
  while(q)
  {
    printf("%3d",q->data);
    q=q->next;
  }
  printf("\n");
}

void main()
{
  LinkList A,B,C;   //这里你定义了C  C的类型是指针  但是你都没有给他申请空间
  C->next=NULL;     ///而这里你就直接开始用next了 C都没有空间你怎么给next赋值  显然不行的 


  //C =  (LinkList)malloc(sizeof(LNode));   ////加上这句  
  A=create_LinkList();
  B=create_LinkList();
  C=In_LinkList(A,B);
  printf("A: ");
  Print_LinkList(A);
  printf("\nB: ");
  Print_LinkList(B);
  printf("\nC: ");
  Print_LinkList(C);
  getch();
}


[解决办法]
++

引用:

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define NULL 0

typedef struct node
{
  int data;
  struct node *next;
}LNode,*LinkList;

LinkList create_LinkList()
{
  int x;
  LinkList L;
  LNode *r;
  L=(LinkList)malloc(sizeof(LNode));
  L->next=NULL;
  r=L;
  printf("input the value x:\n");
  scanf("%d",&x);
  while(x)
  {
    r->next=(LNode *)malloc(sizeof(LNode));
    r->next->data=x;
    r=r->next;
    scanf("%d",&x);
  }
  r->next=NULL;
  return L;
}

LinkList In_LinkList(LinkList A,LinkList B)
{
  LinkList C;
  LNode *p,*q,*m;
  C=(LinkList)malloc(sizeof(LNode));
  C->next=NULL;
  m=C;
  p=A->next;
  q=B->next;
  while(p&&q)
  {
    if(p->data==q->data)
    {
      m->next=p;
      m=p;
      m->next=NULL;
      p=p->next;
      q=q->next;
    }
    else
      q=q->next;

  }
  return C;
}

void Print_LinkList(LinkList p)
{
  LNode *q=p->next;
  while(q)
  {
    printf("%3d",q->data);
    q=q->next;
  }
  printf("\n");
}

void main()
{
  LinkList A,B,C;   //这里你定义了C  C的类型是指针  但是你都没有给他申请空间
  C->next=NULL;     ///而这里你就直接开始用next了 C都没有空间你怎么给next赋值  显然不行的 
  //C =  (LinkList)malloc(sizeof(LNode));   ////加上这句  
  A=create_LinkList();
  B=create_LinkList();
  C=In_LinkList(A,B);
  printf("A: ");
  Print_LinkList(A);
  printf("\nB: ");
  Print_LinkList(B);
  printf("\nC: ");
  Print_LinkList(C);
  getch();
}


[解决办法]
LinkList In_LinkList(LinkList A,LinkList B)
这个函数写的有问题

LinkList In_LinkList(LinkList A,LinkList B)
{
  LinkList C;
  LNode *p,*q,*m,*r;
  C = (LinkList)malloc(sizeof(LNode));
  C->next = NULL;
  m = C;
  p = A->next;
  r = q = B->next;
  
  while(p && r ){   
    q = r;
    while(p && q){ 
      if(p->data == q->data)
      {
         //m->next = p; //这是错误的,求交集不会破坏源链表,所以要创建新节点。
           m->next = (LinkList)malloc(sizeof(LNode));//创建新节点。
           m->next->data = p->data;//复制数据。
           m = m->next;            //新节点作为当前节点。
           m->next = NULL;         //新节点是尾节点,所以 next 为 NULL。
           p = p->next;
           q = q->next;
           r = q; 
      }
      else
        q = q->next; 
     }
     p = p->next; 
  }
  return C;
}


PS :
   你可能没有理顺,求交集的逻辑,所以代码得不到,你想要的结果。

热点排行