主题:[讨论]逆转一个线性表
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct node
{
ElemType data;
struct node * link;
}LNode,*LinkList;
LinkList Creat(int n)
{
LinkList p,r,list;
int i,num;
list=NULL;
for(i=1;i<=n;i++)
{
puts("Input a number");
scanf("%d",&num);
p=(LinkList)malloc(sizeof(LNode));
p->data=num;
p->link=NULL;
if(list==NULL)
list=p;
else
r->link=p;
r=p;
}
return list;
}
LinkList Invert(LinkList list)
{
LinkList q,p,r;
p=list;
q=NULL;
while(p!=NULL)
{
r=q;
q=p;
p=p->link;
q->link=r;
}
return q;
}
void Print(LinkList list)
{
LinkList p;
p=list;
while(p!=NULL)
{
printf("%d\n",p->data);
p=p->link;
}
}
main()
{
int n;
LinkList list,ilist;
puts("Input the size of LinkList you want to creat");
scanf("%d",&n);
list=Creat(n);
ilist=Invert(list);
Print(list);
Print(ilist);
system("pause");
}
大家好。。。这个程序到底错在哪里
#include <stdlib.h>
typedef int ElemType;
typedef struct node
{
ElemType data;
struct node * link;
}LNode,*LinkList;
LinkList Creat(int n)
{
LinkList p,r,list;
int i,num;
list=NULL;
for(i=1;i<=n;i++)
{
puts("Input a number");
scanf("%d",&num);
p=(LinkList)malloc(sizeof(LNode));
p->data=num;
p->link=NULL;
if(list==NULL)
list=p;
else
r->link=p;
r=p;
}
return list;
}
LinkList Invert(LinkList list)
{
LinkList q,p,r;
p=list;
q=NULL;
while(p!=NULL)
{
r=q;
q=p;
p=p->link;
q->link=r;
}
return q;
}
void Print(LinkList list)
{
LinkList p;
p=list;
while(p!=NULL)
{
printf("%d\n",p->data);
p=p->link;
}
}
main()
{
int n;
LinkList list,ilist;
puts("Input the size of LinkList you want to creat");
scanf("%d",&n);
list=Creat(n);
ilist=Invert(list);
Print(list);
Print(ilist);
system("pause");
}
大家好。。。这个程序到底错在哪里