| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 606 人关注过本帖
标题:链表的问题?
只看楼主 加入收藏
sunmingchun
Rank: 4
来 自:安徽-滁州
等 级:业余侠客
帖 子:198
专家分:277
注 册:2010-4-2
结帖率:84%
收藏
已结贴  问题点数:20 回复次数:2 
链表的问题?
我这个程序没错误怎么输入不了内容啊!请教各位大侠帮我看看啊  
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN  sizeof(struct student)
struct student
{
    int number;
    char name[10];
    char sex;
    int age;
    struct student *next;
};
int n;
struct student *creat()
{
    struct student *p1,*p2,*head;
    n=0;
    p1=p2=(struct student *)malloc(LEN);
    scanf("%d,%s,%c,%d",&p1->number,p1->name,&p1->sex,&p1->age);
    head=NULL;
    while(p1->age!=0)
    {
        n=n+1;
        if(n==1)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
        p1=(struct student *)malloc(LEN);
        scanf("%d,%s,%c,%d",&p1->number,p1->name,&p1->sex,&p1->age);
    }
    p2->next=NULL;
    return head;
}
struct student *del(struct student *head,int age)
{
    struct student *p1,*p2;
    if(head==NULL)
    {
        printf("list null\n");
        return head;
    }
    p1=head;
    while(age!=p1->age && p1->next!=NULL)
    {
        p2=p1;
        p1=p1->next;
    }
    if(age==p1->age)
    {
        if(p1==head)
            head=p1->next;
        else
            p2->next=p1->next;
        n=n-1;
    }
    else
        printf("%d not been found:\n",age);
    return head;
}
struct student *insert(struct student *head,struct student *stu)
{
    struct student *p1,*p2,*p0;
    p0=stu;
    p1=head;
    if(head==NULL)
    {
        head=p0;
    p0->next=NULL;
    }
    else
    {
        while(p1->number<p0->number && p1->next!=NULL)
        {
            p2=p1;
            p1=p1->next;
        }
        if(p0->number==p1->number)
        {
            if(head==p1)
                head=p0;
            else
            {
                p2->next=p0;
            p0->next=p1;
            }
        }
        else
        {
            p1->next=p0;
            p0->next=NULL;
        }
        n=n+1;
    }
    return head;
}
void print(struct student *head)
{
    struct student *p,*q;
    p=head;
    while(p->next!=NULL)
    {
        printf("%d %s %c %d",p->number,p->name,p->sex,p->age);
        q=p;
        p=p->next;
    }
}
void main()
{
    struct student *head,*stu;
    int age;
    head=creat();
    print(head);
    scanf("%d",&age);
    while(age!=0)
    {
        head=del(head,age);
        print(head);
        printf(" input the deleted age:\n");
        scanf("%d",&age);
    }
    printf("input the insert nunber:\n");
    stu=(struct student*)malloc(LEN);
    scanf("%d,%s,%c,%d",&stu->number,stu->name,&stu->sex,&stu->age);
    while(stu->age!=0)
    {
        head=insert(head,stu);
        print(head);
        stu=(struct student *)malloc(LEN);
        scanf("%d,%s,%c,%d",&stu->number,stu->name,&stu->sex,&stu->age);
    }
}
搜索更多相关主题的帖子: 链表 
2010-06-09 18:14
lijm1989
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:珠海
等 级:贵宾
威 望:12
帖 子:675
专家分:2844
注 册:2009-10-14
收藏
得分:14 
什么代码的说明都没有~~~
2010-06-11 15:38
aitajiujiage
Rank: 2
等 级:论坛游民
帖 子:21
专家分:34
注 册:2010-5-10
收藏
得分:0 
我说老兄啊 你找别人帮忙最起码得附上说明吧!也好让别人看明白点啊
2010-06-29 20:57
快速回复:链表的问题?
数据加载中...
 
   



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

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