外设天下 - 电脑外设发烧友聚集地

12下一页
我的人缘0

帮忙做个C题目···

18 7023
楼主
跳转到指定楼层
发表于 2008-10-26 11:39 只看该作者 倒序浏览 阅读模式
#include<malloc.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
struct student
{
        int num;                  /*学号*/
        float score;              /*成绩*/
        struct student *next;     /*指针域*/
};
#define LEN sizeof(struct student)

struct student *create()     /*此函数返回一个指向链表的头结点*/
{
        struct student *head;     /*指向student类型变量的指针head为头指针*/
        struct student *p,*tail;  /*tail指向链表末尾元素,p指向新分配的结点*/
        float temp;               /*临时数据变量*/
        head=NULL;
        do
        {
                p=(struct student *)malloc(sizeof(struct student));/*分配新结点*/
                printf("Number Score:");
                fflush(stdin);          /*清除输入缓冲区*/
                scanf("%d  %f",&p->num,&temp);
                if(p->num==0)
                {
                        free(p);
                        break;
                };
                p->score=temp;          /*取得结点数据*/
                p->next=NULL;
                if(head==NULL)
                {
                        head=p;
                        tail=p;
                }
                else
                {
                        tail->next=p;
                        tail=p;       /*指向尾结点*/
                }
        }while(p->num!=0);
        return(head);
}
void display(struct student *head)
{
        struct student *p;
        p=head;
        while(p!=NULL)
        {
                printf("%4d  %5.1f\n",p->num,p->score);
                p=p->next;
        }
}
struct student *insert(struct student *head,struct student *new)
{
        struct student *p,*q;
        if(head==NULL)
                head=new;
        else
        {
                if(head->num>=new->num)
                {
                        new->next=head;
                        head=new;
                }/*新结点插在链首*/
                else
                {
                        p=head;
                        while(p->num<new->num&&p->next!=NULL)
                        {
                                q=p;
                                p=p->next;
                        }
                        if(p->num>new->num)
                        {
                                q->next=new;
                                new->next=p;
                        }
                        else
                        {
                                p->next=new;
                                new->next=NULL;
                        }
                }
        }
        return(head);
}
struct student *delete(struct student *head,int num)
/*在头为head的单链表中将指定的学号为num的结点删除*/
/*算法返回新的链表的表头结点*/
{
        struct student *p1,*p2;  /*设置两个指针,用以指示删除位置*/
        if(head==NULL)
                printf("\nList null\n");
        else
        {
                p1=head;
                while(num!=p1->num&&p1->next!=NULL)
                /*p1所指的结点不是要删除的结点,且其后还有结点*/
                {
                        p2=p1;
                        p1=p1->next;/*后移一个结点*/
                }
                if(num==p1->num)/*找到了要删除的结点*/
                {
                        if(p1==head)
                                head=p1->next;                /*若p1所指为第一个结点,则把第二个结点的地址赋给head*/
                        else
                                p2->next=p1->next;        /*否则将下一个结点的地址赋给前一个结点的指针域*/
                        printf("delete:%4d\n",num);
                        free(p1);
                }
                else
                        printf("%4d not been found!\n",num);
        }
        return(head);
}

void main()
{
        struct student *head,*stu;
        float temp;
        int number;
        printf("Input records:\n");
        head=create();
        display(head);
        stu=(struct student *)malloc(LEN);
        printf("Input the insert Number Score:");
        scanf("%d %f",&stu->num,&temp);
        stu->score=temp;
        stu->next=NULL;
        head=insert(head,stu);
        display(head);
        printf("\nInput the number to delete:");
        scanf("%4d",&number);
        head=delete(head,number);
        display(head);
        getch();
}
修改一下···功能基本相同···哎···自己做了N小时还没出来
0
已赞
微信分享 收藏
回复

使用道具 举报

我的人缘0
2
发表于 2008-10-26 11:47 只看该作者
日宝贝据说是搞这个的
我的人缘0
3
发表于 2008-10-26 11:54 只看该作者
:shui: :shui: 哥已经把这个都拌饭吃了。。。
我的人缘0
4
发表于 2008-10-26 12:00 只看该作者
C好久没搞了 都在弄JAVA,。。。。。。。。。。
我的人缘0
5
发表于 2008-10-26 12:03 只看该作者
感觉这个程序很不错了···不需要改动了
我的人缘0
6
发表于 2008-10-26 12:15 只看该作者
我是做.NET的,C已经不整了
我的人缘0
7
发表于 2008-10-26 12:28 只看该作者
哎,不是计算机系的...C语言基础在大一学过,早还给老师了...
我的人缘0
8
发表于 2008-10-26 12:33 只看该作者
:lg: :lg:
我的人缘0
9
发表于 2008-10-26 13:14 只看该作者
:lg: :lg: :lg: 。
我的人缘0
10
发表于 2008-10-26 13:17 只看该作者
测试
不懂……
12下一页
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则