首页 > 其他 > 详细

线性表的顺序表示和实现

时间:2021-02-28 15:24:13      阅读:16      评论:0      收藏:0      [点我收藏+]

 结构体:

struct Sqlist {

ElemType* elem;
int length;
};//定义顺序表类型

 

函数方法

Status InitList(Sqlist& L); //初始化
void Destory(Sqlist& L); //销毁已存在的线性表
void ClearList(Sqlist& L); //清空
int GetLength(Sqlist L);
int IsEmpty(Sqlist L); //判断线性表是否为空
Status GetElem(Sqlist L, int i, ElemType& e); //将L中第i位返回给e
Status LocateElem(Sqlist L, ElemType e); //L中查找与e相等的元素,成功返回序号,失败返回0
Status ListInsert(Sqlist& L, int i, ElemType e); //在第i个位置插入e
Status ListDelete(Sqlist& L, int i); //删除线性表第i个元素

 

代码

.h

技术分享图片
#define MAXSIZE 100

//函数结果状态
#define TRUE    1
#define FALSE    0
#define OK        1
#define ERROR    0
#define INFEASIBLE    -1
#define OVERFLOW    -2
//Status 是函数的类,值为函数结果状态代码
typedef int Status;
typedef char ElemType;

struct Sqlist {
    ElemType* elem;
    int length;
};//定义顺序表类型

Status InitList(Sqlist& L);                                //初始化
void Destory(Sqlist& L);                                //销毁已存在的线性表
void ClearList(Sqlist& L);                                //清空
int GetLength(Sqlist L);
int IsEmpty(Sqlist L);                                    //判断线性表是否为空
Status GetElem(Sqlist L, int i, ElemType& e);            //将L中第i位返回给e
Status LocateElem(Sqlist L, ElemType e);                //L中查找与e相等的元素,成功返回序号,失败返回0
Status ListInsert(Sqlist& L, int i, ElemType e);        //在第i个位置插入e
Status ListDelete(Sqlist& L, int i);                    //删除线性表第i个元素
View Code

.cpp

技术分享图片
#include "Sqlist.h"

//初始化
Status InitList(Sqlist& L)
{
    L.elem = new ElemType[MAXSIZE];
    if (!L.elem) return OVERFLOW;
    L.length = 0;
    return OK;
}

//销毁
void Destory(Sqlist& L)
{
    if (L.elem) delete L.elem;
}

//清空
void ClearList(Sqlist& L)
{
    L.length = 0;
}

int GetLength(Sqlist L)
{
    return L.length;
}

Status IsEmpty(Sqlist L)
{
    if (L.length == 0) return TRUE;
    else return FALSE;
}

Status GetElem(Sqlist L, int i, ElemType& e)
{
    if(i<1 || i>L.length) return ERROR;
    e = L.elem[i - 1];
    return OK;
}

Status LocateElem(Sqlist L, ElemType e)
{
    for (int i = 0; i < L.length; i++)
    {
        if (L.elem[i] == e) return i + 1;
    }
    return ERROR;
}

Status ListInsert(Sqlist& L, int i, ElemType e)
{
    if (i<1 || i>L.length + 1) return ERROR;
    if (L.length == MAXSIZE) return ERROR;
    for (int j = L.length - 1; j >= i - 1; j--)
        L.elem[j + 1] = L.elem[j];
    L.elem[i - 1] = e;
    L.length++;
    return OK;
}

Status ListDelete(Sqlist& L, int i)
{
    if (i<1 || i>L.length) return ERROR;
    for (int j = i;j <= L.length ;j++)
    {
        L.elem[j - 1] = L.elem[j];
    }
    L.length--;
    return OK;
}
View Code

.main

技术分享图片
#include <iostream>
#include "Sqlist.h"
using namespace std;

int main()
{
    Sqlist L;
    InitList(L);
    ListInsert(L, 1, H);
    ListInsert(L, 2, e);
    ListInsert(L, 3, l);
    ListInsert(L, 4, l);
    ListInsert(L, 5, o);
    ElemType a;
    GetElem(L, 4, a);
    cout << a <<endl;
    system("pause");
}
View Code

 

线性表的顺序表示和实现

原文:https://www.cnblogs.com/yuqi-chen/p/14458582.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!