请用c语言实现:建立顺序表从有序的顺序表中删除所有其值重复的元素,使表中所有元素的值均不同
时间: 2023-11-10 09:09:05 浏览: 95
```
#include <stdio.h>
#define MAXSIZE 100
typedef struct {
int data[MAXSIZE];
int length;
} SqList;
void initList(SqList *L) {
int i;
for (i = 0; i < MAXSIZE; i++) {
L->data[i] = 0;
}
L->length = 0;
}
void printList(SqList L) {
int i;
for (i = 0; i < L.length; i++) {
printf("%d ", L.data[i]);
}
printf("\n");
}
void deleteRepeat(SqList *L) {
int i, j, k;
for (i = 0; i < L->length; i++) {
k = L->data[i];
for (j = i + 1; j < L->length;) {
if (L->data[j] == k) {
L->data[j] = L->data[--L->length];
} else {
j++;
}
}
}
}
int main() {
SqList L;
initList(&L);
L.length = 10;
L.data[0] = 1;
L.data[1] = 2;
L.data[2] = 2;
L.data[3] = 3;
L.data[4] = 4;
L.data[5] = 4;
L.data[6] = 4;
L.data[7] = 5;
L.data[8] = 6;
L.data[9] = 6;
printf("原始顺序表:\n");
printList(L);
deleteRepeat(&L);
printf("删除重复元素后的顺序表:\n");
printList(L);
return 0;
}
```
阅读全文