c语言实现列表创建 删除 和插入

本文介绍了一个使用C语言实现的简单链表操作案例,包括创建、打印、删除节点及插入新节点等功能,并提供了完整的代码示例。

yuanzhen@debian:~/C_script$ cat simple_list.c 
#include <stdio.h>
#include <malloc.h>
//#define LEN sizeof(struct tag_student)
#define LEN sizeof(student)

typedef struct tag_student
{
    int number;
    float score;
    struct tag_student *next;
} student;

int m=0;

student *create()
{
    //int m=0;
    student *head, *data, *final;
    data=(student *)malloc(LEN);
    scanf("%d,%f",&data->number, &data->score);
    head=NULL;
    while(data->number!=0 )
    {
        m=m+1;
        if(m==1)
            head=data;
        else
        {
            final->next=data;
        }
        final=data;
        data=(student *)malloc(LEN);
        scanf("%d,%f",&data->number, &data->score);
    }
    final->next=NULL;
    printf("The list's length is %d.\n", m);
    return head;
}

void print(student *head)
{
    student *point;
    point=head;
    if(head)
    {
        do
        {
            printf("The student %d's score is %.2f\n",point->number, point->score);
            point=point->next;
        }
        while(point!=NULL);
    }
    else printf("the list is zero\n");
}

student *del(student *head, int num)
{
    student *data, *final;
    if(!head)
        printf("\nthis is a zero list!!\n");
    else
    {
        data=head;
        while(num!=data->number && data->next!=NULL)
        {
            final=data;
            data=data->next;
        }
        if(num==data->number)
        {
            if(data==head)head=head->next;// or head=data->next;
            else
            {
                final->next=data->next;
            }
            m=m-1;
            printf("delete: %d\n",num);
        }
        else
            printf("there is no this num!!\n");
    }
    return head;
}

student *insert(student *head, int num, float sco)
{
    student *data, *final, *temp;
    int i;

    if(!head) 
    {
        //printf("\nthis is a zero list\n");
        data=(student *)malloc(LEN);
        data->number=num;
        data->score=sco;
        data->next=NULL;
        head=data;
        m=m+1;
        return head;
    }
    else
    {
        data=head;
        if(num<head->number)
        {
            data=(student *)malloc(LEN);
            data->number=num;
            data->score=sco;
            data->next=head;
            m=m+1;
            head=data;
            return head;
        }
        else
        {
            //printf("000000000000000000000");
            for(i=0;i<m;i++)
            {
                printf("print %d,%d\n",i,num);
                final=data;
                data=data->next;
                if(!data)
                {
                    data=(student *)malloc(LEN);
                    data->number=num;
                    data->score=sco;
                    data->next=NULL;
                    final->next=data;
                    m=m+1;
                    return head;
                }
                else
                {
                    if(num>=final->number && num<data->number)
                    {
                        temp=(student *)malloc(LEN);
                        temp->number=num;
                        temp->score=sco;
                        temp->next=data;
                        final->next=temp;
                        m=m+1;
                        return head;
                    }
                }
            }
        }
    }
}
void main()
{
    student *head, *data, *final;
    int delnum;
    int num_in;
    float sco_in;

    head=create();
    print(head);
    printf("m is %d!!!",m);
    
    printf("\nPlease, input the num you hope to delete!!\n");
    scanf("%d",&delnum);
    head=del(head, delnum);
    print(head);
    printf("m is %d!!!",m);

    printf("\nPlease, input the student data:\n");
    scanf("%d,%f",&num_in, &sco_in);
    //scanf("%d,%f",&data->number, &data->score);
    head=insert(head, num_in, sco_in);
    //head=insert(head, data->number, data->score);
    print(head);
    printf("m is %d!!!",m);

}
 

转载于:https://my.oschina.net/lCQ3FC3/blog/745480

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值