红联Linux门户
Linux帮助

linux下编写链表输入输出无法运行。求助?????

发布时间:2011-07-16 22:21:53来源:红联作者:那伤太虚伪
用ubuntu编写了一个用链表输出,并按大小输出的小程序,用gcc编译时能通过,但是运行时却没反应,求解。。。。。
程序如下、
#include
#include
struct student
{
int num;
char name[20];
int score;
struct student *next;
}*head;


struct student *input()
{

struct student *p1,*p2;
int n=0;
p1=p2=(struct student *)malloc(sizeof(struct student));
if(p1=NULL)
printf("the memery is failed\n");
else
{
printf("input the number:\n");
scanf("%d",&p1->num);
printf("input the name:\n");
scanf("%s",p1->name);
printf("input the score:\n");
scanf("%d",&p1->score);
}
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(sizeof(struct student));
if(p1=NULL)
printf("the memery is failed\n");
else
{
printf("input the number:\n");
scanf("%d",&p1->num);
printf("input the name:\n");
scanf("%s",p1->name);
printf("input the score:\n");
scanf("%d",&p1->score);
}

}
p2->next=NULL;
return(head);
}

struct student *selector(struct student *head)
{
struct student *first,*tail;
struct student *p,*min;
first=NULL;
while(head!=NULL)
{
min=head;
for(p=head;p->next!=NULL;p=p->next)
{
if(p->next->numnum)
min=p->next;
}
if(first==NULL)
{
first=min;
tail=min;
}
else
{
tail->next=min;
tail=min;
}
if(min=head)
head=head->next;
else
p->next=min->next;
}
if(first!=NULL)
tail->next=NULL;
head=first;
return head;
}

int main()
{
struct student *input();


struct student *selector(struct student *head);

while(head!=NULL)
{
printf("the num is:%4d,the name is:%10s,the score is:%4d",head->num,head->name,head->score);
head=head->next;
}

}
文章评论

共有 5 条评论

  1. yuankangjian 于 2011-09-12 18:40:34发表:

    这是什么啊

  2. icemou 于 2011-08-28 18:42:14发表:

    楼主,函数怎么调用都没搞清楚么?

  3. lijiang 于 2011-07-21 13:03:21发表:

    [i=s] 本帖最后由 lijiang 于 2011-7-21 13:06 编辑 [/i]

    最明显的错误就像楼上说的主函数中没有正确的调用子函数 !想搞清楚仔细学学谭浩强版的c程序设计!

  4. hml1006 于 2011-07-19 12:08:30发表:

    struct student *input();
    struct student *selector(struct student *head);
    这是声明函数,不是调用函数。

  5. 那伤太虚伪 于 2011-07-17 10:44:08发表:

    没有大虾吗?自己顶下了??????