一个测试复制结点到链表的程序,却无法正常运行,不知道哪里出了有关问题
一个测试复制结点到链表的程序,却无法正常运行,不知道哪里出了问题?
#include "stdlib.h "
#include "stddef.h "
#include "string.h "
#include "ctype.h "
#include "stdio.h "
#include "math.h "
#include "assert.h "
struct LNode
{
int sequence;
struct LNode *next;
}LNode;
typedef struct LNode *Linklist;
void Copy(Linklist node, Linklist parentdmp)
{ /* 将结点拷贝到新建的链表中 */
Linklist p,q;
struct LNode *r;
p=parentdmp;
q=node;
r=(struct LNode *)malloc(sizeof(struct LNode));
assert(r!=NULL);
r-> sequence=q-> sequence;
while(p-> next)
p=p-> next;
r-> next=NULL;
p-> next=r;
}
void main()
{
int i;
Linklist head;
Linklist p,q;
head=(Linklist)malloc(sizeof(struct LNode));
assert(head!=NULL);
head-> next=NULL;
for(i=0; i <5; i++)
{
p=(Linklist)malloc(sizeof(struct LNode));
p-> sequence=i;
Copy(p,head);
q=head-> next;
free(p);
}
p=head-> next;
while(p)
{
printf( "sequence=%d\n ",p-> sequence);
}
}
本来是个很简单的程序,是一个把结点拷贝到新建立的链表parentdmp中,但是却调不出来。我在测试的主程序中,先建立一个链表的头结点head,然后在循环中,每次建立一个结点,通过Copy函数将结点拷贝到head中,然后再释放刚才建立的结点。但是每次运行,却是“sequence=0”的无限循环,不知道问题在哪里,请教大家了!
------解决方案--------------------
错的地方还是挺多的.先把改后的代码贴上,如下所示:
#include "stdlib.h "
#include "stddef.h "
#include "string.h "
#include "ctype.h "
#include "stdio.h "
#include "math.h "
#include "assert.h "
struct LNode
{
int sequence;
struct LNode *next;
}LNode;
typedef struct LNode *Linklist;
void Copy(Linklist node, Linklist &parentdmp) // 这里改了
{/* 将结点拷贝到新建的链表中 */
Linklist &p=parentdmp,q; // 这里改了
struct LNode *r;
//p=parentdmp;
q=node;
r=(struct LNode *)malloc(sizeof(struct LNode));
assert(r!=NULL);
r-> sequence=q-> sequence;
r-> next=p-> next; // 这里改了
p-> next=r;
}
int main()
{
int i;
Linklist head;
Linklist p,q;
head=(Linklist)malloc(sizeof(struct LNode));
assert(head!=NULL);
head-> next=NULL;
for(i=0; i <5; i++)
{
p=(Linklist)malloc(sizeof(struct LNode));
p-> sequence=i;
Copy(p,head);
//q=head-> next; // 这句好象没用吧?
free(p);
}
p=head-> next;
while(p)
{
printf( "sequence=%d\n ",p-> sequence);
p = p-> next; // 这里加了此句
}
getchar();
return 0;
}
#include "stdlib.h "
#include "stddef.h "
#include "string.h "
#include "ctype.h "
#include "stdio.h "
#include "math.h "
#include "assert.h "
struct LNode
{
int sequence;
struct LNode *next;
}LNode;
typedef struct LNode *Linklist;
void Copy(Linklist node, Linklist parentdmp)
{ /* 将结点拷贝到新建的链表中 */
Linklist p,q;
struct LNode *r;
p=parentdmp;
q=node;
r=(struct LNode *)malloc(sizeof(struct LNode));
assert(r!=NULL);
r-> sequence=q-> sequence;
while(p-> next)
p=p-> next;
r-> next=NULL;
p-> next=r;
}
void main()
{
int i;
Linklist head;
Linklist p,q;
head=(Linklist)malloc(sizeof(struct LNode));
assert(head!=NULL);
head-> next=NULL;
for(i=0; i <5; i++)
{
p=(Linklist)malloc(sizeof(struct LNode));
p-> sequence=i;
Copy(p,head);
q=head-> next;
free(p);
}
p=head-> next;
while(p)
{
printf( "sequence=%d\n ",p-> sequence);
}
}
本来是个很简单的程序,是一个把结点拷贝到新建立的链表parentdmp中,但是却调不出来。我在测试的主程序中,先建立一个链表的头结点head,然后在循环中,每次建立一个结点,通过Copy函数将结点拷贝到head中,然后再释放刚才建立的结点。但是每次运行,却是“sequence=0”的无限循环,不知道问题在哪里,请教大家了!
------解决方案--------------------
错的地方还是挺多的.先把改后的代码贴上,如下所示:
#include "stdlib.h "
#include "stddef.h "
#include "string.h "
#include "ctype.h "
#include "stdio.h "
#include "math.h "
#include "assert.h "
struct LNode
{
int sequence;
struct LNode *next;
}LNode;
typedef struct LNode *Linklist;
void Copy(Linklist node, Linklist &parentdmp) // 这里改了
{/* 将结点拷贝到新建的链表中 */
Linklist &p=parentdmp,q; // 这里改了
struct LNode *r;
//p=parentdmp;
q=node;
r=(struct LNode *)malloc(sizeof(struct LNode));
assert(r!=NULL);
r-> sequence=q-> sequence;
r-> next=p-> next; // 这里改了
p-> next=r;
}
int main()
{
int i;
Linklist head;
Linklist p,q;
head=(Linklist)malloc(sizeof(struct LNode));
assert(head!=NULL);
head-> next=NULL;
for(i=0; i <5; i++)
{
p=(Linklist)malloc(sizeof(struct LNode));
p-> sequence=i;
Copy(p,head);
//q=head-> next; // 这句好象没用吧?
free(p);
}
p=head-> next;
while(p)
{
printf( "sequence=%d\n ",p-> sequence);
p = p-> next; // 这里加了此句
}
getchar();
return 0;
}