C封装线性表对象

github源码

LinearList.c文件

#include <stdio.h>
#include <malloc.h>
#include "LinearList.h"
//线性表

static void clear(LinearList *This);
static int isEmpty(LinearList *This);
static int length(LinearList *This);
static void print(LinearList *This);
static int indexElem(LinearList *This, ElemType* x);
static int priorElem(LinearList *This, ElemType* cur_elem, ElemType* pre_elem);
static int getElem(LinearList *This, int index, ElemType* e);
static int modifyElem(LinearList *This, int index, ElemType* e);
static int nextElem(LinearList *This, ElemType* cur_elem, ElemType* next_elem);
static int insertElem(LinearList *This, int index, ElemType* e);
static int deleteElem(LinearList *This, int index, ElemType* e);
static int appendElem(LinearList *This,ElemType* e);
static int popElem(LinearList *This, ElemType* e);

LinearList *InitLinearList(){
    LinearList *L = (LinearList *)malloc(sizeof(LinearList));
    LinearList_P *p = (LinearList_P *)malloc(sizeof(LinearList_P));
    p->elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
    p->length = 0; //当前长度
    p->size = LIST_INIT_SIZE; //当前分配量
    L->This = p;
    L->clear = clear;
    L->isEmpty = isEmpty;
    L->length = length;
    L->print = print;
    L->indexElem = indexElem;
    L->priorElem = priorElem;
    L->getElem = getElem;
    L->modifyElem = modifyElem;
    L->nextElem = nextElem;
    L->insertElem = insertElem;
    L->deleteElem = deleteElem;
    L->appendElem = appendElem;
    L->popElem = popElem;
    return L;
}

void DestroyLinearList(LinearList *L){
    free(L->This);
    free(L);
    L = NULL;
}

static void clear(LinearList *This){
    LinearList_P *p = This->This;
    p->length = 0; //当前长度
}

static int isEmpty(LinearList *This){
    LinearList_P *p = This->This;
    return (p->length == 0);
}

static int length(LinearList *This){
    LinearList_P *p = This->This;
    return p->length;
}

static void print(LinearList *This){
    LinearList_P *p = This->This;
    int i;
    for (i=0; i < p->length; i++){
        printf("%d ", p->elem[i]);
    }
    printf("\n");
}

static int indexElem(LinearList *This, ElemType* x){
    LinearList_P *p = This->This;
    int pos = -1;
    for (int i = 0; i < p->length; i++){
        if (p->elem[i] == *x){
            pos = i;
        }
    }
    return pos;
}

static int priorElem(LinearList *This, ElemType* cur_elem, ElemType* pre_elem){
    LinearList_P *p = This->This;
    int pos = -1;
    pos = indexElem(This, cur_elem);
    if(pos <= 0) return -1;
    *pre_elem = p->elem[pos-1];
    return 0;
}

static int getElem(LinearList *This, int index, ElemType* e){
    LinearList_P *p = This->This;
    if (index<0 || index >= p->length) return -1;
    *e = p->elem[index];
    return 0;
}

static int modifyElem(LinearList *This, int index, ElemType* e){
    LinearList_P *p = This->This;
    if (index<0 || index >= p->length) return -1;
    p->elem[index] = *e;
    return 0;
}

static int nextElem(LinearList *This, ElemType* cur_elem, ElemType* next_elem){
    LinearList_P *p = This->This;
    int pos = -1;
    pos = indexElem(This, cur_elem);
    if(pos == -1 || pos == (p->length - 1)) return -1;
    *next_elem = p->elem[pos+1];
    return 0;
}

static int insertElem(LinearList *This, int index, ElemType* e){
    LinearList_P *p = This->This;
    if (index<0 || index >= p->length) return -1;
    if (p->length >= p->size){ //判断存储空间是否够用
        ElemType *newbase = (ElemType *)realloc(p->elem, (p->size + LISTINCREMENT)*sizeof(ElemType));
        if (!newbase) return -1;//存储空间分配失败
        p->elem = newbase;//新基址
        p->size += LISTINCREMENT;//增加存储容量
    }
    ElemType *elem_q = NULL;, *elem_p = NULL;;
    elem_q = &(p->elem[index]); //q为插入位置
    for (elem_p = &(p->elem[p->length - 1]); elem_p >= elem_q; --elem_p){ //从ai到an-1依次后移,注意后移操作要从后往前进行
        *(elem_p + 1) = *elem_p;
    }
    *elem_q = *e;
    ++p->length;
    return 0;
}

static int deleteElem(LinearList *This, int index, ElemType* e){
    LinearList_P *p = This->This;
    if (index<1 || index > p->length) return -1;
    ElemType *elem_q = NULL;, *elem_p = NULL;;
    elem_p = &(p->elem[index]);//elem_p为被删除元素的位置
    *e = *elem_p; //被删除的元素赋值给e
    elem_q = p->elem + p->length - 1;//elem_q指向表尾最后一个元素
    for (++elem_p; elem_p <= elem_q; ++elem_p){ //从elem_p的下一个元素开始依次前移
        *(elem_p - 1) = *elem_p;
    }
    --p->length;
    return 0;
}

static int appendElem(LinearList *This,ElemType* e){
    LinearList_P *p = This->This;
    if (p->length >= p->size){ //判断存储空间是否够用
        ElemType *newbase = (ElemType *)realloc(p->elem, (p->size + LISTINCREMENT)*sizeof(ElemType));
        if (!newbase) return -1;//存储空间分配失败
        p->elem = newbase;//新基址
        p->size += LISTINCREMENT;//增加存储容量
    }
    p->elem[p->length] = *e;
    ++p->length;
    return 0;
}

