| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 938 人关注过本帖
标题:求助链表...无助ing!
只看楼主 加入收藏
y218z903
Rank: 1
等 级:新手上路
帖 子:63
专家分:0
注 册:2007-7-27
收藏
 问题点数:0 回复次数:7 
求助链表...无助ing!

#include"stdio.h"
#include"malloc.h"
#include"conio.h"
typedef struct stu
{
int num;
struct stu *next;

} node;
node *crtlink(node *head,int n)
{
node *p,*s;
int i;
s=head;
for(i=0;i<n;i++)
{
p=(node *)malloc(sizeof(node));
printf("请输入第%d个数据:",i+1);
scanf("%d",&p->num);
s->next =p;
s=p;

}
p->next=NULL;
return head;
}

void display(node *head)
{
node *p;
p=head->next;
while(p)
{
printf("输出刚刚的数据为:%d",p->num);
p=p->next;
}

}
int main(void)
{
node *head;
int n;
head=(node *)malloc(sizeof(node));

printf("请输入你所需要创建的数据的长度: \n");

scanf("%d",&n);
head=crtlink(head,n);
display(head);
return 0;
}
提示 ;The program 'D:\C++\MSDEV98\MYPROJECTS\链表\Debug\链表.exe' has exited with code 0 (0x0).

搜索更多相关主题的帖子: 链表 ing 
2007-10-15 17:23
y218z903
Rank: 1
等 级:新手上路
帖 子:63
专家分:0
注 册:2007-7-27
收藏
得分:0 

希望多指出代码的错误点及格式....


2007-10-15 17:25
cobby
Rank: 1
等 级:新手上路
威 望:1
帖 子:565
专家分:0
注 册:2007-7-11
收藏
得分:0 
你的程序没问题,我刚调试运行过,这个问题请检查编译器环境。
图片附件: 游客没有浏览图片的权限,请 登录注册


努力成为菜鸟!
2007-10-15 17:44
y218z903
Rank: 1
等 级:新手上路
帖 子:63
专家分:0
注 册:2007-7-27
收藏
得分:0 
我用的是vc++6.0  编译器环境。?  如何检查?

2007-10-15 17:50
y218z903
Rank: 1
等 级:新手上路
帖 子:63
专家分:0
注 册:2007-7-27
收藏
得分:0 

#include"stdio.h"
#include"malloc.h"
#include"conio.h"
typedef struct stu
{
int num;
struct stu *next;

} node;
node *crtlink(node *head,int n)
{
node *p,*s;
int i;
s=head;
for(i=0;i<n;i++)
{
p=(node *)malloc(sizeof(node));
printf("请输入第%d个数据:",i+1);
scanf("%d",&p->num);
s->next =p;
s=p;

}
p->next=NULL;
return head;
}

void display(node *head)
{
node *p;
p=head->next;
while(p)
{
printf("输出刚刚的数据为:%d\n",p->num);
p=p->next;
}

}
void nizhi(node *head)
{
node *p,*s;
p=head->next;
head->next=NULL;
while(p)
{
s=p;p=p->next;
p->next=head->next;
head->next=s;
}
}
int main(void)
{
node *head,*p;
int n;
head=(node *)malloc(sizeof(node));

printf("请输入你所需要创建的数据的长度: \n");

scanf("%d",&n);
head=crtlink(head,n);
display(head);
nizhi(&head);
display(head);
getch();
return 0;
} 我换了个编译器,,,之后就行了,,不过我另外增加了个逆置的函数..之后又出错...


2007-10-15 18:56
missiyou
Rank: 5Rank: 5
等 级:贵宾
威 望:16
帖 子:531
专家分:218
注 册:2007-10-9
收藏
得分:0 

#include"stdio.h"
#include"malloc.h"
#include"conio.h"
typedef struct stu
{
int num;
struct stu *next;

} node;
node *crtlink(node *head,int n)
{
node *p,*s;
int i;
s=head;
for(i=0;i<n;i++)
{
p=(node *)malloc(sizeof(node));
p->next=NULL; //直接在这里加上就好了
printf("请输入第%d个数据:",i+1);
scanf("%d",&p->num);
s->next =p;
s=p;

}
p->next=NULL;//p这个是在for里释放了,在这里加就等于野指针了,
return head;
}

void display(node *head)
{
node *p;
p=head->next;
while(p)
{
printf("输出刚刚的数据为:%d\n",p->num);
p=p->next;
}

}
void nizhi(node *head)
{
node *p,*s;
p=head->next;
head->next=NULL;
while(p)
{
/*s=p;p=p->next;
p->next=head->next;
head->next=s;*/我来改一下;s=p->next; p->next=head->next;head->next=p;p=s;我想这样就对了;
}
int main(void)
{
node *head,*p;
int n;
head=(node *)malloc(sizeof(node));

printf("请输入你所需要创建的数据的长度: \n");

scanf("%d",&n);
head=crtlink(head,n);
display(head);
nizhi(&head);//为什么要加一个&直接,nizhi(head);
display(head);
getch();
return 0;
}

2007-10-15 22:09
missiyou
Rank: 5Rank: 5
等 级:贵宾
威 望:16
帖 子:531
专家分:218
注 册:2007-10-9
收藏
得分:0 

提示 ;The program 'D:\C++\MSDEV98\MYPROJECTS\链表\Debug\链表.exe' has exited with code 0 (0x0).

这个可能是你打开了 链表.exe 没有关了,所以编译时显示没有退出。
本人英语太差,我是这样理解的

2007-10-15 22:18
zxc1998
Rank: 1
等 级:新手上路
威 望:1
帖 子:133
专家分:0
注 册:2007-3-21
收藏
得分:0 
void nizhi(node *head)
{
node *p,*s;
p=head->next;
head->next=NULL;
while(p)
{
s=p;p=p->next;
p->next=head->next; //这个地方错了,应该为s->next = head->next;
head->next=s;
}
}
2007-10-18 23:46
快速回复:求助链表...无助ing!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029646 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved