顺序表的实现(C语言版)

运行环境:(vs2015)

#include
#include

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW 0

typedef int ElemType;
typedef int Status;

typedef struct {
    ElemType *elem;
    int length;     //当前长度
    int size;       //存储容量
    int increment;  //扩容的增量
}SqList;        //顺序表

//-----声明接口----
Status InitList_Sq(SqList &L, int size, int inc);   //初始化顺序表
Status DestoryList_Sq(SqList &L);   //销毁顺序表
Status ClearList_Sq(SqList &L); //将顺序表清空
Status ListEmpty_Sq(SqList L);  //判断顺序表是否为空
int ListLength_Sq(SqList L);  //返回顺序表中的元素个数
Status GetElem_Sq(SqList L, int i, ElemType &e);    //用e返回顺序表l中第i个元素的值
int Search_Sq(SqList L, ElemType e);        //在顺序表L中查找元素e,成功时返回第一次出现的位置
Status ListTraverse_Sq(SqList L, Status(*visit)(ElemType e));   //遍历顺序表L,依次对每一个元素调用visit
Status PutElem_Sq(SqList &L, int i, ElemType e);    //将顺序表L中的第i个元素赋值为e
Status Append_Sq(SqList &L, ElemType e);  //在顺序表表尾添加元素e
Status DeleteLast_Sq(SqList &L, ElemType &e);  //删除顺序表L的表尾元素,用e返回其值

//-----接口实现-------

//初始化顺序表
Status InitList_Sq(SqList &L, int size, int inc) {
    L.elem = (ElemType *)malloc(size * sizeof(ElemType));
    if (L.elem == NULL) return OVERFLOW;  //分配空间失败
    L.size = size;
    L.increment = inc;
    L.length = 0;
    return OK;
}

//销毁顺序表
Status DestoryList_Sq(SqList &L) {
    L.elem = NULL;
    L.length = 0;
    L.size = 0;
    free(L.elem);
    return OK;
}

//将顺序表清空
Status ClearList_Sq(SqList &L) {
    if (L.length == 0) return OK;
    for (int i = L.length; i > 0; i--) {        //细节决定成败!
        L.elem[i] = 0;
    }
    L.length = 0;
    return OK;
}

//判断顺序表是否为空
Status ListEmpty_Sq(SqList L) {
    if (L.length == 0) return TRUE;
    else return FALSE;
}

//返回顺序表中的元素个数
int ListLength_Sq(SqList L) {
    return L.length;
}

//用e返回顺序表l中第i个元素的值
Status GetElem_Sq(SqList L, int i, ElemType &e) {
    if (L.length == 0) return ERROR;
    if (i <= L.length) {
        e = L.elem[i-1];
        return e;
    }
    else return ERROR;
}

//在顺序表L中查找元素e,成功时返回第一次出现的位置
int Search_Sq(SqList L, ElemType e) {
    int i = 0;
    while (i < L.length&&e != L.elem[i])    i++;
    if (i < L.length)   return i;
    else return -1;
}

//将顺序表L中的第i个元素赋值为e
Status PutElem_Sq(SqList &L, int i, ElemType e) {
    if (i > L.length)   return ERROR;   //参数错误
    L.elem[i] = e;
    return OK;
}

//在顺序表表尾添加元素e
Status Append_Sq(SqList &L, ElemType e) {
    //---注意存储空间的问题---
    ElemType *temp;
    if (L.size <= L.length) {   //注意特殊情况,扩容
        temp = (ElemType *)realloc(L.elem, (L.size + L.increment) * sizeof(ElemType));
        L.elem = temp;
        L.size = L.size + L.increment;
    }
    L.elem[L.length++] = e;
    return OK;
}

//删除顺序表L的表尾元素,用e返回其值
Status DeleteLast_Sq(SqList &L, ElemType &e) {
    if (L.length == 0)  return ERROR;  //空表
    e = L.elem[L.length - 1];//被删除元素的值赋给e
    --L.length;
    return e;
}

void main() {
    SqList L;
    ElemType e;
    //初始化顺序表
    InitList_Sq(L, 8, 10);
    //在表尾添加元素
    for (int i = 0; i < 10; i++) {      //会进行扩容的
        Append_Sq(L, i + 1);    //添加从110
    }
    //遍历此时的顺序表
    for (int i = 0; i < L.length; i++) {
        printf("%d\n", L.elem[i]);
    }
    //返回顺序表中的元素个数
    int a = ListLength_Sq(L);
    printf("此时顺序表的元素个数为:%d\n", a);
    //返回第4个元素
    int b = GetElem_Sq(L, 4, e);
    printf("第4个元素的值为:%d\n", b);
    //查找元素 4 和 元素 100,返回第一次出现的位置
    int c1 = Search_Sq(L, 4);
    printf("元素4出现的第一次位置为:%d\n", c1);
    int c2 = Search_Sq(L, 100);     //应该返回-1
    printf("元素100出现的第一位置为:%d\n", c2);
    //删除表尾元素
    int d = DeleteLast_Sq(L, e);
    printf("最后一个元素:%d\n", d);
    //再遍历一下
    for (int i = 0; i < L.length; i++) {
        printf("%d\n", L.elem[i]);
    }
    //清空顺序表
    ClearList_Sq(L);
    //判断是否为空
    if (ListEmpty_Sq(L) == TRUE)    printf("此时顺序表为空\n");
    else printf("此时顺序表不为空\n");
    //销毁顺序表
    DestoryList_Sq(L);
    if (L.elem == NULL) printf("销毁成功,此时顺序表不存在\n");
}

你可能感兴趣的:(数据结构)