CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C语言

复制单链表问题,请前辈帮忙改错

楼主landay001(方方)2006-03-30 23:17:06 在 C/C++ / C语言 提问

这是一个复制单链表问题,编译的时候出现警告,warning:   possible   use   of   'cop'before   definition   of   function.     我明明已经定义了的,为何还会出现这种情况呢.  
   
  下面是我写的程序(TC环境下),请前辈指点.  
  #include<stdio.h>  
  #include<malloc.h>  
  #define   NULL   0  
  #define   LEN   sizeof(struct   letter)  
  /*定义一个结点*/  
  struct   letter  
  {  
    char   let;  
    struct   letter   *next;  
  };  
   
   
  /*产生一个单链表*/  
  struct   letter   *creat(void)  
      {   struct   letter   *head;  
          struct   letter   *p1,*p2;             /*p1用于指向新开辟的节点*/  
           
          p1=(struct   letter*)malloc(LEN)   ;  
   
          scanf("%c",&p1->let);  
          head=p1;  
          head->next=NULL;  
          while(1)  
            {  
              p2   =   p1;  
              p1   =   (struct   letter*)malloc(LEN);  
              scanf("%c",   &p1->let);  
              if(p1->let   ==   '\n')    
      break;  
              p2->next   =   p1;  
               
              p2               =   p2->next;  
   
            }  
        p2->next   =   NULL;  
        return(head);  
      }  
   
  /*打印*/  
      void   print(struct   letter   *head)  
   
      {  
        struct   letter   *p;  
        p   =   head;  
        while(   p   !=   NULL   )  
        {  
        printf("   %c",   p->let);  
        p   =   p->next;  
        }  
  }  
   
   
   
   
  /*复制一个单链表*/  
  struct   letter   *copy(struct   letter   *head,struct   letter   *cop)  
  {  
      struct   letter   *p1,*p2,*p;  
      p1=head;  
      p=(struct   letter*)malloc(LEN)   ;         /*p用于指向新开辟的节点*/  
       
      p2=cop=p;  
      while(p1->next!=NULL)  
            {p2->let=p1->let;  
              p1=p1->next;  
   
              p   =   (struct   letter*)malloc(LEN);  
              p2->next=p;  
              p2=p2->next;        
   
            }  
    p2->let=p1->let;  
    p2->next=NULL;  
    return   (cop);  
    }  
   
  void     main()  
  {         char   c;  
            struct   letter   *head;  
            struct   letter   *cop;  
            printf(     "input     a     string:\n     ");  
            head=creat();  
            print(head);   printf("\n");       /*打印输入的字符串*/            
               
             
            head=copy(head,cop);  
            print(head);  
            print(cop);  
  }      
   
  问题点数:20、回复次数:5Top

