当前位置: 代码迷 >> C语言 >> 菜鸟求助...关于链表问题....
  详细解决方案

菜鸟求助...关于链表问题....

热度:136   发布时间:2007-09-09 14:11:43.0
菜鸟求助...关于链表问题....

今天学习单链表添加数据...我用的是DEV C++ 编译器,,程序没有报错..但是不能正常运行

就出现了这个图片..想了好久,都不明白..


但是用 TC编译器却,可以运行.. 有点 晕了... 求求大家帮帮我啊~~


以下是程序,,主要是功能是: 让用户输入数据,并把数据打印出来....



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


typedef struct student /*建立*/
{
int data;
struct student*next;
}students;

students*init() /*初始化*/
{
students*head;
head=(students*)malloc(sizeof(students));
head->next=NULL;
return head ;
}

int main(void)
{
int input(students*head);
int print(students*head);
students*head;
head=(students*)malloc(sizeof(students));
head=init();
input(head);
print(head);
getch();
}


int input(students*head)
{
char sign;
students*p;
do
{
p=(students*)malloc(sizeof(students));
scanf("%d",&p->data);
head->next=p;
head=p;
printf("wether to inout (y/n):");
scanf("%s",&sign);
}
while(sign=='Y'||sign=='y'); /*do-while 结构。*/

head->next=NULL;
return 1;
}


int print(students*head)
{
while(head->next!=NULL)
{
head=head->next;
printf("%d",head->data);
printf("hello");
}
return 2;
}



先谢谢各位了............

搜索更多相关的解决方案: 链表  

----------------解决方案--------------------------------------------------------
很简单,因你的操作指针有错
还有,别以为能运行就是对的
特别是TC上



by 雨中飞燕 QQ:78803110 QQ讨论群:5305909

[url=http://bbs.bc-cn.net/viewthread.php?tid=163571]请大家不要用TC来学习C语言,点击此处查看原因[/url]
[url=http://bbs.bc-cn.net/viewthread.php?tid=162918]C++编写的Windows界面游戏[/url]
[url=http://yzfy.org/]C/C++算法习题(OnlineJudge):[/url] http://yzfy.org/
----------------解决方案--------------------------------------------------------


很感谢,斑竹的留言,,但是,我还是不懂啊>>?? 555555555
还有就是
原来程序输入一个数据的时候,用(Y/N)来表示输入是否(继续/结束),,我把它换成了,,但用户输入 -1表示程序结束...结果就可以运行了,,,TC就不用说了...
下面是可以运行的程序,,只是改变了sign(标记)... 到底是怎么回事啊>>?



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


typedef struct student /*建立*/
{
int data;
struct student*next;
}students;

students*init() /*初始化*/
{
students*head;
head=(students*)malloc(sizeof(students));
head->next=NULL;
return head ;
}

int main(void)
{
int input(students*head);
int print(students*head);
students*head;
head=(students*)malloc(sizeof(students));
head=init();
input(head);
print(head);
getch();
}


int input(students*head)
{
int sign;
students*p;
do
{
p=(students*)malloc(sizeof(students));
scanf("%d",&p->data);
head->next=p;
head=p;
printf("wether to inout or not (-1 means end):");
scanf("%d",&sign);
}
while(sign!=-1); /*do-while 结构。*/

head->next=NULL;
return 1;
}


int print(students*head)
{
while(head->next!=NULL)
{
head=head->next;
printf("\n%d",head->data);
}
return 2;
}


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

----------------解决方案--------------------------------------------------------
char sign;
scanf("%s", &sign);
天哪。。。。这么危险的代码。。。。



by 雨中飞燕 QQ:78803110 QQ讨论群:5305909

[url=http://bbs.bc-cn.net/viewthread.php?tid=163571]请大家不要用TC来学习C语言,点击此处查看原因[/url]
[url=http://bbs.bc-cn.net/viewthread.php?tid=162918]C++编写的Windows界面游戏[/url]
[url=http://yzfy.org/]C/C++算法习题(OnlineJudge):[/url] http://yzfy.org/
----------------解决方案--------------------------------------------------------


我又测试了一下,把 标记变了一下..
do
{
......
char sign;
scanf("%c",&sign)
}while(sign=='Y'||sign=='y');


;,,结果只运行一次,,,就 直接打印了出来,,, 似乎把输入的 'enter' 相对应的ASLL也赋给了 sign,,,从儿导致,while语句判断的时候,,就 以为拥护是输入的是 putch(),而不是 'y','Y' ...


5555555555 郁闷啊~~~~~~~~~~
----------------解决方案--------------------------------------------------------

啊~~.
指针应该没有问题吧...


到底是怎么回事呀???


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

啊 ~坚决不要让这个.(我的处女贴)沉下去...


大家帮帮我啊~~~~~~~~~ 呵呵


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

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


typedef struct student /*建立*/
{
int data;
struct student *next;
}students;

students *init() /*初始化*/
{
students *head;
head=(students * )malloc(sizeof(students));
head->next=NULL;
return head ;
}

int main(void)
{
int input( students * );
int print( students * );
students*head;
head=(students*)malloc(sizeof(students));
head=init();
input(head);
print(head);
getch();
}


int input(students *head)
{
int sign;
students*p;
do
{
p=(students*)malloc(sizeof(students));
scanf("%d",&p->data);
head->next=p;
head=p;
printf("wether to inout or not (-1 means end):");
scanf("%d",&sign);
}
while(sign!=-1); /*do-while 结构。*/

head->next=NULL;
return 1;
}


int print(students *head)
{
while(head->next!=NULL)
{
head=head->next;
printf("\n%d",head->data);
}
return 2;
}
我用vc++运行的,没什么错误.


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

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


typedef struct student /*建立*/
{
int data;
struct student *next;
}students;

students *init() /*初始化*/
{
students *head;
head=(students * )malloc(sizeof(students));
head->next=NULL;
return head ;
}

int main(void)
{
int input( students * );
int print( students * );
students *head;
head=(students*)malloc(sizeof(students));
head=init();
input(head);
print(head);
getch();
}


int input(students *head)
{
int sign;
students *p;
printf("wether to inout or not (-1 means end):\n");
do
{ p=(students*)malloc(sizeof(students));
scanf("%d",&p->data);
if(p->data!=-1)
{
head->next=p;
head=p;
}
else break;
}
while(1); /*do-while 结构。*/

head->next=NULL;
return 1;
}


int print(students *head)
{
while(head->next!=NULL)
{
head=head->next;
printf(" %d ",head->data);
}
return 2;
}
这个是才改的,你看看这个啊.


----------------解决方案--------------------------------------------------------
  相关解决方案