第一篇:线性表-顺序表的设计

##对于⾮空的线性表和线性结构,其特点如下:

• 存在唯⼀的⼀个被称作”第⼀个”的数据元素;

• 存在唯⼀的⼀个被称作”最后⼀个"的数据元素

• 除了第⼀个之外,结构中的每个数据元素均有⼀个前驱

• 除了最后⼀个之外,结构中的每个数据元素都有⼀个后继.

#include

#include "stdlib.h"

#include"math.h"

#include"time.h"

#define MAXSIZE100/// 表大小

#define OK1/// 状态

#define ERROR0/// 错误

typedefintElemType;///元素类型,假设为int

typedefintStatus;///状态:函数结果返回状态

// 顺序表的结构

typedef struct {

    ElemType*data;///数据

 intlength;///长度

} Sqlist;

//1、 初始化顺序表

Status InitSqlist (Sqlist *L) {

    // 分配数据空间

L->data=malloc(sizeof(ElemType) *MAXSIZE);

 if(!L->data) {

        exit(ERROR);// 报错则退出

    }

    // 空白长度为0

    L->length=0;

 returnOK;

}

// 2、顺序表:数据插入

/*

 初始条件:顺序线性表L已存在,1≤i≤ListLength(L);

 操作结果:在L中第i个位置之前插入新的数据元素e,L的长度加1

 */

Status InSertElem (Sqlist *L, int i, ElemType e) {

    // 位置是否合法

 if(i <0|| i > L->length) {

 returnERROR;

    }

    // 存储空间是否已满

 if(L->length>=MAXSIZE) {

 returnERROR;

    }

    // 插入位置是否在表尾

 if(i < L->length) {

        // 不在表尾,则移动表,空出位置

 for(intj = L->length; j > i; j--) {

            // 插入位置以及之后的位置后移动1位

            L->data[j] = L->data[j -1];

        }

    }

    // 插入新元素

    L->data[i] = e;

    // 表长度加1

    ++L->length;

 returnOK;

}

// 3、顺序表:取值

Status GetElem (Sqlist L, int i, ElemType *e) {

 if((i > L.length-1) || i <0) {

 returnERROR;

    }

    (*e) = L.data[i];

 returnOK;

}

// 4、顺序表:删除

Status ListDeleteNum(Sqlist *L, int i) {

    // 异常情况处理

 if(i <0|| i > (L->length-1) || L ->length==0) {

 returnERROR;

    }

    // 被删除元素后面的元素都往前移动一位

 for(intj = i; j < L->length; j++) {

        L->data[j] = L->data[j +1];

    }

    // 长度减少1

    L->length--;

 returnOK;

}

// 5、顺序表:遍历输出

Status TraverseList (Sqlist L) {

 for(inti =0; i < L.length; i++) {

        ElemTypee = L.data[i];

        printf("元素值:%d -- %d\n", i, e);

    }

 returnOK;

}

// 6、遍历查找是否 存在摸个元素,存在则返回位置,否则返回-1

int LocateElem(Sqlist L, ElemType e) {

 if(L.length==0) {

 return-1;

    }

 inti =0;

 for(i =0; i < L.length-1; i++) {

 if(L.data[i] == e) {

 break;

        }

    }

 if(i > L.length-1) {

 return-1;

    }

 return(i +1);

}

intmain(intargc,constchar* argv[]) {

    // insert code here...

    printf("Hello, World!\n");

    SqlistL;

    ElemType e;

    StatusiStatus;

    // 初始化顺序表

    iStatus =InitSqlist(&L);

    printf("初始化结果: %d\n -- 长度:%d\n", iStatus, L.length);

    // 插入数据

 for(intj =0; j <5;j++){

        iStatus =InSertElem(&L,0, j);

    }

    printf("插入数据L长度: %d\n",L.length);

    // 输出

    iStatus =TraverseList(L);

    // 取值

 intnum =2;

    GetElem(L, num -1, &e);

    printf("取第%d个元素值为: %d\n", num, e);

    // 删除第2个元素

    num =2;

    iStatus =ListDeleteNum(&L, num -1);

    // 输出

    iStatus =TraverseList(L);

    printf("++++++++++++++++++++++++++\n");

    // 查找

 for(intj = L.length-1; j <27; j++){

        iStatus =InSertElem(&L,0, j);

    }

    // 输出

    iStatus =TraverseList(L);

    printf("20的位置:%d\n",LocateElem(L,20));

 return0;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容