数据结构之顺序线性表操作(C++实现-C风格写法) 完整代码

稍微bb几句,最近想好好学习,所以把游戏都卸载了,下面开始正题哈哈。

一、线性表的创建和一些基本的操作

下边是一些预先的定义

#define OK 1	//	定义返回状态
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 20 // 定义线性表的最大长度。
typedef int Status;
typedef int ElemType;
typedef struct
{
	ElemType data[MAXSIZE];
	int length;
}SqList;		// 定义线性表的结构

下边是线性表的初始化操作

Status InitList(SqList *L)
{
	L->length = 0;
	return OK;
}

接下来是线性表的GetElem操作:(获得下标为i的元素的值)

Status GetElem(SqList L, int i, ElemType *e)
{
	if(i < 0 || i > L.length )
		return ERROR;
	else
		*e = L.data[i];
	return OK;
}

算了,我把我完整的代码贴出来吧,反正太基础没人看。

#include <iostream>

using namespace std;
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 2

typedef int ElemType;
typedef int Status;
typedef struct
{
    ElemType data[MAXSIZE];
    int length;
}SqList;



Status GetElem(SqList L, int i, ElemType *e);
Status ListInsert(SqList *L, int i, ElemType e);
Status InitList(SqList *);
Status Insert(SqList *, int, ElemType);
Status DeleteElem(SqList *, int, ElemType *);
void print();
int main()
{
    int isInit = FALSE;
    SqList L;
    ElemType e;
    int choice , index;
    InitList(&L);
    cout << "OK,为您初始化了一张表" << endl  << endl;
    print();

    while(cin >> choice)
    {
        switch(choice)
        {
        case 1:
            InitList(&L);
            cout << "OK,初始化成功" << endl;
            break;
        case 2:
            if(L.length == MAXSIZE)
            {
                cout << "表已经满了,不能再插入了哦" << endl;
                break;
            }
            cout << "请输入index和e的值" << endl;
            cin >> index >> e;
            Insert(&L, index, e);
            break;
        case 3:
            cout << "请输入index的值" << endl;
            cin >> index;
            GetElem(L, index, &e);
            cout << "The value of e is:" << e << endl;
            break;
        case 4:
            cout << "请输入index的值" << endl;
            cin >> index;
            DeleteElem(&L, index, &e);
            cout << "删除了" << index << "位置上的元素:" << e << endl;
            break;
        case 5:
            if(L.length == 0)
                cout << "线性表是空的" << endl;
            else
            {
                cout << "下边打印线性表存储的元素" << endl;
                for(index = 0; index < L.length; index++)
                    cout << L.data[index] << "\t" ;
                cout << endl;
            }

            break;
        }
        print();
    }
    InitList(&L);
    Insert(&L, 0, 10);
    Insert(&L, 10, 10);
    Insert(&L, 1, 10);
    Insert(&L, 2, 10);
    GetElem(L, 0, &e);
}
Status InitList(SqList *L)
{
    L->length = 0;
    return OK;
}
Status GetElem(SqList L, int i, ElemType *e)
{
    if(i < 0 || i > L.length-1)
    {
        cout << "访问出错啦超出了表的范围"  << endl;
        return ERROR;
    }

    else
        *e = L.data[i];
    cout << "获取成功" << "\t" << *e << endl;
    return OK;
}
Status Insert(SqList *L, int i, ElemType e)
{
    int k;
    // 插入的时候不能超过MAXSIZE,L->length == MAXSIZE则表已经满了
    if(i < 0 || i > L->length || L->length == MAXSIZE)
    {
        cout << "插入位置超出了表的范围"  << endl;
        return ERROR;
    }

    else
    {
        for(k = L->length; k > i; k--)  // 如果插入在i = k的位置,则不用移动直接插入。
        {
            L->data[k] = L->data[k-1];
        }
    }
    L->data[i] = e;
    cout << "Insert successfully" << endl;
    L->length++;
    if(L->length ==MAXSIZE)
        cout << "线性表已经插入完全了" << endl;
}
Status DeleteElem(SqList *L, int i, ElemType *e)
{
    int k;
    if(i < 0 || i > L->length-1)
        return ERROR;
    else
    {
        *e = L->data[i];
        for(k = i; k < L->length; k++)
        {
            L->data[k] = L->data[k+1];
        }
    }
    cout << "Delete Successfully" << endl;
}
void print()    // 打印功能
{
    cout << "您正在使用线性表" << endl
            << "输入1 重新初始化线性表" << endl
            << "输入2 将元素插入线性表" << endl
            << "输入3 获取index位置的元素的值" << endl
            << "输入4 删除指定位置的元素" << endl
            << "输入5 打印线性表" << endl;
}

好了那就先到这啦。希望我能坚持下去。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值