1.順序表的存儲類型
#include <bits/stdc++.h>
using namespace std;
#define MaxSize 50
typedef int ElemType;
typedef struct {
ElemType *data;
int length;
}SqList;
2.順序表的一些基本操作
/// 將一個元素差入到順序表的第i個位置之前。
bool ListInsert(SqList &L, int i, ElemType e){
if(i<1 || i>L.length+1)
return false;
if (L.length>=MaxSize)
return false;
for(int j = L.length; j>=i; --j)
L.data[j] = L.data[j-1];
L.data[i-1] = e;
L.length += 1;
return true;
}
///將順序表第i個元素刪除,並且返回給e
bool ListDelete(SqList &L, int i, ElemType &e)
{
if(1>i || i>L.length)
return false;
e = L.data[i-1];
for(int j=i;j<L.length;++j)
L.data[j-1] = L.data[j];
L.length -= 1;
return true;
}
/// 按值查找。查找元素e在列表中的的位置
int LocateElem(SqList L, ElemType e)
{
int i = 0;
for(;i<L.length;++i)
if(L.data[i] == e)
return i+1;
return 0;
}
/// 將順序表內的東西全部輸出(按順序)
void ListPrint(SqList L)
{
for(int i=0;i<L.length;++i)
{
cout<<L.data[i]<<' ';
}
cout<<'\n';
}
注:參考資料《王道考研-數據結構》。