数据结构(1)———线性表

				**数据结构之线性表## 标题**

#include
using namespace std;
#include

const int maxsize = 20;
template
class Seqlist {
private:
Datatype list[maxsize];
int length;
public:
Seqlist() { length = 0; }
Seqlist(Datatype list[], int length);
int Length() { return length; }
Datatype pos_find(int i);//输入位置返回值
int val_find(Datatype i);//输入值(Datatype)返回位置(int)
void insert(int i, Datatype x);
void Data_delete(int i);
void Printlist();
};

template
Seqlist::Seqlist(Datatype a[], int n) {
if (n > maxsize) throw “wrong parameter”;
for (int i = 0; i < n; i++) {
list[i] = a[i];
}
length = n;
}

template
Datatype Seqlist::pos_find(int i) {
return list[i];
}

template
int Seqlist::val_find(Datatype a) {
for (int i = 0; i < length; i++)
{
if (list[i] == a)return(i);
}
return -1;
}

template
void Seqlist::insert(int i, Datatype x) {
if (length + 1 > maxsize)throw “wrong parameter”;
for (int j = length; j >= i; j–) {
list[j] = list[j - 1];
}
list[i] = x;
length++;
}

template
void Seqlist::Data_delete(int i) {
if (length == 0)throw"wrong parameter";
for (int j = i; j < length - 1; j++)
{
list[j] = list[j + 1];
}
list[length - 1] = NULL;
length–;
}

template
void Seqlist::Printlist()
{
for (int i = 0; i < length; i++)cout << list[i] << " ";
}

int main()
{
int a[10] = { 1,2,3,4,5,6,7,8,9,0 };
Seqlist ll(a, 10);
ll.Printlist();
cout << endl;
ll.Data_delete(5);
cout << endl;
ll.Printlist();
return 0;
}

你可能感兴趣的:(小白,其他)