顺序表
虽然之前写过顺序表,但是没用模板所以很不方便,于是这次补一个顺序表的模板,写得比较仓促也比较简单,虽然没有注释但应该不难懂
#pragma once
#include <iostream>
#define MAX_LIST_SIZE 20
using namespace std;
enum Status { ERROR = 0, OK = 1 };
template<typename ElemType>
struct SqList
{
ElemType data[MAX_LIST_SIZE + 1];
int length;
};
template<typename ElemType>
Status initSqList(SqList<ElemType>& L)
{
L.length = 0;
return OK;
}
template<typename ElemType>
bool isSqListEmpty(SqList<ElemType>& L)
{
return L.length == 0;
}
template<typename ElemType>
bool isSqListFull(SqList<ElemType>& L)
{
return L.length == MAX_LIST_SIZE;
}
template<typename ElemType>
Status insertSqList(SqList<ElemType>& L, ElemType data, int index)
{
if (index < 1 || index > L.length + 1)
{
cout << "位置错误" << endl;
return ERROR;
}
if (isSqListFull(L))
{
cout << "表满" << endl;
return ERROR;
}
for (int i = L.length; i >= index; i--)
L.data[i + 1] = L.data[i];
L.data[index] = data;
L.length++;
return OK;
}
template<typename ElemType>
Status delSqList(SqList<ElemType>& L, ElemType& data, int index)
{
if (index < 1 || index > L.length)
{
cout << "位置错误" << endl;
return ERROR;
}
if (isSqListEmpty(L))
{
cout << "表空" << endl;
return ERROR;
}
for (int i = index; i < L.length; i++)
L.data[i] = L.data[i + 1];
L.length--;
return OK;
}