数据结构---线性表---顺序存储结构

头文件

header.h

#ifndef _LIST_H
#define _LIST_H

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

#define MY_TYPE int   /*0:int 1:double 2:float 3:char*/
#define _MY_TYPE_ 0
#define LIST_INIT_SIZE 3   /*单位:MY_TYPE*/
#define LIST_INCRE_SIZE 2   /*单位:MY_TYPE*/

typedef enum status {ERROR, OK} St; /*0  1*/

typedef struct list_array{
    MY_TYPE *head_pointer;  /*指向存储空间基址*/
    int length;             /*当前长度*/
    int listsize;           /*当前分配的存储容量*/
}ALIST;

typedef St (*compare)(const MY_TYPE, const MY_TYPE);
/*初始化*/
St init_alist(ALIST *Lpointer);
/*销毁*/
St destroy_alist(ALIST *Lpointer);
/*清空*/
St clear_alist(ALIST *Lpointer);
/*判断线性表是否为空*/
St alist_empty(const ALIST *Lpointer);
/*表长*/
int alist_length(ALIST *Lpointer);
/*取给定位置的元素用*pe返回*/
St get_elem(const ALIST *Lpointer, const int i, MY_TYPE *pe);
/*查看满足compare关系的元素的位置*/                      /*pc简单设定为大于小于等于*/
int locate_elem(const ALIST *Lpointer, const MY_TYPE e, compare pc);
/*取某个元素的前驱*/
St prior_elem(const ALIST *Lpointer, const MY_TYPE cur_e, MY_TYPE *ppre_e);
/*取某个元素的后继*/
St next_elem(const ALIST *Lpointer, const MY_TYPE cur_e, MY_TYPE *ppre_e);
/*在数组给定下标为i的位置插入e*/
St alist_insert(ALIST *Lpointer, const int i, const MY_TYPE e);
/*增加一个元素在表尾*/
St alist_add(ALIST *Lpointer, MY_TYPE e);
/*在数组给定下标为i的位置删除此元素用*P_e返回*/
St alist_delete(ALIST *Lpointer, const int i, MY_TYPE *p_e);
/*修改指定位置的元素*/
St alist_mod(ALIST *Lpointer, const int i, MY_TYPE mod_e);
/*遍历线性表*/
St alist_visit(const ALIST *Lpointer);

/*compare--->equal*/
St cmp_equal(MY_TYPE e1, MY_TYPE e2);
/*compare--->less*/
St cmp_less(MY_TYPE e1, MY_TYPE e2);
/*compare--->more*/
St cmp_more(MY_TYPE e1, MY_TYPE e2);

#endif /* _LIST_H */

顺序表基本操作

初始化

/*初始化*/
St init_alist(ALIST *Lpointer){
    Lpointer->head_pointer = (MY_TYPE *)calloc(LIST_INIT_SIZE, sizeof(MY_TYPE));
    if(!Lpointer->head_pointer){
        return ERROR;
    }
    Lpointer->length = 0;
    Lpointer->listsize = LIST_INIT_SIZE;
    return OK;
}

销毁

/*销毁*/
St destroy_alist(ALIST *Lpointer){
    Lpointer->length = 0;
    Lpointer->listsize = 0;
    free(Lpointer->head_pointer);
    Lpointer->head_pointer = NULL;
    return OK;
}

 清空

/*清空*/
St clear_alist(ALIST *Lpointer){
    Lpointer->length = 0;
    memset(Lpointer->head_pointer, 0, Lpointer->listsize);
    return OK;
}

判断线性表是否为空

/*判断线性表是否为空*/
St alist_empty(const ALIST *Lpointer){
    if(Lpointer->length)
        return ERROR;
    else
        return OK;
}

表长

/*表长*/
int alist_length(ALIST *Lpointer){
    return (Lpointer->length);
}

取给定位置的元素用*pe返回

/*取给定位置的元素用*pe返回*/
St get_elem(const ALIST *Lpointer, const int i, MY_TYPE *pe){
    if(i > -1 && i < Lpointer->length){
        *pe = *(Lpointer->head_pointer + i);
        return OK;
    }else{
        return ERROR;
    }
}

查看满足compare关系的元素的位置

/*查看满足compare关系的元素的位置*/                      /*pc简单设定为大于小于等于*/
int locate_elem(const ALIST *Lpointer, const MY_TYPE e, compare pc){
    int i = 0;
    for(; i < Lpointer->length; ++i){
        if((*pc)(*(Lpointer->head_pointer + i), e))
            break;
    }
    if(i < Lpointer->length)
        return i;
    else
        return -1;
}

取某个元素的前驱

