线性表的实现(C语言)

 小编使用的编译器CLion。如有问题可以私信小编,有不懂得也可以私信哦。

    初始化线性表

    遍历打印线性表

    销毁线性表

    清空线性表

    判断线性表是否为空

    求线性表长度

    获取指定位序元素

    线性表扩容

    表尾插入元素

    在指定元素前插入元素

    删除指定元素

    查找指定元素

    将在la中不在lb中的元素插入lb

    合并两个有序表


#include <stdio.h>

#include <malloc.h>

#include <stdlib.h>

#define LIST_INIT_SIZE 5

#define LIST_INCREMENT 5

typedef int ElemType;

typedef struct{

    ElemType *elem;  //存储基地址

    int listsize;

    int length;

}SqList;

/*初始化线性表*/

void InitList(SqList *L){

    L->elem =  (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType)); //申请存储空间

    if(!L->elem)  //如果申请失败,退出,提示初始化失败

    {

        printf("初始化失败!\n");

        exit(-1);

    }

    L->length = 0; //

    L->listsize = LIST_INIT_SIZE;

    printf("初始化完成!\n");

}

/*打印列表所有元素*/

void ShowList(SqList *L)

{

    if(L->elem==NULL)

    {

        printf("当前线性表不存在!\n");

        exit(-1);

    }

    if(L->length==0)

    {

        printf("当前列表为空!\n");

        exit(-1);

    }

    for(int i=0;i<L->length;i++)

    {

        printf("%d-",L->elem[i]);

    }

    printf("\n");

}

/*销毁线性表*/

void DestroyList(SqList *L)

{

    L->elem = NULL;

    free(L->elem);

    printf("线性表销毁成功!\n");

}

/*清空线性表*/

void ClearList(SqList *L)

{

    L->length = 0;

    printf("线性表成功清空!\n");

}

/*线性表是否为空*/

int ListEmpty(SqList *L)

{

    if (L->length == 0)

    {

        return 1;

    }

    else

        return 0;

}

/*线性表长度*/

int ListLength(SqList *L)

{

    return L->length;

}

/*获取第i个元素*/

ElemType GetElem(SqList *L,int i)

{

    if(i>L->length-1){

        printf("您的输入超出界限!/n");

        return 0;

    }

    else {

      return L->elem[i];

    }

}

/*表满,额外,申请空间*/

void ListIncerment(SqList *L)

{

    ElemType *newbase;

    newbase = (ElemType *)realloc(L->elem, ((L->listsize+LIST_INCREMENT) * sizeof(ElemType)));

    if(!newbase){

        printf("扩容失败!/n");

        exit(-1);

    }

    L->elem = newbase;

    L->listsize += LIST_INCREMENT;

    printf("扩容成功!\n");

}

/*依次在表尾插入元素*/

void InputElem(SqList *L,ElemType e)

{

    if(L->length == L->listsize){

        ListIncerment(L);

    } //如果表满,申请扩容

    L->elem[L->length] = e;

    L->length +=1;

    printf("插入元素-%d-成功!\n",e);

}

/*在位置i之前插入元素e*/

void ListInsert(SqList *L,int i,ElemType e)

{

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

        printf("当前共有%d个元素,请输入1-%d之间的数字!\n",L->length,L->length);

        exit(-1);

    }

    if(L->length==L->listsize)

    {

        ListIncerment(L);

    }

    for(int n=L->length-1;n>i-2;n--)

    {

        L->elem[n+1] = L->elem[n];

    }

    L->elem[i-1] = e;

    L->length++;

    printf("插入成功!\n");

}

/*删除i位置的元素,并用e返回*/

void DeleteElem(SqList *L,int i,ElemType* e)

{

    if(i<1||i>L->length)

    {

        printf("输入有误! 请输入1-%d之间的数字!\n",L->length);

        exit(-1);

    }

    *e = L->elem[i-1];

    for(int n=i;n<L->length;n++)

    {

        L->elem[n-1] = L->elem[n];

    }

    L->length --;

    printf("删除成功!\n");

}

/*查找是否存在某元素。存在,打印位序;不存在,提示并退出。*/

int LocateElem(SqList *L,ElemType e)

{

    int n;

    for(int i=0;i<L->length;i++)

    {

        if(L->elem[i]==e)

        {

            n = i;

            return 1;

        }

    }

    printf("该元素不存在\n");

    return 0;

}

/*将所有在la中但不在lb中的元素插入到lb中*/

void Union(SqList *la,SqList *lb)

{

    for(int i=0;i<la->length;i++)

    {

        if(!LocateElem(lb,la->elem[i]))

        {

            InputElem(lb,la->elem[i]);

        }

        else

        {

            continue;

        }

    }

}

/*两个有序表的合并,由lc返回*/

void MergeList(SqList *la,SqList *lb,SqList *lc)

{

    int i = 0;

    int j = 0;

    ElemType a,b;

    //当a,b均不为空时,合并一部分数据

    while(i<la->length && j<lb->length)

    {

        a = GetElem(la,i);

        b = GetElem(lb,j);

        if(a<b)

        {

            InputElem(lc,a);

            i++;

        }

        else

        {

            InputElem(lc,b);

            j++;

        }

    }

    //当a不为空时

    while(i<la->length)

    {

        InputElem(lc,la->elem[i]);

        i++;

    }

    //当b不为空时

    while (j<lb->length)

    {

        InputElem(lc,lb->elem[j]);

        j++;

    }

    printf("合并完成!\n");

}

int main() {

    return 0;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容