这段简单的单练表怎么就出错了?错在哪里?奇怪?谢谢

Kenn_C 2006-04-20 11:04:53
#include <stdio.h>

struct Node{
int info;
struct Node *next;
};
typedef struct Node *PLinkList;


main()
{
int i,n,e;

printf("How many nodes you want to creat:");
scanf("%d",&n);
PLinkList L=(PLinkList)malloc(sizeof(struct Node));
L -> next = NULL;
if(L != NULL){
for(i = n;i > 0; i--)
{
PLinkList p=(PLinkList)malloc(sizeof(struct Node));
if(p != NULL)
{
printf("input:");
scanf("%d",&e);
p -> info = e;
p -> next = L -> next;
L -> next = p;
}
}

}

while(L -> next != NULL)
{
printf("%d\n",L -> next -> info);
L = L -> next;
}
getch();
}
...全文
144 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复

typedef struct Node *PLinkList;


————————————————————————————

现在没时间调试

不过感觉这句很有问题
abiao1421 2006-04-21
  • 打赏
  • 举报
回复
改成这样就可以了
#include <stdio.h>


typedef struct Node{
int info;
struct Node *next;
}PLinkList;



main()
{
int i,n,e;
PLinkList *L,*p;
printf("How many nodes you want to creat:");
scanf("%d",&n);
L=(PLinkList *)malloc(sizeof(struct Node));
L -> next = NULL;
if(L != NULL){
for(i = n;i > 0; i--)
{
p=(PLinkList *)malloc(sizeof(struct Node));
if(p != NULL)
{
printf("input:");
scanf("%d",&e);
p -> info = e;
p -> next = L -> next;
L -> next = p;
}
}

}

while(L -> next != NULL)
{
printf("%d\n",L -> next -> info);
L = L -> next;
}
getch();
}
Balu__ 2006-04-21
  • 打赏
  • 举报
回复
就是头文件的问题.
mysear 2006-04-21
  • 打赏
  • 举报
回复
添加头文件
#include <stdlib.h>
#include <conio.h>
还有最后退出程序前最好将malloc的内存全部释放掉
Tdxdy 2006-04-21
  • 打赏
  • 举报
回复
#include <stdio.h>

struct Node
{
int info;
struct Node *next;
};
typedef struct Node *PLinkList;


main()
{
int i,n,e;

printf("How many nodes you want to creat:");
scanf("%d",&n);
PLinkList L=(PLinkList)malloc(sizeof(struct Node));
L -> next = NULL;
if(L != NULL)
{
for(i=n;i>0;i--)
{
PLinkList p=(PLinkList)malloc(sizeof(struct Node));
if(p != NULL)
{
printf("input:");
scanf("%d",&e);
p -> info = e;
p -> next = L -> next; // 这2句有问题
L -> next = p; // LZ找本书看看吧
}
}

}

while(L->next!=NULL)
{
printf("%d\n",L -> next -> info);
L = L -> next;
}
getch();
}
baggio1984 2006-04-20
  • 打赏
  • 举报
回复
#include <conio.h>
baggio1984 2006-04-20
  • 打赏
  • 举报
回复
#include<malloc.h>

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