-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSeqList.c
204 lines (187 loc) · 3.36 KB
/
SeqList.c
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<windows.h>
#include<assert.h>
#include"SeqList.h"
//初始化
void InitSeqList(pSeqList ppseqlist)
{
assert(ppseqlist);
memset(ppseqlist->arr,0,sizeof(DataType)*MAX);
ppseqlist->count = 0;
}
//显示顺序表内容
void show(pSeqList ppseqlist)
{
int i = 0;
assert(ppseqlist);
if (0 == ppseqlist->count)
{
printf("顺序表为空\n");
return;
}
for (i = 0; i < ppseqlist->count; i++)
{
printf("%d ", ppseqlist->arr[i]);
}
}
//从后面放入一个数据
void PushBack(pSeqList ppseqlist)
{
DataType data = 0;
assert(ppseqlist);
if ((ppseqlist->count) == MAX)
{
printf("空间已满,请执行其他操作\n");
return;
}
printf("请输入你需要增加的数\n");
scanf("%d",&data);
ppseqlist->arr[ppseqlist->count] = data;
ppseqlist->count++;
}
//从后面取出一个数据
int PopBack(pSeqList ppseqlist)
{
assert(ppseqlist);
//判断顺序表是否为空
if (0 == ppseqlist->count)
{
printf("顺序表为空\n");
return -1;
}
ppseqlist->count--;
return ppseqlist->arr[(ppseqlist->count)];
}
//前面插入一个数据
void PushFront(pSeqList ppseqlist)
{
DataType data = 0;
int i = 0;
assert(ppseqlist);
if ((ppseqlist->count) == MAX) //判断空间是否已经满了
{
printf("空间已满,请执行其他操作\n");
return;
}
printf("请输入你需要增加的数\n");
scanf("%d", &data);
if (0 == ppseqlist->count) //判断第一个位置是否有数
{
ppseqlist->arr[0] = data;
ppseqlist->count++;
}
else
{
for (i = (ppseqlist->count) - 1; i >= 0; i--)
{
ppseqlist->arr[i + 1] = ppseqlist->arr[i];
}
ppseqlist->arr[0] = data;
ppseqlist->count++;
}
}
//从前面取出一个数据
int PopFront(pSeqList ppseqlist)
{
int i = 0;
int data = ppseqlist->arr[0];
assert(ppseqlist);
//判断顺序表是否为空
if (0 == ppseqlist->count)
{
printf("顺序表为空\n");
return -1;
}
for (i = 0; i < (ppseqlist->count)-1; i++)
{
ppseqlist->arr[i] = ppseqlist->arr[i + 1];
}
ppseqlist->count--;
return data;
}
//查找某一元素,返回该元素的下标
int Find(pSeqList ppseqlist)
{
int i = 0;
int data = 0;
assert(ppseqlist);
//判断顺序表是否为空
if (0 == ppseqlist->count)
{
printf("顺序表为空\n");
return -1;
}
printf("请输入你需要查找的数>:");
scanf("%d", &data);
for (i = 0; i < ppseqlist->count; i++)
{
if (ppseqlist->arr[i] == data)
{
return i;
}
}
printf("顺序表中没有该数据\n");
return -1;
}
void Remove(pSeqList ppseqlist)
{
int data = 0;
int i = 0;
assert(ppseqlist);
//判断顺序表是否为空
if (0 == ppseqlist->count)
{
printf("顺序表为空\n");
return;
}
printf("请输入你需要删除的数>:");
scanf("%d", &data);
for (i = 0; i < (ppseqlist->count); i++)
{
if (ppseqlist->arr[i] == data)
{
ppseqlist->arr[i] = ppseqlist->arr[i + 1];
ppseqlist->count--;
printf("数据已删除\n");
return;
}
}
printf("顺序表中没有该数据\n");
return;
}
//清空所有的数据
void RemoveAll(pSeqList ppseqlist)
{
assert(ppseqlist);
ppseqlist->count = 0;
printf("顺序表全部清空\n");
}
//排序
void Sort(pSeqList ppseqlist)
{
int i = 0, j = ppseqlist->count-1;
int temp = 0;
int flag = 0;
assert(ppseqlist);
//判断顺序表是否为空
if (0 == ppseqlist->count)
{
printf("顺序表为空\n");
return;
}
for (; j > 0; j--)
{
for (i = 0; i <= j; i++)
{
if ((ppseqlist->arr[flag]) > (ppseqlist->arr[i]))
{
flag = i;
}
temp = ppseqlist->arr[flag];
ppseqlist->arr[flag] = ppseqlist->arr[j];
ppseqlist->arr[j] = temp;
}
}
printf("排序成功\n");
}