| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 462 人关注过本帖
标题:为什么链表老是出问题,一运行就退不出来了
只看楼主 加入收藏
liuxiaodong
Rank: 1
等 级:新手上路
帖 子:23
专家分:6
注 册:2010-3-26
结帖率:42.86%
收藏
已结贴  问题点数:15 回复次数:2 
为什么链表老是出问题,一运行就退不出来了
#include <stdio.h>
#define NULL 0
struct student
{
    char name[20];
    char sex;
    int num, year;
    struct student *next;
};
struct student *input(struct student *head)
{
    int n = 0;
    struct student *p1, *p2;
    p1 = p2 = (struct student*) malloc (sizeof(struct student));
    printf ("please enter information:");
    scanf ("%s%d%c%d", p1->name, &p1->num, &p1->sex, &p1->year);
    while (p1->num > 0)
    {
        n++;
        if (n == 1)
        {
            head = p1;
        }
        else
        {
            p2->next = p1;
            p2 = p1;
        }
        p1 = (struct student*) malloc (sizeof(struct student));
        scanf ("%s%d%c%d", p1->name, &p1->num, &p1->sex, &p1->year);
    }
    p2->next = NULL;
    return (head);
}
void output (struct student *head)
{
    struct student *p;
    p = head;
    while (p != NULL)
    {
        printf ("%s     %d     %c     %d", p->name, p->num, p->sex, p->year);
        p = p->next;
    }
}
void main ()
{
    struct student *head;
    head = NULL;
    head = input (head);
    output (head);
}
搜索更多相关主题的帖子: 链表 运行 
2010-05-29 12:32
LUNNAN
Rank: 2
等 级:论坛游民
帖 子:30
专家分:44
注 册:2010-5-23
收藏
得分:7 
程序代码:
  if (n == 1)
        {
            head = p1;
            p2=p1;                //在这加个,否则的化,你的程序在第一个结点后面就连不起来!
        }
2010-05-29 13:10
流行语
Rank: 2
等 级:论坛游民
帖 子:8
专家分:19
注 册:2010-5-27
收藏
得分:7 
#include <stdio.h>
#include <malloc.h>
#define NULL 0
struct student
{
    char name[20];
    char sex[10];
    int num, year;
    struct student *next;
};
struct student *input(struct student *head)
{
    int n = 0;
    struct student *p1, *p2;
    p1 = p2 = (struct student*) malloc (sizeof(struct student));
    printf ("please enter information:name,num,sex,year");
    printf("\n");
    scanf ("%s %d %s %d", p1->name, &p1->num, p1->sex, &p1->year);
    while (p1->num > 0)
    {
        n++;
        if (n == 1)
        {
            head = p1;
        }
        else
        {
            p2->next = p1;
            p2 = p1;
        }
        p1 = (struct student*) malloc (sizeof(struct student));
        printf ("please enter information:name,num,sex,year");
        printf("\n");
        scanf ("%s %d %s %d", p1->name, &p1->num, p1->sex, &p1->year);
    }
    p2->next=p1;
    p2=p1;
    p2->next = NULL;
    return (head);
}
void output (struct student *head)
{
    struct student *p;
    p = head;
    printf("information is:");
    while (p ->next!= NULL)
    {
        printf("\n");
        printf ("%s     %d     %s     %d", p->name, p->num, p->sex, p->year);
        p = p->next;
        printf("\n");
    }
}
void main ()
{
    struct student *head;
    head = NULL;
    head = input (head);
    output (head);
}
我给你改了一下,不过还有一些不足之处,我希望你能尽量完善它。
你说的那个问题,我看了一下,你没有把最后一个和倒数第二个连起来,所以应在while循环外面再加点东西,使他们连起来。

[ 本帖最后由 流行语 于 2010-5-29 16:55 编辑 ]
2010-05-29 16:53
快速回复:为什么链表老是出问题,一运行就退不出来了
数据加载中...
 
   



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

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