• 线性链表


    #include <stdio.h>
    #include <stdlib.h>
    
    struct Grade_Info
    {
        int score;
        struct Grade_Info *next;
    };
    
    typedef struct Grade_Info NODE;
    
    NODE *Create_LinkList();
    void Insert_LinkList (NODE *head,NODE *pnew,int i);
    void Delete_LinkList (NODE *head,int i);
    void Display_LinkList(NODE *head);
    void Free_LinkList (NODE *head);
    
    void main()
    {
        NODE *head,*pnew;
        head=Create_LinkList();
        if(head==NULL)
            return;
        printf("after create:");
        Display_LinkList(head);
    
        pnew=(NODE*)malloc(sizeof(NODE));
        if(pnew==NULL)
        {
            printf("no enough memory!
    ");
            return ;
        }
        pnew->score=88;
        Insert_LinkList(head,pnew,3);
        printf("after insert:");
        Display_LinkList(head);
    
        Delete_LinkList(head,3);
        printf("after delete:");
        Display_LinkList(head);
    
        Free_LinkList(head);
    }
    
    NODE *Create_LinkList()
    {
        NODE *head,*tail,*pnew;
        int score;
    
        head=(NODE*)malloc(sizeof(NODE));
        if(head==NULL)
        {
            printf("no enough memory!
    ");
            return (NULL);
        }
        head->next=NULL;
        tail=head;
    
        printf("input the score of students:
    ");
        while(1)
        {
            scanf("%d",&score);
            if(score<0)
                break;
            pnew=(NODE*)malloc(sizeof(NODE));
            if(pnew==NULL)
            {
                printf("no enough memory!
    ");
                return (NULL);
            }
            pnew->score=score;
            pnew->next=NULL;
    
            tail->next=pnew;
            tail=pnew;
        }
        return (head);
    }
    
    void Insert_LinkList(NODE*head,NODE*pnew,int i)
    {
        NODE *p;
        int j;
    
        p=head;
        for(j=0;j<i&&p!=NULL;j++)
            p=p->next;
        if(p==NULL)
        {
            printf("the %d node not found!
    ",i);
            return ;
        }
    
        pnew->next=p->next;
        p->next=pnew;
    }
    
    void Delete_LinkList(NODE *head,int i)
    {
        NODE *p,*q;
        int j;
    
        if(i==0)
            return ;
    
        p=head;
        for(j=1;j<i&&p->next!=NULL;j++)
            p=p->next;
        if(p->next==NULL)
        {
            printf("the %d node not found!
    ",i);
            return ;
        }
    
        q=p->next;
        p->next=q->next;
        free(q);
    }
    
    void Display_LinkList(NODE *head)
    {
        NODE *p;
    
        for(p=head->next;p!=NULL;p=p->next)
            printf("%d  ",p->score);
        printf("
    ");
    }
    
    void Free_LinkList(NODE *head)
    {
        NODE *p,*q;
    
        p=head;
        while(p->next!=NULL)
        {
            q=p->next;
            p->next=q->next;
            free(q);
        }
        free(head);
    }
  • 相关阅读:
    ASP.NET和PHP全面对比
    GridView事件DataBinding,DataBound,RowCreated,RowDataBound区别及执行顺序分析
    OA、CRM、ERP之间的区别和联系是什么?
    C#继承
    对软件项目管理的几点认识
    冒泡
    经典排序算法
    asp.net遍历页面中所有TextBox,并赋值为String.Empty的方法
    String.Format用法
    frame和iframe
  • 原文地址:https://www.cnblogs.com/919czzl/p/4431133.html
Copyright © 2020-2023  润新知