序列E:2 18 29 18 41 55 41 76 41 92 根据序列E的数据,用c++写出完整的程序,实现顺序表的构建、删除表中重复元素算法,
时间: 2024-04-29 18:23:20 浏览: 7
以及输出最终的顺序表。
```c
#include <stdio.h>
#define MAXSIZE 10
typedef struct {
int data[MAXSIZE];
int length;
} SqList;
void InitList(SqList *L) {
L->length = 0;
}
void InsertList(SqList *L, int elem) {
if (L->length == MAXSIZE) {
printf("顺序表已满,无法插入!\n");
return;
}
L->data[L->length] = elem;
L->length++;
}
void DeleteDup(SqList *L) {
int i, j, k;
for (i = 0; i < L->length; i++) {
for (j = i + 1; j < L->length; j++) {
if (L->data[i] == L->data[j]) {
for (k = j; k < L->length - 1; k++) {
L->data[k] = L->data[k + 1];
}
L->length--;
j--;
}
}
}
}
void PrintList(SqList L) {
int i;
for (i = 0; i < L.length; i++) {
printf("%d ", L.data[i]);
}
printf("\n");
}
int main() {
SqList L;
InitList(&L);
InsertList(&L, 2);
InsertList(&L, 18);
InsertList(&L, 29);
InsertList(&L, 18);
InsertList(&L, 41);
InsertList(&L, 55);
InsertList(&L, 41);
InsertList(&L, 76);
InsertList(&L, 41);
InsertList(&L, 92);
printf("原始数据:");
PrintList(L);
DeleteDup(&L);
printf("删除重复元素后:");
PrintList(L);
return 0;
}
```
输出结果为:
```
原始数据:2 18 29 18 41 55 41 76 41 92
删除重复元素后:2 18 29 41 55 76 92
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)