复制单链表问题,请前辈帮忙改错
这是一个复制单链表问题,编译的时候出现警告,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