static int popElem(LinearList *This, ElemType* e){
    LinearList_P *p = This->This;
    if (isEmpty(This)) return -1;
    *e = p->elem[p->length - 1];
    --p->length;
    return 0;
}

LinearList.h文件:

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __LINEAR_LIST_H
#define __LINEAR_LIST_H
/* Includes ------------------------------------------------------------------*/
/* Exported types ------------------------------------------------------------*/
typedef int ElemType;      //数据元素的类型,假设是int型的

typedef struct LinearList_P{
    ElemType *elem;  //存储空间的基地址
    int length;      //当前线性表的长度
    int size;    //当前分配的存储容量
}LinearList_P;

typedef struct LinearList{
    LinearList_P *This;  
    void (*clear)(struct LinearList *This);
    int (*isEmpty)(struct LinearList *This);
    int (*length)(struct LinearList *This);
    void (*print)(struct LinearList *This);
    int (*indexElem)(struct LinearList *This, ElemType* x);
    int (*priorElem)(struct LinearList *This, ElemType* cur_elem, ElemType* pre_elem);
    int (*getElem)(struct LinearList *This, int index, ElemType* e);
    int (*modifyElem)(struct LinearList *This, int index, ElemType* e);
    int (*nextElem)(struct LinearList *This, ElemType* cur_elem, ElemType* next_elem);
    int (*insertElem)(struct LinearList *This, int index, ElemType* e);
    int (*deleteElem)(struct LinearList *This, int index, ElemType* e);
    int (*appendElem)(struct LinearList *This,ElemType* e);
    int (*popElem)(struct LinearList *This, ElemType* e);
}LinearList;

/* Exported define -----------------------------------------------------------*/
#define LIST_INIT_SIZE 100 //线性表存储空间的初始分配量
#define LISTINCREMENT 10   //线性表存储空间的分配增量(当存储空间不够时要用到)
/* Exported macro ------------------------------------------------------------*/
LinearList *InitLinearList();
void DestroyLinearList(LinearList *L);

#endif

测试文件 testLinearList.c

#include <stdio.h>
#include <malloc.h>
#include "LinearList.h"

int main(void)
{
    int i;
    ElemType elem,elem1;
    LinearList *list = InitLinearList();
    printf("list is empty:%d\n",list->isEmpty(list));
    for (i = 0; i < 10; i++){
        list->appendElem(list,&i);
    }
    list->print(list);
    printf("list is empty:%d\n",list->isEmpty(list));
    printf("list length:%d\n",list->length(list));
    list->clear(list);
    for (i = 10; i < 20; i++){
        list->appendElem(list,&i);
    }   
    list->print(list);
    elem = 17;
    printf("the index of 17 is %d\n",list->indexElem(list,&elem));
    list->priorElem(list,&elem,&elem1);
    printf("the prior elem of 17 is %d\n",elem1);
    list->nextElem(list,&elem,&elem1);
    printf("the next elem of 17 is %d\n",elem1);
    list->getElem(list,3,&elem1);
    printf("the elem of index 3 is %d\n",elem1);
    list->modifyElem(list,3,&elem);
    list->getElem(list,3,&elem1);
    printf("modify the elem of index 3 to %d\n",elem1);
    list->print(list);
    elem = 25;
    list->insertElem(list,5,&elem);
    printf("insert elem %d to index 5\n",elem);
    list->deleteElem(list,7,&elem);
    printf("delete elem %d of index 7\n",elem);
    list->print(list);
    list->popElem(list,&elem);
    printf("pop elem %d\n",elem);
    list->print(list);
    DestroyLinearList(list);
    return 0;
}

编译:

gcc LinearList.c LinearList.h testLinearList.c -o testLinearList

运行testLinearList
输出:

list is empty:1
0 1 2 3 4 5 6 7 8 9
list is empty:0
list length:10
10 11 12 13 14 15 16 17 18 19
the index of 17 is 7
the prior elem of 17 is 16
the next elem of 17 is 18
the elem of index 3 is 13
modify the elem of index 3 to 17
10 11 12 17 14 15 16 17 18 19
insert elem 25 to index 5
delete elem 16 of index 7
10 11 12 17 14 25 15 17 18 19
pop elem 19
10 11 12 17 14 25 15 17 18
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 161,326评论 4 369
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,228评论 1 304
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 110,979评论 0 252
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,489评论 0 217
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,894评论 3 294
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,900评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,075评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,803评论 0 205
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,565评论 1 249
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,778评论 2 253
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,255评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,582评论 3 261
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,254评论 3 241
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,151评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,952评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,035评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,839评论 2 277

推荐阅读更多精彩内容

  • 在C语言中,五种基本数据类型存储空间长度的排列顺序是: A)char B)char=int<=float C)ch...
    夏天再来阅读 3,102评论 0 2
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 2,625评论 0 3
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,106评论 18 139
  • 果真 當我說了我要出國去玩 而且已付訂金 對方第一個想到的就是我要花錢了 而為了怕我把錢花掉 他們會騙不到錢 對方...
    Ching9036HSU阅读 369评论 7 0
  • 提着晚餐便当就准备回家 走着, 嘀嗒嘀嗒的作响 原来是破了口袋,粗了心来 饭菜怎么不可口, 曾拥挤得饭台,不在 你...
    有只鱼阅读 71评论 0 0