• 数据结构-顺序表


    #include <stdio.h>
    #include <stdlib.h>
    #define LIST_INIT_SIZE 10
    #define LISTINCREMENT 100
    #define TRUE 1
    #define FALSE 0
    #define OK 1
    #define ERROR 0
    #define INFEASIBLE -1
    #define OVERFLOW -2
    
    typedef int status;
    typedef int ElemType;
    
    typedef struct
    {
        ElemType *elem;
        int length;
        int listsize;
    } SqList;
    
    status InitList(SqList *L);                         //构造空线性表
    status ListInsert_Sq(SqList *L, int i, ElemType e); //线性表i位置前插入新元素e
    status ListDelete_Sq(SqList *L, int i, ElemType *e);//线性表i位置元素删除,用e返回其值
    status DestroyList(SqList *L);                      //销毁线性表
    status ClearList(SqList *L);                        //制空线性表
    status ListEmpty(SqList L);                         //线性表判空,空为true
    status ListLength(SqList L);                        //线性表长度
    status GetElem(SqList L, int i, ElemType *e);       //用e返回线性表i位置的元素值
    status LocateElem(SqList L, ElemType e,status *compare(ElemType a,ElemType b));     //返回第一个和e满足compare关系的元素位置
    status PriorElem(SqList L, ElemType cur_e, ElemType *pre_e);                        //用pre_e返回e 的前驱
    status NextElem(SqList L, ElemType cur_e, ElemType *next_e);                        //用next_e返回e的后继
    status ListTraverse(SqList L,status visit(ElemType a));                             //使用visit遍历线性表
    status Equal(ElemType a , ElemType b);
    status visit(ElemType a);
    
    int main()
    {
        int i = 0;
        ElemType e;
        SqList list;
        InitList(&list);
        printf("After carry out InitList(&list) the length of list: %d
    ",ListLength(list));
        for(i = 1 ; i < 9 ; i++)
            ListInsert_Sq(&list , 1 , i);
        printf("After carry out 
    for(i = 0 ; i < 8 ; i++)
    	ListInsert(&list , 1 ,i) 
    the length of list: %d
    ",ListLength(list));
        if(ListEmpty(list))
            printf("At present the list is empty
    ");
        else printf("At present the list is't empty
    ");
        GetElem(list,1,&e);
        printf("After carry out GetElem(list,1,&e); the value of e is : %d
    ", e);
        printf("The Equal() is used to determine the equality of the function
    After carry out LocateElem(list , 3 , Equal)");
        printf("The value it returned is %d
    ",LocateElem(list , 3 , Equal));
        printf("After carry out ListTraverse(list , visit)
    ");
        ListTraverse(list , visit);
        return 0;
    }
    
    status DestroyList(SqList *L)
    {
        free(L->elem);
        L->length=0;
        L->listsize=0;
        L->elem= NULL;
        return OK;
    }
    
    status ClearList(SqList *L)
    {
        L->length=0;
        return OK;
    }
    
    status ListEmpty(SqList L)
    {
        if(0 == L.length)
            return TRUE;
        return FALSE;
    }
    
    status ListLength(SqList L)
    {
        return L.length;
    }
    
    status GetElem(SqList L, int i, ElemType *e)
    {
        if(i < 1 || i > L.length)
            return ERROR;
        *e = *(L.elem + i -1);
        return OK;
    }
    
    status LocateElem(SqList L, ElemType e,status *compare(ElemType a,ElemType b))
    {
        int i=0;
        ElemType *p = L.elem;
        while(i < L.length && !compare(e, *p++))
            i++;
        if(i <= L.length)
            return i;
        return 0;
    }
    
    status PriorElem(SqList L, ElemType cur_e, ElemType *pre_e)
    {
        ElemType *p = L.elem;
        int i = 1;
        while(1)
            if(*++p == cur_e && ++i <= L.length)
            {
                *pre_e = *--p;
                return OK;
            }
        return ERROR;
    }
    
    status NextElem(SqList L, ElemType cur_e, ElemType *next_e)
    {
        ElemType *p = L.elem;
        int i = 1;
        while(1)
            if(*p++ == cur_e && i++ <= L.length)
            {
                *next_e = *++p;
                return OK;
            }
        return ERROR;
    }
    
    status ListTraverse(SqList L, status visit(ElemType a))
    {
        int i = 0;
        ElemType *p = L.elem;
        while(i++ < L.length)
        {
            visit(*p++);
        }
    }
    
    status InitList(SqList *L)
    {
        L->elem = (ElemType *) malloc(LIST_INIT_SIZE * sizeof(ElemType));
        if (!L->elem)
            exit(OVERFLOW);
        L->length = 0;
        L->listsize = LIST_INIT_SIZE;
        return OK;
    }
    
    status ListInsert_Sq(SqList *L, int i, ElemType e)
    {
        if (i < 1 || i > L->length + 1)
            return ERROR;
        if (L->length >= L->listsize)
        {
            ElemType *newbase;
            newbase = (ElemType *)realloc(L->elem, (L->listsize + LISTINCREMENT) * sizeof(ElemType)) ;
            if  (!newbase)
                exit (OVERFLOW);
            L->elem = newbase;
            L->listsize = L->listsize + LISTINCREMENT;
        }
        ElemType *q,*p;
        q = &(L->elem[i-1]);
        for (p = &(L->elem[L->length-1]); p >= q; --p) *(p + 1) = *p;
        *q = e;
        L->length++;
        return OK;
    }
    
    status ListDelete_Sq(SqList *L, int i, ElemType *e)
    {
        ElemType *q,*p;
        if (i < 1 || i > L->length)
            return ERROR;
        p = &L->elem[i-1];
        e = *p;
        q = L->elem + L->length - 1;
        for (++p ; p <= q ; ++p)
            *(p - 1) = *p;
        --L->length;
        return OK;
    }
    
    status Equal(ElemType a, ElemType b)
    {
        if(a == b)
            return 1;
        return 0;
    }
    
    status visit(ElemType a)
    {
        printf("visit : %d 
    ",a);
    }
    
    
    

  • 相关阅读:
    Densely Connected Convolutional Networks 论文阅读
    仙剑美丽的背景图片
    欢迎follow github:https://github.com/wuxiangli91
    L1和L2特征的适用场景
    决策树和adaboost
    Dropout caffe源码
    destoon 短信发送函数及短信接口修改
    destoon后台权限-不给客户创始人权限并屏蔽部分功能
    destoon 数据库操作
    destoon修改笔记
  • 原文地址:https://www.cnblogs.com/chinashenkai/p/9451398.html
Copyright © 2020-2023  润新知