-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest6.cpp
More file actions
185 lines (178 loc) · 2.89 KB
/
test6.cpp
File metadata and controls
185 lines (178 loc) · 2.89 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct Seqlist
{
int* pBase;
int length;
int count;
}Seqlist;
void init_Seqlist(Seqlist *seq,int length)
{
seq->pBase=(int*)malloc(sizeof(int) * length);
if (seq->pBase == NULL)
printf("动态分配内存失败\n");
else
{
printf("动态分配内存成功\n");
printf("------------------------\n");
seq->length = length;
seq->count = 0;
}
}
bool is_full(Seqlist* seq)
{
if (seq->count == seq->length)
return true;
else
return false;
}
bool is_empty(Seqlist* seq)
{
if (seq->count == 0)
return true;
else
return false;
}
void append_Seqlist(Seqlist* seq, int val)
{
if (is_full(seq))
printf("表已满,无法继续插入\n");
else
seq->pBase[seq->count] = val;
(seq->count)++;
}
void show_Seqlist(Seqlist* seq)
{
if (is_empty(seq))
printf("表为空\n");
else
{
for (int i=0;i<seq->count;i++)
{
printf("%d ", seq->pBase[i]);
}
printf("\n");
}
}
void insert_Seqlist(Seqlist* seq, int pos, int val)
{
if (is_full(seq))
printf("表已满,无法继续插入\n");
else if (pos<=0 || pos>seq->count)
{
printf("位置输入有误");
}
else
{
int i= seq->count - 1;
for (i;i>=pos-1 ;i--)
{
seq->pBase[i+1]= seq->pBase[i];
}
seq->pBase[pos - 1] = val;
seq->count++;
}
}
void delete_Seqlist(Seqlist* seq, int pos, int* e)
{
if (is_empty(seq))
printf("表为空");
else if (pos <= 0 || pos > seq->count)
{
printf("位置输入有误");
}
else
{
int i = pos - 1;
*e = seq->pBase[pos - 1];
for (i;i<=pos;i++)
{
seq->pBase[i] = seq->pBase[i + 1];
}
seq->count--;
}
}
void sort_Seqlist(Seqlist* seq)
{
if (is_empty(seq))
printf("表为空\n");
else
{
int i, j, t;
for ( i = 0; i < seq->count; i++)
{
for ( j = i+1; j < seq->count; j++)
{
if (seq->pBase[i] > seq->pBase[j])
{
t = seq->pBase[i];
seq->pBase[i] = seq->pBase[j];
seq->pBase[j] = t;
}
}
}
}
}
void inversion_sqlist(Seqlist *seq)
{
if (is_empty(seq))
printf("表为空\n");
else
{
int i=0;
int j=seq->count-1;
int t;
while (i<j)
{
t = seq->pBase[i];
seq->pBase[i] = seq->pBase[j];
seq->pBase[j] = t;
i++;
j--;
}
}
}
void find_ElemPos(Seqlist *seq,int pos,int *e)
{
if (is_full(seq))
printf("表已满\n");
else if (pos <= 0 || pos > seq->count)
{
printf("位置输入有误");
}
*e = seq->pBase[pos - 1];
}
void find_Elem(Seqlist* seq, int e, int* pos)
{
for (int i = 0; i < seq->count; i++)
{
if (e == seq->pBase[i])
{
*pos = i+1;
}
}
}
int main()
{
Seqlist seq;
init_Seqlist(&seq,6);
append_Seqlist(&seq, 7);
append_Seqlist(&seq, 3);
append_Seqlist(&seq, 1);
/*show_Seqlist(&seq);
insert_Seqlist(&seq, 2, 5);*/
show_Seqlist(&seq);
//int e;
//delete_Seqlist(&seq, 4, &e);
//sort_Seqlist(&seq);
//inversion_sqlist(&seq);
show_Seqlist(&seq);
//printf("%d", e);
/*int e;
find_ElemPos(&seq, 1, &e);
printf("%d", e);*/
int pos;
find_Elem(&seq,3, &pos);
printf("该元素位置在%d", pos);
}