-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsequential-list.cpp
More file actions
93 lines (92 loc) · 2.12 KB
/
sequential-list.cpp
File metadata and controls
93 lines (92 loc) · 2.12 KB
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
84
85
86
87
88
89
90
91
92
93
#include< iostream >
using namespace std;
const int maxsize=1000;
template < class T >
class Seqlist{
public:
Seqlist():length(0) {}
Seqlist(const T a[],int n);
int Getlength();
void Printlist();
void Insert(int i,T x);
T Delete(int i);
T Get(int i);
int Locate(T x);
private:
T date[maxsize];
int length;
};
template < class T >
Seqlist< T >::Seqlist(const T a[],int n){
if(n>maxsize)
throw "error";
for(int i=0;i<n;i++){
date[i]=a[i];
}
length=n;
}
template < class T >
int Seqlist< T >::Getlength(){
return length;
}
template < class T >
void Seqlist< T >::Printlist(){
cout<<"遍历线性表的各个数据元素"<<endl;
for(int i=0;i<length;i++){
cout<<date[i]<<' ';
}
cout<<endl;
}
template < class T >
void Seqlist< T >::Insert(int i,T x){
if(length>maxsize||i<1||i>length+1)
throw "error";
for(int j=length;j>=i;j--){
date[j]=date[j-1];
}
date[i-1]=x;
length++;
}
template < class T >
T Seqlist< T >::Delete(int i){
if(length==0||i<1||i>length)
throw "error";
T x=date[i-1];
for(int j=i;j<length;j++){
date[j-1]=date[j];
}
length--;
return x;
}
template < class T >
T Seqlist< T >::Get(int i){
if(i<1||i>length)
throw "error";
return date[i-1];
}
template < class T >
int Seqlist< T >::Locate(T x){
for(int i=0;i<length;i++){
if(date[i]==x)
return i+1;
}
return 0;
}
int main(){
int a[1000],n;
cout<<"输入元素个数(<1000)"<<endl;
cin>>n;
cout<<"输入元素"<<endl;
for(int i=0;i<n;i++)
cin>>a[i];
Seqlist < int > list1(a,n);
list1.Printlist();
cout<<"向位置2插入元素0后";
list1.Insert(2,0);
list1.Printlist();
int x=list1.Delete(4);
cout<<"删除元素:"<<x<<endl;
list1.Printlist();
int p=list1.Locate(4);
cout<<"元素4的位置:"<<p<<endl;
}