Repository navigation
Expand file tree
/
Copy pathtest2.cpp
More file actions
174 lines (157 loc) · 2.68 KB
/
Copy pathtest2.cpp
File metadata and controls
174 lines (157 loc) · 2.68 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
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
typedef struct Sqlist
{
int* arr;
int count;
int length;
}SEQ;
void init_Sqlist(SEQ *sq,int length)
{
sq->arr = (int*)malloc(sizeof(int) * length);
if (sq->arr == NULL)
{
printf("动态分配内存失败");
exit(-1);
}
else
{
sq->count = 0;
sq->length = length;
printf("分配内存成功,长度为%d\n", sq->length);
}
}
bool is_empty(SEQ* sq)
{
if (sq->count == 0)
return true;
else
return false;
}
bool is_full(SEQ *sq)
{
if (sq->count == sq->length)
return true;
else
return false;
}
void show_Sqlist(SEQ *sq)
{
if (is_empty(sq))
{
printf("数组内容为空\n");
}
for (int i = 0; i < sq->count; i++)
{
printf("第%d 个数组内容为%d\n",(i+1),sq->arr[i]);
}
}
bool append_Sqlist(SEQ *sq,int val)
{
if (is_full(sq))
{
return false;
}
else
{
sq->arr[sq->count] = val;
sq->count++;
return true;
}
}
bool insert_Sqlist(SEQ *sq,int pos,int val)
{
if (is_full(sq))
return false;
if (pos<1 || pos>sq->count + 1)
return false;
for (int i = sq->count - 1; i >= pos - 1; i--)
{
sq->arr[i + 1] = sq->arr[i];
}
sq->arr[pos - 1] = val;
sq->count++;
return true;
}
bool delete_Sqlist(SEQ* sq, int pos, int* e)
{
if (is_empty(sq))
return false;
if (pos<1 || pos >sq->count)
return false;
*e = sq->arr[pos-1];
int i = pos - 1;
for (i;i<sq->count-1;i++)
{
sq->arr[i]=sq->arr[i+1];
}
sq->count--;
}
void inversion_Sqlist(SEQ *sq)
{
int i = 0;
int j = sq->count - 1;
int t;
while (i<j)
{
t = sq->arr[i];
sq->arr[i] = sq->arr[j];
sq->arr[j] = t;
i++;
j--;
}
}
void sort_Sqlist(SEQ *sq)
{
int i;
int j;
for (i = 0; i < sq->count; i++)
{
for (j=i+1;j<sq->count;j++)
{
if (sq->arr[i] > sq->arr[j])
{
int t;
t = sq->arr[i];
sq->arr[i] = sq->arr[j];
sq->arr[j] = t;
}
}
}
}
int main()
{
SEQ sq;
init_Sqlist(&sq,6);
append_Sqlist(&sq, 12);
append_Sqlist(&sq, 13);
append_Sqlist(&sq, 14);
append_Sqlist(&sq, 51);
append_Sqlist(&sq, 14);
show_Sqlist(&sq);
printf("-------------\n");
sort_Sqlist(&sq);
show_Sqlist(&sq);
/*int e;
printf("-------------\n");
delete_Sqlist(&sq,2,&e);
show_Sqlist(&sq);
inversion_Sqlist(&sq);*/
/*printf("-------------\n");
inversion_Sqlist(&sq);
show_Sqlist(&sq);*/
/*show_Sqlist(&sq);
if(!insert_Sqlist(&sq, 2, 4))
printf("满了");
printf("-------------\n");
show_Sqlist(&sq);*/
/*append_Sqlist(&sq, 51);
append_Sqlist(&sq, 1);
append_Sqlist(&sq, 1);
if (append_Sqlist(&sq, 1))
printf("满了");
show_Sqlist(&sq);
insert_Sqlist(&sq,1,3 );*/
sort_Sqlist(&sq);
}