Chinaunix首页 | 论坛 | 博客
  • 博客访问: 97272
  • 博文数量: 23
  • 博客积分: 516
  • 博客等级: 中士
  • 技术积分: 315
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-29 16:45
文章分类
文章存档

2012年(11)

2011年(12)

分类: C/C++

2011-11-10 23:20:13

#include
#include

#define LEN sizeof(TypeStu)

typedef struct student
{
    long num;
    float score;
    struct student *next;
}TypeStu;

int n;

TypeStu *creat(void)
{
    TypeStu *head = NULL;
    TypeStu *p1,*p2;
    n = 0;
    if((p2=p1=(TypeStu *)malloc(LEN)) == NULL)
    {
        printf("malloc memory failure!\n");
        return head;
    }

    scanf("%ld,%f",&p1->num,&p1->score);

    while(p1->num!=0)
    {
        n ;
        if(n==1) head = p1;
        else p2->next = p1;
        p2 = p1;
        p1 = (TypeStu *)malloc(LEN);
        scanf("%ld,%f",&p1->num,&p1->score);
    }
    p2->next = NULL;
    return head;
}

TypeStu *del(TypeStu *head,long num)
{
    TypeStu *p1,*p2;
    if(head == NULL)
    {
        printf("\nlist null! \n");
        return head;    
    }
    p1 = head;
    while(num != p1->num && p1->next != NULL)
    {
        p2 = p1;
        p1 = p1->next;
    }
    if(num == p1->num)
    {
        if(p1 == head) head = p1->next;
        else p2->next = p1->next;
        printf("delete:%ld\ n",num);
        n = n-1;
    }
    else
    {
        printf("%ld not been found! \n",num);
    }
    return head;
}

TypeStu * insert(TypeStu *head,TypeStu *stud)
{
    TypeStu *p0,*p1,*p2;
    p1 = head;
    p0 = stud;
    
    if(head == NULL)
    {
        head = p0;
        p0->next = NULL;
    }
    else
    {
        while((p0->num > p1->num) && (p1->next != NULL))
        {
            p2 = p1;
            p1 = p1->next;
        }
        if(p0->num <= p1->num)
        {
            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(TypeStu *head)
{
    TypeStu *p;
    printf("\nNow,These %d records are:\n",n);
    p = head;
    if(head!=NULL)
    {
        while(p!=NULL)
        {
            printf("%ld %f \n",p->num,p->score);
            p = p->next;        
        }
    }
}

void print_help()
{
    printf("func = 1  ------>    call del function\n");
    printf("func = 2  ------>    call insert function\n");
}

void main()
{
    TypeStu *p_head,*stu;
    int func,Flg_exit = 0;
    long del_num;
    printf("input records:\n");
    p_head = creat();
    print(p_head);

    print_help();
    printf("input operate function:");
    scanf("%d",&func);
    while(!Flg_exit)
    {
        switch(func)
        {
            case 1:
                printf("\ninput the deleted number:");
                scanf("%ld",&del_num);
                while(del_num!=0)
                {
                    p_head = del(p_head,del_num);    
                    print(p_head);    
                    printf("input the deleted number:");
                    scanf("%ld",&del_num);
                }
            break;
            
            case 2:
                printf("\ninput the inserted record:");
                stu = (TypeStu *)malloc(LEN);
                scanf("%ld,%f",&stu->num,&stu->score);
                while(stu->num != 0)
                {
                    p_head = insert(p_head,stu);
                    print(p_head);
                    printf("input the inserted record:");
                    stu = (TypeStu *)malloc(LEN);
                    scanf("%ld,%f",&stu->num,&stu->score);
                }
            break;

            default:
                printf("\nhave not the function and procedure exit\n\n");    
                Flg_exit = 1;
            break;
                
        }
        if(!Flg_exit)
        {
            printf("\ninput operate function:");
                scanf("%d",&func);
        }
    }
}


运行结果:
input records:
89,96
90,95
91,92
93,94
0

Now,These 4 records are:
89 96.000000
90 95.000000
91 92.000000
93 94.000000
func = 1  ------>    call del function
func = 2  ------>    call insert function
input operate function:1

input the deleted number:89
delete:89 n
Now,These 3 records are:
90 95.000000
91 92.000000
93 94.000000
input the deleted number:93
delete:93 n
Now,These 2 records are:
90 95.000000
91 92.000000
input the deleted number:0

input operate function:2

input the inserted record:89,96

Now,These 3 records are:
89 96.000000
90 95.000000
91 92.000000
input the inserted record:93,94

Now,These 4 records are:
89 96.000000
90 95.000000
91 92.000000
93 94.000000
input the inserted record:878,96

Now,These 5 records are:
89 96.000000
90 95.000000
91 92.000000
93 94.000000
878 96.000000
input the inserted record:0

input operate function:1

input the deleted number:878
delete:878 n
Now,These 4 records are:
89 96.000000
90 95.000000
91 92.000000
93 94.000000
input the deleted number:7
7 not been found!

Now,These 4 records are:
89 96.000000
90 95.000000
91 92.000000
93 94.000000
input the deleted number:0

input operate function:7

have not the function and procedure exit




注:此程序摘自谭浩强《C程序设计》,只做部分修改。











阅读(1115) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~