所谓顺序表,即线性表的顺序存储结构。下面给出的是数据结构---线性表的定义。

ADT List{

  数据对象:

    线性表的数据对象的集合为{a1,a2,a3,...,an},每个元素的类型为ElemType。

  数据关系:

    除第一个元素a1外,每一个元素有且只有一个直接前驱元素,除了最后一个元素an外,每个元素有且仅有一个直接后继元素。

    数据元素之间的关系是一对一的关系。

  基础操作:

    InitList(&L);  //构造一个空的线性表

    DestroyList(&L); //销毁线性表

    ClearList(&L); //清空线性表

    ListEmpty(L); //若线性表为空,返回true,否则返回false

    ListLength(L);//求表长

    GetElem(L, i, &e); //将线性表中第i个元素赋值给e

    LocateElem(L, e, cmp()); //返回L中第一个满足cmp()函数的元素的序号,若不存在,则返回0

    ListInsert(&L, i, e); //在L中的第i个位置之前插入元素e且L的长度+1

    ListDelete(&L, i, &e); //删除L中的第i个元素并用e返回其值,且L的长度-1

    ListTraverse(L, visit()); //线性表的遍历,依次对每个元素调用visit函数

  其他操作:

    PrioElem(L, cur_e, &pre_e); //cue_e是L中的数据元素,用pre_e返回其前驱;若无前驱则操作失败

    NextElem(L, cue_e, &next_e); //cur_e是L中的数据元素,用next_e返回后继;若无后继则操作失败

    union(&L1, &L2); //求二者元素并集;又可根据线性表是否有序有不同的操作。

}

以下是线性表的顺序存储结构的多种实现方式,详细参阅代码。(大部分只实现了基础操作)

 

1.用C语言实现顺序表 静态数组方式

/************************************************************************
    用C语言实现顺序表 静态数组方式
************************************************************************/
#include <cstdio>

typedef int ElemType;
typedef int Status;

const int ERROR = 0;
const int OK = 1;
const int TRUE = 1;
const int FALSE = 0;
const int LIST_SIZE = 100;

//定义顺序表结构ADT
typedef struct{
    int elem[LIST_SIZE];
    int length;
}SqList, *pList;

//初始化顺序表
Status InitList(pList List)
{
    List->length = 0;
    return OK;
}

//释放顺序表
Status DestroyList(pList List)
{
    List->length = 0;
    return OK;
}

//判断顺序表是否为空 是 返回 1 否则 返回 0
Status ListEmpty(pList List)
{
    if (List->length)
        return FALSE;
    return TRUE;
}

//返回顺序表的长度
int ListLength(pList List)
{
    return List->length;
}

//根据下标获取元素
Status GetElem(pList List, int i, ElemType *e)
{
    if (i < 1 || i > List->length)
        return ERROR;
    //第i个数据元素存储在下标为i-1的数组中
    *e = List->elem[i - 1];
    return OK;
}

//判断给定数据是否为顺序表的元素
Status LocateElem(pList List, int e)
{
    if (ListEmpty(List))
        return ERROR;
    for (int i = 0; i < List->length; ++i)
    {
        if (e == List->elem[i])
            return TRUE;
    }
    return FALSE;
}

//返回元素的前驱
Status PriorElem(pList List, ElemType cur_e, ElemType *pre_e)
{
    if (0 == List->length)
        return ERROR;
    for (int i = 0; i < List->length; ++i)
    {
        if (cur_e == List->elem[i] && i != 0){
            *pre_e = List->elem[i - 1];
            return OK;
        }
    }
    return ERROR;

}

//返回元素cur_e的后驱
Status NextElem(pList List, ElemType cur_e, ElemType *next_e)
{
    if (0 == List->length)
        return ERROR;
    for (int i = 0; i < List->length - 1; ++i)
    {
        if (cur_e == List->elem[i]){
            *next_e = List->elem[i + 1];
            return OK;
        }
    }
    return ERROR;
}

//在数据元素i之前插入新元素
Status ListInsert(pList List, int i, ElemType e)
{
    if (i < 1 || i > List->length + 1)
        return ERROR;
    if (LIST_SIZE <= List->length)
        return ERROR;
    //q为插入位置,次位置及之后的要先移位才能在q这插入
    ElemType* q = &List->elem[i - 1];
    //移位
    for (ElemType *p = &List->elem[List->length - 1]; p >= q; --p)
        *(p + 1) = *p;
    *q = e;
    ++List->length;
    return OK;
}

//删除顺序表中的第i个数据元素,并用e返回
Status ListDelete(pList List, int i, ElemType *e)
{
    if (i < 1 || i > List->length)
        return ERROR;
    //p为需要删除的元素地址 讲后面的一层层移位就好 q为最后一位元素地址
    ElemType *p = &List->elem[i - 1];
    ElemType *q = &List->elem[List->length - 1];
    *e = *p;
    //移位
    while (p < q){
        *p = *(p + 1);
        ++p;
    }
    --List->length;
    return OK;
}

//线性表的遍历
Status ListTraverse(pList List, void(*visit)(ElemType elem))
{
    if (ListEmpty(List))
        return ERROR;
    for (int i = 0; i < List->length; ++i)
        visit(List->elem[i]);
    return OK;
}

void visit(ElemType e){
    printf("%4d", e);
}

int main()
{
#ifdef _LOCAL
    freopen("input.txt", "r", stdin);
#endif
    SqList MyList;
    pList List = &MyList;

    InitList(List);
    ElemType tmp;
    for (int i = 1; i <= 5; ++i)
    {
        scanf("%d", &tmp);
        if (!ListInsert(List, i, tmp)){
            printf("ListInsert Error!\n");
        }
    }

    //if (!ListDelete(List, 5, &tmp)){
    //    printf("ListDelete Error!\n");
    //}
    //else{
    //    printf("Delete %d\n", tmp);
    //}

    ListTraverse(List, visit); printf("\n");

    return 0;
}
View Code

相关文章:

  • 2022-02-27
  • 2021-09-19
  • 2022-12-23
  • 2022-12-23
  • 2021-11-24
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-16
  • 2021-07-30
  • 2021-07-16
  • 2022-12-23
  • 2022-12-23
  • 2022-01-02
  • 2021-07-12
相关资源
相似解决方案