zoukankan      html  css  js  c++  java
  • 线性链表

    #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);
    }
  • 相关阅读:
    论面向服务架构及其应用
    一线架构师实践指南阅读笔记03
    一线架构师实践指南阅读笔记02
    一线架构师实践指南Refined Architecture阶段阅读笔记01
    TortoiseGit无法勾选Load Putty Key,该选项为灰色的解决办法
    maya 2015配置openCollada插件
    阿里云ubuntu搭建SVN服务器
    Ventuz配置Leap Motion环境
    Android中将Bitmap对象以PNG格式保存在内部存储中
    Android Studio使用OpenCV后,使APP不安装OpenCV Manager即可运行
  • 原文地址:https://www.cnblogs.com/919czzl/p/4431133.html
Copyright © 2011-2022 走看看