/*取某个元素的前驱*/
St prior_elem(const ALIST *Lpointer, const MY_TYPE cur_e, MY_TYPE *ppre_e){
    int i = 0;
    for(; i < Lpointer->length; ++i){
        if(cur_e == *(Lpointer->head_pointer + i)){
            break;
        }
    }
    if(i > 0 && i < Lpointer->length){
        *ppre_e = *(Lpointer->head_pointer + i - 1);
        return OK;
    }else{
        return ERROR;
    }
}

取某个元素的后继

St next_elem(const ALIST *Lpointer, const MY_TYPE cur_e, MY_TYPE *ppre_e){
    int i = 0;
    for(; i < Lpointer->length; ++i){
        if(cur_e == *(Lpointer->head_pointer + i)){
            break;
        }
    }
    if(i > -1 && i < (Lpointer->length - 1)){
        *ppre_e = *(Lpointer->head_pointer + i + 1);
        return OK;
    }else{
        return ERROR;
    }
}

在数组给定下标为i的位置插入e

/*在数组给定下标为i的位置插入e*/
St alist_insert(ALIST *Lpointer, const int i, const MY_TYPE e){
    if(!(i > -1 && i <= Lpointer->length))
        return ERROR;
    if(Lpointer->length >= Lpointer->listsize){
        MY_TYPE *newpointer = (MY_TYPE *)realloc(Lpointer->head_pointer,\
        (Lpointer->listsize + LIST_INCRE_SIZE) * sizeof(MY_TYPE));
        if(!newpointer)
            return ERROR;
        Lpointer->head_pointer = newpointer;
        Lpointer->listsize += LIST_INCRE_SIZE;
    }
    for(int j = (Lpointer->length - 1); j > i; --j)
        *(Lpointer->head_pointer + j + 1) = *(Lpointer->head_pointer + j);
    *(Lpointer->head_pointer + i) = e;
    ++Lpointer->length;
    return OK;
}

增加一个元素在表尾

/*增加一个元素在表尾*/
St alist_add(ALIST *Lpointer, MY_TYPE e){
    if(!e)
        return ERROR;
    if(Lpointer->length >= Lpointer->listsize){
        MY_TYPE *newpointer = (MY_TYPE *)realloc(Lpointer->head_pointer,\
        (Lpointer->listsize + LIST_INCRE_SIZE) * sizeof(MY_TYPE));
        if(!newpointer)
            return ERROR;
        Lpointer->head_pointer = newpointer;
        Lpointer->listsize += LIST_INCRE_SIZE;
    }
    *(Lpointer->head_pointer + Lpointer->length) = e;
    ++Lpointer->length;
    return OK;
}

在数组给定下标为i的位置删除此元素用*P_e返回

/*在数组给定下标为i的位置删除此元素用*P_e返回*/
St alist_delete(ALIST *Lpointer, const int i, MY_TYPE *p_e){
    if(!(i > -1 && i < Lpointer->length))
        return ERROR;
    *p_e = *(Lpointer->head_pointer + i);
    for(int j = (Lpointer->length - 1); j > i; --j)
        *(Lpointer->head_pointer + j - 1) = *(Lpointer->head_pointer + j);
    --Lpointer->length;
    return OK;
}

修改指定位置的元素

/*修改指定位置的元素*/
St alist_mod(ALIST *Lpointer, const int i, MY_TYPE mod_e){
    if(!(i > -1 && i < Lpointer->length))
        return ERROR;
    *(Lpointer->head_pointer + i) = mod_e;
    return OK;
}

遍历线性表

/*遍历线性表*/
St alist_visit(const ALIST *Lpointer){
    switch(_MY_TYPE_){
    case 0:
        {
            MY_TYPE *p = Lpointer->head_pointer;
            for(int i = 1; i <= Lpointer->length; ++i)
                printf("第%d个元素是:%d\n", i, (int)*p++);
            break;
        }
    case 1:
        {
            MY_TYPE *p = Lpointer->head_pointer;
            for(int i = 1; i <= Lpointer->length; ++i)
                printf("第%d个元素是:%lf\n", i, (double)*p++);
            break;
        }
    case 2:
        {
            MY_TYPE *p = Lpointer->head_pointer;
            for(int i = 1; i <= Lpointer->length; ++i)
                printf("第%d个元素是:%f\n", i, (float)*p++);
            break;
        }
    case 3:
        {
            MY_TYPE *p = Lpointer->head_pointer;
            for(int i = 1; i <= Lpointer->length; ++i)
                printf("第%d个元素是:%c\n", i, (char)*p++);
            break;
        }
    default:
        printf("属下无能\n");
        return ERROR;
    }
    return OK;
}

简单的比较函数

/*compare--->equal*/
St cmp_equal(MY_TYPE e1, MY_TYPE e2){
    if(e1 == e2)
        return OK;
    else
        return ERROR;
}
/*compare--->less*/
St cmp_less(MY_TYPE e1, MY_TYPE e2){
    if(e1 < e2)
        return OK;
    else
        return ERROR;
}
/*compare--->more*/
St cmp_more(MY_TYPE e1, MY_TYPE e2){
    if(e1 > e2)
        return OK;
    else
        return ERROR;
}

