• 数据结构。顺序表


    #include <stdio.h>
    #include <stdlib.h>

    #define LIST_INIT_SIZE 100
    #define LIST_INCREMENT 10
    typedef struct Point   //element type
    {
        int x;
        int y;
    }Point;
    typedef Point ElemType;
    typedef struct          //list type
    {
        ElemType *elem;     //data
        int length;         //current length
        int listsize;       //maximum size
    }Sqlist;
    int cpy(ElemType *des,ElemType data)
    {
        des->x=data.x;
        des->y=data.y;
        return 0;
    }
    int elem_cmp(ElemType data1,ElemType data2);
    int list_init(Sqlist *mylist);
    int list_insert(Sqlist *mylist,ElemType data);
    int list_find(Sqlist *mylist,ElemType data);
    int list_delete(Sqlist *mylist,ElemType data);
    int list_disp(Sqlist mylist);
    int main()
    {
        Sqlist mylist;
        list_init(&mylist);
        printf("%d,%d ",mylist.length,mylist.listsize);
        printf("Hello world! ");

        Point tempt;
        tempt.x=11;
        tempt.y=12;

        list_insert(&mylist,tempt);
        list_disp(mylist);
        printf("%d,%d ",mylist.length,mylist.listsize);
        getchar();

        tempt.x=21;
        tempt.y=22;
        list_insert(&mylist,tempt);
        list_disp(mylist);
        printf("%d,%d ",mylist.length,mylist.listsize);
        getchar();

        list_delete(&mylist,tempt);
        list_disp(mylist);
        printf("%d,%d ",mylist.length,mylist.listsize);
        getchar();

        return 0;
    }
    int elem_cmp(ElemType data1,ElemType data2)
    {
        if( (data1.x==data2.x) &&(data1.y==data2.y) )
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    int list_init(Sqlist *mylist)
    {
        mylist->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
        if(mylist->elem!=NULL)
        {
            mylist->length=0;
            mylist->listsize=LIST_INIT_SIZE;

            printf("%d,%d ",mylist->length,mylist->listsize);
            return 0;
        }
        else
        {
            printf("allocate memory is failed! sorry ");
            return 1;
        }
    }
    int list_insert(Sqlist *mylist,ElemType data)
    {
        if(mylist->length>=mylist->listsize)
        {
            //if the memo is not enougth to use,realloc
            mylist->elem=(ElemType*)realloc(mylist->elem,LIST_INIT_SIZE+LIST_INCREMENT);
            if(mylist->elem==NULL)
            {
                printf("memo realloc is failed! sorry,friend ");
                return 1;
            }
            else
            {
                mylist->listsize+=LIST_INCREMENT;
            }
        }
        cpy(&mylist->elem[mylist->length],data);
        mylist->length+=1;
        return 0;
    }
    int list_find(Sqlist *mylist,ElemType data)
    {
        int i=0;
        for(i=0;i<mylist->length;i++)
        {
            if(elem_cmp(mylist->elem[i],data)>0)
            {
                return i+1;
            }
        }
        return 0;
    }
    int list_delete(Sqlist *mylist,ElemType data)
    {
        int i=0,result=list_find(mylist,data);
        if(result==0)
        {
            printf("not found the data ");
            return 1;
        }
        else
        {
            for(i=result-1;i<mylist->length;i++)
            {
                mylist->elem[i]=mylist->elem[i+1];
            }
            mylist->length--;
            return 0;
        }
    }
    int list_disp(Sqlist mylist)
    {
        system("clear");
        int j=0;
        for(j=0;j<mylist.length;j++)
        {
            printf("{%d,%d} ",mylist.elem[j].x,mylist.elem[j].y);
        }
        return 0;
    }

    生活的残酷,让我们习惯了忘记疲倦,一直奔向远方,追寻着自己的梦想。
  • 相关阅读:
    django Highcharts制作图表--显示CPU使用率
    django--ajax的使用,应用
    Selenium&Pytesseract模拟登录+验证码识别
    django Highcharts制作图表--显示CPU使用率
    django--ajax的使用,应用
    斗鱼直播招聘测试总监
    腾讯自动化测试的AI智能
    转载Linq中GroupBy方法的使用总结
    转载.NET 4.0中的泛型的协变和逆变
    转载c#泛型 类型参数的约束(c#编程指南)
  • 原文地址:https://www.cnblogs.com/L-Arikes/p/3898364.html
Copyright © 2020-2023  润新知