简单链表问题,求教

mars131 2005-03-19 11:28:31
这是一个创建链表的程序,可是不知道为什么运行的时候会出错,错误在for 循环的
L->next = NULL语句上,兄弟们帮我看看,vs和dev-c++上都有错,编译是可以通过的


#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>


struct LNode
{
int number;
struct LNode *next;
}*Linklist;

void CreatList(struct LNode *L,int n)
{

int i;
for(i= n;i>0;--i)
{
struct LNode *p;
p = (struct LNode *)malloc(sizeof(struct LNode));
printf("please enter the int:");
scanf("%d",&p->number);
printf("%d",(*p).number);
// p->next = L->next;
L->next = NULL;
//printf("/n the number %d struct is succesfull", i);
}
}


int main(int argc, char *argv[])
{
struct LNode *L;
L = (struct LNode *)malloc(sizeof(struct LNode));
L->next = NULL;
CreatList(Linklist,3);
system("PAUSE");
return 0;
}
...全文
137 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Flood1984 2005-03-20
  • 打赏
  • 举报
回复
for(i= n;i>0;--i)
{
struct LNode *p;
p = (struct LNode *)malloc(sizeof(struct LNode));
printf("please enter the int:");
scanf("%d",&p->number);
printf("%d",(*p).number);
L->next=p;
p->next = NULL;
L=p;
printf("/n the number %d struct is succesfull", i);
}
dongpy 2005-03-19
  • 打赏
  • 举报
回复
你原来的程序里:
int main(int argc, char *argv[])
{
struct LNode *L;
L = (struct LNode *)malloc(sizeof(struct LNode));
L->next = NULL;
CreatList(Linklist,3); //Linklist只是一个指针,没有分配内存。
system("PAUSE");
return 0;
}
dongpy 2005-03-19
  • 打赏
  • 举报
回复
帮你改了:

struct LNode
{
int number;
struct LNode *next;
}*Linklist;

void CreatList(struct LNode *L,int n)
{

int i;
for(i= n;i>0;--i)
{
struct LNode *p;
p = (struct LNode *)malloc(sizeof(struct LNode));
printf("please enter the int:");
scanf("%d",&p->number);
printf("%d",(*p).number);
// p->next = L->next;
L->next = p; ///////////////////////////
L = L->next; ////////////////////////////
//printf("/n the number %d struct is succesfull", i);
}
}


int main(int argc, char *argv[])
{
struct LNode *L;
L = (struct LNode *)malloc(sizeof(struct LNode));
L->next = NULL;
CreatList(L,3); /////////////////////////
system("PAUSE");
return 0;
}
llf_hust 2005-03-19
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>


struct LNode
{
int number;
struct LNode *next;
}*Linklist;

void CreatList(struct LNode **L,int n)
{

int i;
for(i= n;i>0;--i)
{
struct LNode *p;
p = (struct LNode *)malloc(sizeof(struct LNode));
printf("please enter the int:");
scanf("%d",&p->number);
printf("%d",(*p).number);
p->next = (*L)->next;
*L = p;

}
}


int main()
{
struct LNode *L;
int i;
L = (struct LNode *)malloc(sizeof(struct LNode));
L->next = NULL;
CreatList(&L,3);


system("PAUSE");
return 0;
}
languagec 2005-03-19
  • 打赏
  • 举报
回复
没插好
languagec 2005-03-19
  • 打赏
  • 举报
回复
void CreatList(struct LNode *L,int n)
{

int i;
for(i= n;i>0;--i)
{
struct LNode *p;
p = (struct LNode *)malloc(sizeof(struct LNode));
printf("please enter the int:");
scanf("%d",&p->number);
printf("%d",(*p).number);

p->next = L->next;
L->next = p;

//printf("/n the number %d struct is succesfull", i);
}
}
mars131 2005-03-19
  • 打赏
  • 举报
回复
呵呵,真是谢谢你们,星期六还这么快的帮助我,回答的那么详细:),感动
缘分啊~~~~~~~~~~~

70,022

社区成员

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

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