测试操作顺序表的基本功能

在main.中测试

#include "header.h"

int main()
{
    ALIST L = {0};
    init_alist(&L);
    printf("------------------------------------------------------------------\n");
    printf("地址:%d\n长度:%d\n容量:%d\n", L.head_pointer, L.length, L.listsize);
    printf("------------------------------------------------------------------\n");
    if(alist_empty(&L))
        printf("是否为空:OK\n");
    else
        printf("是否为空:NO\n");
    printf("------------------------------------------------------------------\n");
    printf("当前长度:%d\n", alist_length(&L));
    printf("------------------------------------------------------------------\n");
    alist_add(&L, 1);
    alist_add(&L, 2);
    alist_add(&L, 200);
    alist_add(&L, 34);
    alist_visit(&L);
    printf("------------------------------------------------------------------\n");
    MY_TYPE e1;
    alist_delete(&L, 2, &e1);
    printf("被删除的元素:%d\n", e1);
    printf("------------------------------------------------------------------\n");
    alist_visit(&L);
    printf("------------------------------------------------------------------\n");
    printf("地址:%d\n长度:%d\n容量:%d\n", L.head_pointer, L.length, L.listsize);
    printf("------------------------------------------------------------------\n");
    alist_insert(&L, 2, 200);
    alist_visit(&L);
    printf("------------------------------------------------------------------\n");
    printf("地址:%d\n长度:%d\n容量:%d\n", L.head_pointer, L.length, L.listsize);
    printf("------------------------------------------------------------------\n");
    MY_TYPE e2;
    if(next_elem(&L, 200, &e2))
        printf("200的后继:%d\n", e2);
    else
        printf("后继访问失败啦\n");
    printf("------------------------------------------------------------------\n");
    MY_TYPE e3;
    if(prior_elem(&L, 200, &e3))
        printf("200的前驱:%d\n", e3);
    else
        printf("前驱访问失败啦\n");
    printf("------------------------------------------------------------------\n");
    printf("%d\n", locate_elem(&L, 200, cmp_equal));
    printf("------------------------------------------------------------------\n");
    MY_TYPE e4;
    if(get_elem(&L, 4, &e4))
        printf("index=2:%d\n", e4);
    else
        printf("获取元素失败啦\n");
    printf("------------------------------------------------------------------\n");
    if(alist_mod(&L, 2, 300))
        alist_visit(&L);
    else
        printf("修改元素失败啦\n");
    printf("------------------------------------------------------------------\n");
    if(alist_empty(&L))
        printf("是否为空:OK\n");
    else
        printf("是否为空:NO\n");
    printf("------------------------------------------------------------------\n");
    if(clear_alist(&L)){
        printf("清空成功\n");
        alist_visit(&L);
    }
    else
        printf("清空失败\n");
    printf("------------------------------------------------------------------\n");
    printf("地址:%d\n长度:%d\n容量:%d\n", L.head_pointer, L.length, L.listsize);
    printf("------------------------------------------------------------------\n");
    if(destroy_alist(&L))
        printf("地址:%d\n长度:%d\n容量:%d\n", L.head_pointer, L.length, L.listsize);
    else
        printf("销毁失败啦\n");
    printf("------------------------------------------------------------------\n");

    return 0;
}

测试结果

------------------------------------------------------------------
地址:6893504
长度:0
容量:3
------------------------------------------------------------------
是否为空:OK
------------------------------------------------------------------
当前长度:0
------------------------------------------------------------------
第1个元素是:1
第2个元素是:2
第3个元素是:200
第4个元素是:34
------------------------------------------------------------------
被删除的元素:200
------------------------------------------------------------------
第1个元素是:1
第2个元素是:2
第3个元素是:34
------------------------------------------------------------------
地址:6893408
长度:3
容量:5
------------------------------------------------------------------
第1个元素是:1
第2个元素是:2
第3个元素是:200
第4个元素是:34
------------------------------------------------------------------
地址:6893408
长度:4
容量:5
------------------------------------------------------------------
200的后继:34
------------------------------------------------------------------
200的前驱:2
------------------------------------------------------------------
200的位置:2
------------------------------------------------------------------
获取元素失败啦
------------------------------------------------------------------
第1个元素是:1
第2个元素是:2
第3个元素是:300
第4个元素是:34
------------------------------------------------------------------
是否为空:NO
------------------------------------------------------------------
清空成功
------------------------------------------------------------------
地址:6893408
长度:0
容量:5
------------------------------------------------------------------
地址:0
长度:0
容量:0
------------------------------------------------------------------

猜你喜欢

转载自www.cnblogs.com/xinglichao/p/9164113.html