• 数据结构-顺序表


    #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);
    }
    
    
    

  • 相关阅读:
    Centos安装mysql
    @autowired注解报错原因及解决办法
    注解@Resource与@Autowired的区别
    SpringBoot三种获取Request和Response的方法
    oss 上传图片、下载 中文名称
    git tags 和 Branches的区别是什么呀,什么时候应该创建一个Tag?
    git使用命令,git checkout -b a 与 git branch a区别
    PostgreSQL-With子句实现递归
    redisson锁 tryLock的使用及正确用法
    mysql any 和in 函数的使用
  • 原文地址:https://www.cnblogs.com/chinashenkai/p/9451398.html
Copyright © 2020-2023  润新知