-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathADT
83 lines (74 loc) · 1.61 KB
/
ADT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include<iostream>
using namespace std;
#include<string>
const int maxsize = 20;
template<class Datatype>
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<class Datatype>
Seqlist<Datatype>::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<class Datatype>
Datatype Seqlist<Datatype>::pos_find(int i) {
return list[i];
}
template<class Datatype>
int Seqlist<Datatype>::val_find(Datatype a) {
for (int i = 0; i < length; i++)
{
if (list[i] == a)return(i);
}
return -1;
}
template<class Datatype>
void Seqlist<Datatype>::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<class Datatype>
void Seqlist<Datatype>::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<class Datatype>
void Seqlist<Datatype>::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 <int>ll(a, 10);
ll.Printlist();
cout << endl;
ll.Data_delete(5);
cout << endl;
ll.Printlist();
return 0;
}