1 楼sms88(白板http://shop34112882.taobao.com)回复于 2006-03-30 23:26:55 得分 3

怎么我在VC6.0下就没有任何错误呢?Top

2 楼landay001(方方)回复于 2006-03-30 23:31:42 得分 0

唉,不知道啊,烦死我了.Top

3 楼jixingzhong(瞌睡虫·星辰)回复于 2006-03-31 10:08:44 得分 2

#include<stdio.h>  
  #include<malloc.h>  
  #define   NULL   0  
  #define   LEN   sizeof(struct   letter)  
   
  struct   letter  
  {  
    char   let;  
    struct   letter   *next;  
  };  
   
   
  struct   letter   *creat(void)  
      {   struct   letter   *head;  
          struct   letter   *p1,*p2;                    
          p1=(struct   letter*)malloc(LEN)   ;  
   
          scanf("%c",&p1->let);  
          head=p1;  
          head->next=NULL;  
          while(1)  
            {  
              p2   =   p1;  
              p1   =   (struct   letter*)malloc(LEN);  
              scanf("%c",   &p1->let);  
              if(p1->let   ==   '\n')    
                      break;  
              p2->next   =   p1;  
               
              p2               =   p2->next;  
   
            }  
        p2->next   =   NULL;  
        return(head);  
      }  
   
   
      void   print(struct   letter   *head)  
   
      {  
        struct   letter   *p;  
        p   =   head;  
        while(   p   !=   NULL   )  
        {  
                printf("%c   ",   p->let);  
                p   =   p->next;  
        }  
  }  
   
  struct   letter   *copy(struct   letter   *head,struct   letter   *cop)  
  {  
      struct   letter   *p1,*p2,*p;  
      p1=head;  
      p=(struct   letter*)malloc(LEN)   ;            
      p2=cop=p;  
      while(p1->next!=NULL)  
            {p2->let=p1->let;  
              p1=p1->next;  
   
              p   =   (struct   letter*)malloc(LEN);  
              p2->next=p;  
              p2=p2->next;        
   
            }  
    p2->let=p1->let;  
    p2->next=NULL;  
    return   (cop);  
    }  
   
  void     main()  
  {         char   c;  
            struct   letter   *head;  
            struct   letter   *cop   =   (struct   letter*)malloc(LEN);  
            printf("input     a     string:\n");  
            head=creat();  
            print(head);  
            printf("\n");                  
            head=copy(head,cop);  
            print(head);  
            print(cop);  
  }Top

4 楼jixingzhong(瞌睡虫·星辰)回复于 2006-03-31 10:09:27 得分 5

问题语句:  
  struct   letter   *cop   =   (struct   letter*)malloc(LEN);  
  cop   没有分配指针空间   ...Top

5 楼lyqx888()回复于 2006-03-31 10:29:21 得分 10

#include<stdio.h>  
  #include<malloc.h>  
  #define   NULL   0  
  #define   LEN   sizeof(struct   letter)  
  /*定义一个结点*/  
  struct   letter  
  {  
    char   let;  
    struct   letter   *next;  
  };  
   
   
  /*产生一个单链表*/  
  struct   letter   *creat(void)  
      {   struct   letter   *head;  
          struct   letter   *p1,*p2;             /*p1用于指向新开辟的节点*/  
           
          p1=(struct   letter*)malloc(LEN)   ;  
   
          scanf("%c",&p1->let);  
          head=p1;  
          head->next=NULL;  
          while(1)  
            {  
              p2   =   p1;  
              p1   =   (struct   letter*)malloc(LEN);  
              scanf("%c",   &p1->let);  
              if(p1->let   ==   '\n')    
      break;  
              p2->next=p1;  
               
              p2=p2->next;  
   
            }  
        p2->next   =   NULL;  
        return(head);  
      }  
   
  /*打印*/  
      void   print(struct   letter   *head)  
   
      {  
        struct   letter   *p;  
        p   =   head;  
        while(   p   !=   NULL   )  
        {  
        printf("   %c",   p->let);  
        p   =   p->next;  
        }  
  }  
   
   
   
   
  /*复制一个单链表*/  
  struct   letter   *copy(struct   letter   *head,struct   letter   *cop)  
  {  
      struct   letter   *p1,*p2,*p;  
      p1=head;  
      p=(struct   letter*)malloc(LEN)   ;         /*p用于指向新开辟的节点*/  
       
      p2=cop=p;  
      while(p1->next!=NULL)  
            {p2->let=p1->let;  
              p1=p1->next;  
   
              p   =   (struct   letter*)malloc(LEN);  
              p2->next=p;  
              p2=p2->next;        
   
            }  
    p2->let=p1->let;  
    p2->next=NULL;  
    return   (cop);  
    }  
   
  struct   letter   *cop;  
  //全局变量  
  void     main()  
  {         char   c;  
            struct   letter   *head;  
             
            printf(     "input     a     string:\n     ");  
            head=creat();  
            print(head);   printf("\n");       /*打印输入的字符串*/            
               
             
            head=copy(head,cop);  
            print(head);  
            print(cop);  
     
  }      
   
  加个全局变量就行了Top

相关问题

  • 简单改错,马上给分。
  • 简单C程序改错求教
  • 又是一个简单的c程序改错!请赐教!
  • 简单的C程序错误-》关于对列的,帮改错
  • 复制图形简单问
  • 改错误
  • 代码改错
  • sql改错
  • 一道改错!
  • 帮忙改错!

关键词

  • 节点
  • null
  • struct letter
  • let
  • cop
  • 单链表
  • head
  • next
  • malloc
  • 复制

得分解答快速导航

  • 帖主:landay001
  • sms88
  • jixingzhong
  • jixingzhong
  • lyqx888

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo