-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_1.cpp
84 lines (84 loc) · 1.31 KB
/
2_1.cpp
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
#include<iostream>
using namespace std;
typedef int datatype;
const int maxsize = 5;
typedef struct{
datatype data[maxsize];
int n;
}sqlist;
void initiate(sqlist *l){
int i = 0;
datatype data;
cin >> data;
l -> data[i] = data;
while(i < maxsize - 1){
i++;
cin >> data;
if(data == 0)
break;
else
l -> data[i] = data;
}
if(data == 0)
i--;
l -> n = i + 1;
}
int length(sqlist *l){
return l->n;
}
datatype get(sqlist *l,int i){
if(l->n == 0)
return -1;
return l -> data[i - 1];
}
int locate(sqlist*l,datatype x){
int n = l->n;
if(n == 0)
return -1;
for(int i = 0;i < n;i++){
if(x == l->data[i])
return i + 1;
}
return -1;
}
int insert(sqlist *l, datatype x, int i){
if(i > l ->n || i < 0){
return 0;
}
if(l -> n == maxsize)
return -1;
for(int j = l->n - 1;j >= i - 1;j--){
l->data[j + 1] = l ->data[j];
}
l -> data[i - 1] = x;
l -> n++ ;
return 1;
}
int deleteData(sqlist *l, int i){
if(i > l->n || i < 0)
return 0;
if(l -> n == 0)
return -1;
for(int j = i - 1;j + 1< l->n;j++){
l -> data[j] = l->data[j + 1];
}
l -> n--;
return 1;
}
int print(sqlist *l){
int j = l->n;
while(j-- > 0){
cout << l -> data[l -> n - j - 1];
cout << " ";
}
cout<<endl;
}
int main(void){
sqlist list;
sqlist *l = &list;
initiate(l);
print(l);
deleteData(l, 2);
print(l);
return 0;
}