C/C++语言:顺序表 的封装

说明:本文介绍 顺序表SequenceList 的简单封装

一、定义基本数据类型

1、定义 元素类型ElementType

typedef struct {
    
    
    int number[11];//学号
    char name[11];//姓名
    int age;//年龄
}ElemType;
//或者
typedef int ElemType;
#define MAXSIZE 100 //顺序表可能达到的最大长度

2、定义 顺序表结构体

//第一种定义
typedef struct SequenceList{
    
    
    ElemType *elem;//顺序表的基地址
    int length;//长度
}SequenceList;
//第二种定义
typedef struct SequenceList {
    
    
    ElemType elem[MAXSIZE];//顺序表的数组
    int length;//长度
}SequenceList;

二、顺序表的基本操作

1、初始化

作用:
1、为顺序表L动态分配一个预定义大小的数组空间,使 elem 指向这段空间的基地址
2、将表的当前长度设为0

//第一种定义的初始化
void InitList(SequenceList &L){
    
    //初始化
    //C++写法
    L.elem = new ElemType[MAXSIZE];//为顺序表分配一个大小为MAXSIZE的数组空间
    //C写法
    //L.elem = malloc(MAXSIZE * sizeof(ElemType));
    if(L.elem){
    
    //申请地址成功
        L.length = 0;//初始化顺序表长度0
    }
    else
        cout<<"初始化失败!"<<endl;
}
//第二种定义的初始化
void InitList(SequenceList &L){
    
    
    L.elem = new ElemType;//为顺序表申请一个基地址空间
    if(L.elem){
    
    
        L.length = 0;
    }
    else
        cout << "初始化失败!" << endl;
}

2、取值

作用:获取顺序表中第 i 个数据元素的值,通过变量 e 返回

void GetElem(SequenceList L,int i,ElemType &e){
    
    //取值
    if(i<=0 || i>L.length)//i值非法
        cout << "i值非法,取值失败!" << endl;
    e = L.elem[i-1];
}

3、查找

作用:根据指定的元素值 e,查找该元素在顺序表中的位序(第几个)

int LocateElem(SequenceList L,ElemType e){
    
    //查找
    if(L.elem){
    
    
        for(int i=0;i<L.length;i++){
    
    
            if(L.elem[i] == e)
                return i+1;
        }
        return 0;
    }
}

4、插入

作用:在顺序表的第i个位置插入一个新的元素e,表的长度加1

void InsertList(SequenceList &L,int i,ElemType e){
    
    //插入
    if(i<=0 || i>L.length+1)//最多插入第 L.length 个位置;
        cout<<"i值非法,插入失败!"<<endl;
    else if(L.length == MAXSIZE)
        cout<<"顺序表已满,插入失败!"<<endl;
    else{
    
    
        for(int j = L.length-1;j >= i-1;j--){
    
    //腾空位
            L.elem[j+1] = L.elem[j];
        }
        L.elem[i-1] = e;//第i个位置为e
        ++L.length;
    }
}

5、删除

作用:删除顺序表的第i个元素,表的长度减1

void DeleteList(SequenceList &L,int i){
    
    //删除
    if(i<=0 || i>L.length)
        cout<<"i值非法,删除失败!"<<endl;
    else{
    
    
        for(int j = i;j <= L.length-1;j++)
            L.elem[j-1] = L.elem[j];
        --L.length;
    }
}

6、遍历

作用:遍历顺序表L

void TraverseList(SequenceList L){
    
    
    for(int i=0;i<L.length;i++)
        cout<<L.elem[i]<<" ";
    cout<<endl;
}

三、完整的头文件 SequenceList.h

#include "iostream"
#include "stdio.h"
using namespace std;

#define MAXSIZE 100

/*
 * TODO:定义数据类型
 * */
/*typedef struct {
    int number[11];//学号
    char name[11];//姓名
    int age;//年龄
}ElemType;*/
typedef int ElemType;

/*
 * TODO:定义顺序表
 * */
//第一种定义
typedef struct SequenceList{
    
    
    ElemType *elem;//顺序表的基地址
    int length;//长度
}SequenceList;

//第二种定义
/*typedef struct SequenceList {
    ElemType elem[MAXSIZE];//顺序表的数组
    int length;//长度
}SequenceList;*/

/*
 * TODO:初始化
 * 顺序表的初始化
 * */
//第一种定义的初始化
void InitList(SequenceList &L){
    
    //初始化
    //C++写法
    L.elem = new ElemType[MAXSIZE];//为顺序表分配一个大小为MAXSIZE的数组空间
    //C写法
    //L.elem = malloc(MAXSIZE * sizeof(ElemType));
    if(L.elem){
    
    //申请地址成功
        L.length = 0;//初始化顺序表长度0
    }
    else
        cout<<"初始化失败!"<<endl;
}

//第二种定义的初始化
/*void InitList(SequenceList &L){
    L.elem = new ElemType;//为顺序表申请一个基地址空间
    if(L.elem){
        L.length = 0;
    }
    else
        cout << "初始化失败!" << endl;
}*/

/*
 *TODO:取值
 * 获取顺序表中第i个元素的值,通过变量e返回
 * */
void GetElem(SequenceList L,int i,ElemType &e){
    
    //取值
    if(i<=0 || i>L.length)//i值非法
        cout << "i值非法,取值失败!" << endl;
    e = L.elem[i-1];
}

/*
 * TODO:查找
 * 根据指定的元素值e,查找该元素在顺序表中的位序(第几个)
 * */
int LocateElem(SequenceList L,ElemType e){
    
    //查找
    if(L.elem){
    
    
        for(int i=0;i<L.length;i++){
    
    
            if(L.elem[i] == e)
                return i+1;
        }
        return 0;
    }
}

/*
 * TODO:插入
 * 在顺序表的第i个位置插入一个新的元素e,表的长度加1
 * */
void InsertList(SequenceList &L,int i,ElemType e){
    
    //插入
    if(i<=0 || i>L.length+1)//最多插入第 L.length 个位置;
        cout<<"i值非法,插入失败!"<<endl;
    else if(L.length == MAXSIZE)
        cout<<"顺序表已满,插入失败!"<<endl;
    else{
    
    
        for(int j = L.length-1;j >= i-1;j--){
    
    //腾空位
            L.elem[j+1] = L.elem[j];
        }
        L.elem[i-1] = e;//第i个位置为e
        ++L.length;
    }
}

/*
 * TODO:删除
 * 删除顺序表的第i个元素,表的长度减1
 * */
void DeleteList(SequenceList &L,int i){
    
    //删除
    if(i<=0 || i>L.length)
        cout<<"i值非法,删除失败!"<<endl;
    else{
    
    
        for(int j = i;j <= L.length-1;j++)
            L.elem[j-1] = L.elem[j];
        --L.length;
    }
}

/*
 * TODO:遍历
 * 遍历并输出顺序表
 * */
void TraverseList(SequenceList L){
    
    
    for(int i=0;i<L.length;i++)
        cout<<L.elem[i]<<" ";
    cout<<endl;
}

猜你喜欢

转载自blog.csdn.net/weixin_45580017/article/details/124058712
今日推荐