数据结构1.1

数据结构第一次作业之顺序表

代码下载

点击下载源代码

代码展示

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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include<stdio.h>
#include<stdlib.h>

/*
该顺序表所能存放的数为int整型(-32768~32767之间的整数)
*/

#define MAXSIZE 100//顺序表最大长度



/*定义顺序表*/
typedef struct
{
int data[MAXSIZE];
int length;
}ShunXuList;

int IsNum()
{
char ch[10];
scanf("%s", ch);
int i = 1;
while(ch[i-1]!='\0'){
if(ch[i-1] < '0' || ch[i-1] > '9'){
printf("输入格式错误\n");
printf("请输入需要输入整数的个数(1~100之间的整数):");
scanf("%s", ch);
i = 1;
}
else{
i++;
}
}

int number = atoi(ch);
return number;
}

//判断输入的是不是整数类型数据
int IsDataNum()
{
char ch[10];
scanf("%s",ch);
int i = 1;
while(ch[i-1]!='\0'){
if(ch[i-1] < '0' || ch[i-1] > '9'){
printf("输入格式错误\n");
printf("请输入-32768~32767之间的整数:");
scanf("%s", ch);
i = 1;
}
else{
i++;
}
}

int number = atoi(ch);
return number;
}

//判断输入的数据是不是小于等于数据个数
int IsLengthNum(ShunXuList *shunXuList)
{
char ch[10];
scanf("%s",ch);
int i = 1;
while(ch[i-1]!='\0'){
if(ch[i-1] < '0' || ch[i-1] > '9'){
printf("输入格式错误\n");
printf("请0~%d之内的整数:",shunXuList->length);
scanf("%s", ch);
i = 1;
}
else{
i++;
}
}

int number = atoi(ch);

return number;
}

//删除顺序表中相同部分的数据
void DeleteSameList(ShunXuList *shunXuList)
{
for (int i = 1; i <= shunXuList->length; i++){
for (int j = i + 1;j <= shunXuList->length; j++){
if (shunXuList->data[i - 1] == shunXuList->data[j - 1]){
DeletePositionList(shunXuList,j);
j--;
}
}
}

PrintList(shunXuList);

}

//按照key值删除顺序表中的数据
void DeleteKeyList(ShunXuList *shunXuList)
{
printf("请输入key值:\n");
int number = IsDataNum();

for(int i = 1; i <= shunXuList->length; i++){
if (number == shunXuList->data[i-1]){
DeletePositionList(shunXuList,i);
i--;
}
}

PrintList(shunXuList);

}

void AddList(ShunXuList *shunXuList)
{
printf("请输入需要插入的数(-32768~32767之间):\n");
int num = IsDataNum();
printf("请输入插入的位置(小于等于%d):\n",shunXuList->length);
int position = IsLengthNum(shunXuList);
for(int i = shunXuList->length; i >= position; i--){
shunXuList->data[i] = shunXuList->data[i-1];
}

shunXuList->data[position-1] = num;

shunXuList->length++;

PrintList(shunXuList);
}

void DeletePositionList(ShunXuList *shunXuList,int position)
{
shunXuList->length--;
for(int i = position; i <= shunXuList->length; i++){
shunXuList->data[i-1] = shunXuList->data[i];
}

}

//释放空间
void ExitList(ShunXuList *shunXuList)
{
free(shunXuList);
printf("空间释放成功");
getchar();
exit(1);
}

void ChuangJian(ShunXuList *shunXuList)
{
printf("请输入需要输入整数的个数(1~100之间的整数):");
int number; //存放输入数据的个数
number = IsNum();
shunXuList->length = number;

for (int i = 1; i <= number; i++){
printf("请输入第%d个数据:",i);
shunXuList->data[i-1] = IsDataNum();
}

}
void PrintList(ShunXuList *shunXuList)
{
printf("开始打印顺序表\n");
for (int i = 1; i <= shunXuList->length; i++){
printf("第%d个数据为:%d\n",i,shunXuList->data[i-1]);
}
}

void Menu(ShunXuList *shunXuList)
{

char ch[10];
scanf("%s",ch);
while(ch[1]!='\0' || ch[0] <= '0' || ch[0] >= '6'){
printf("请键入1~5的整数");
scanf("%s",ch);
}
switch (ch[0])
{
case '1':
DeleteSameList(shunXuList);
break;
case '2':
DeleteKeyList(shunXuList);
break;
case '3':
AddList(shunXuList);
break;
case '4':
printf("请输入想删除的节点位置\n");
DeletePositionList(shunXuList,IsLengthNum(shunXuList));
PrintList(shunXuList);
break;
case '5':
ExitList(shunXuList);
break;
}

}

int main()
{
ShunXuList *shunXuList = NULL;
shunXuList = (ShunXuList *)malloc(sizeof(ShunXuList));
shunXuList->length = 0;
ChuangJian(shunXuList);

PrintList(shunXuList);
printf("您已成功创建顺序表\n*******************************************\n");
printf("请输入你想进行的操作\n");
printf("1:删除重复的节点\n");
printf("2:删除节点中所有的key值\n");
printf("3:插入一个数\n");
printf("4:删除某个节点的数据\n");
printf("5:退出程序\n");

Menu(shunXuList);


}