当前位置: 代码迷 >> C语言 >> 单链表的正序创建的问题(帮下忙谢谢了)
  详细解决方案

单链表的正序创建的问题(帮下忙谢谢了)

热度:154   发布时间:2006-05-17 21:35:00.0
好加油啊
我在等着你呢
----------------解决方案--------------------------------------------------------

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

typedef struct Lnode
{
int date;
struct Lnode *next;

}Node;


Node *CreatList(Node *L,int n)
{
Node *s, *p;

int i;
for(i=0;i<n;i++)
{
s=(Node *)malloc(sizeof(Node));
scanf("%d",&s->date);
if(L==0)
L=p=s;
else p->next=s;
p=s;

}
p->next=NULL;
return L;

}

void print(Node *L)
{
Node *p=L;
printf("********************************\n");
while(p)
{
printf("%d ",p->date);
p=p->next;
}
printf("\n********************************\n");
}


int main()
{
int n;
Node *L=NULL;


printf("Please Input how many NOdes do you want:");
scanf("%d",&n);
printf("Please Input the NOdes you want:");
L=CreatList(L, n);
print(L);

return 0;
}

C-FREE下运行没问题了..

你去试试 VC++吧..不知道是不是一样的问题..

我给你发的短消息没收到吗?


[此贴子已经被作者于2006-5-17 21:49:36编辑过]


----------------解决方案--------------------------------------------------------

太谢谢了啊
我刚才没有看,
我去试试啊
看了你的消息了


----------------解决方案--------------------------------------------------------

#include"stdio.h"
#include"stdlib.h"
#include"malloc.h"

#define null 0

typedef struct Lnode
{
int date;
struct Lnode *next;

}Node;


Node *CreatList(Node *L,int n)
{
Node *s, *p=L;

int i;
for(i=0;i<n;i++)
{
s=(Node *)malloc(sizeof Node);
if(!s) break;
scanf("%d",&s->date);

s->next=NULL;
p->next=s;
p=s;
}
return L;
}

void print(Node *L)
{
Node *p=L->next;
printf("********************************\n");
while(p!=NULL)
{
printf("%d ",p->date);
p=p->next;
}
printf("\n********************************\n");
}


int main()
{
int n;
Node *L;


printf("Please Input how many NOdes do you want:");
scanf("%d",&n);
printf("Please Input the NOdes you want:");
L=(Node *)malloc(sizeof Node );
L->next=NULL;
CreatList(L, n);
print(L);

return 1;
}
我在vc中试了 这个可以运行


----------------解决方案--------------------------------------------------------
多谢谢各位了啊
呵呵
最感谢的是非22楼呵呵
他启发我发现了我的问题
犯了个很低等的错误,
忘记给L申请空间了啊
晕郁闷啊
在此结贴了啊
----------------解决方案--------------------------------------------------------
  相关解决方案