【数据结构】线性表之顺序表(3)(函数功能实现:删除线性表中的重复元素)
【数据结构】线性表之顺序表(3)(函数功能实现:删除线性表中的重复元素)
问题描述:删除线性表L=(a0,a1,…,an-1)中的重复元素;即list_purge(sqlink L)函数。
算法思路:对L中每个ai(0 ≤ i ≤ n-2),依次与aj(i+1≤ j≤ n-1)比较,若与ai相等,则删除之。
1 依次取i,i∈[1,last]
//如果数组中只有一个数,则不需要对比;所以i从第1位置取,故i∈[1,last]。
2 判断ai元素值在不在[0,i-1]中,若不在,继续向后判断。
若存在,则删除i位置上的元素,删除一个元素,后面的元素向前补,所以还要重新比较i位置上的元素。
代码实现:
实现一个功能函数需要三个文件(三个文件在同一目录)。
sqlist.h(定义顺序表)
sqlist.c(实现接口函数)
test.c(主函数实现)
sqlist.h
主要加入int list_purge(sqlink L);
完整代码如下:
typedef int data_t;
#define N 128
typedef struct {
data_t data[N];
int last;
}sqlist, *sqlink;
sqlink list_create();
int list_clear(sqlink L);
int list_empty(sqlink L);
int list_length(sqlink L);
int list_locate(sqlink L, data_t value);
int list_insert(sqlink L, data_t value, int pos);
int list_delete(sqlink L);
int list_show(sqlink L);
int list_delete_pos(sqlink L, int pos);
int list_merge(sqlink L1, sqlink L2);
int list_purge(sqlink L);
sqlist.c
主要实现 list_purge(sqlink L)函数
int list_purge(sqlink L){
int i,j;
if (L->last == 0)
return 0;
i = 1;
while (i <= L->last){
j = i-1;
while (j >= 0){
if (L->data[i] == L->data[j]){
list_delete_pos(L, i);
break;
}
else{
j–;
}
}
if (j < 0) {
i++;
}
}
return 0;
}
sqlist.c完整代码如下:
#include "sqlist.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
sqlink list_create(){
//malloc
sqlink L;
L =(sqlink)malloc(sizeof(sqlist));
if (L == NULL) {
printf("list malloc failed\n");
return L;
}
//initialize
memset(L, 0, sizeof(sqlist));
L->last = -1;
//return
return L;
}
/*
* @ret 0-success -1-failed
**/
int list_clear(sqlink L){
if (L == NULL)
return -1;
memset(L, 0, sizeof(sqlist));
L->last = -1;
return 0;
}
int list_delete(sqlink L){
if (L == NULL)
return -1;
free(L);
L = NULL;
return 0;
}
/*
* list_empty:IS list empty?
*para L:list
*@ret 1--empty 0--not empty
*/
int list_empty(sqlink L){
if (L->last == -1)
return 1;
else
return 0;
}
int list_length(sqlink L){
if (L == NULL)
return -1;
return (L->last+1);
}
/*
* @ret -1--not exist pos
*
*/
int list_locate(sqlink L, data_t value){
int i;
for (i = 0; i <= L->last; i++){
if (L->data[i] == value)
return i;
}
return -1;
}
int list_insert(sqlink L, data_t value, int pos){
int i;
//full?
if (L->last==N-1){
printf("list is full\n");
return -1;
}
//check para 0<=pos<=Last+1 [0, last+1]
if (pos < 0 || pos > L->last+1) {
printf("Pos is invalid\n");
return -1;
}
//move
for (i = L->last; i >= pos; i--){
L->data[i+1] = L->data[i];
}
//update value last
L->data[pos] = value;
L->last++;
return 0;
}
int list_show(sqlink L) {
int i;
if (L == NULL)
return -1;
if (L->last == -1)
printf("list is empty\n");
for (i = 0; i <= L->last; i++){
printf("%d ", L->data[i]);
}
puts("");
return 0;
}
int list_delete_pos(sqlink L, int pos)
{
int i;
if (L->last == -1){
printf("list is empty\n");
return -1;
}
//pos [0, last]
if (pos < 0 || pos > L->last){
printf("delete pos is invalid\n");
return -1;
}
// move [pos+1, last]
for (i = pos+1; i <= L->last; i++){
L->data[i-1] = L->data[i];
}
// update
L->last--;
return 0;
}
int list_merge(sqlink L1, sqlink L2){
int i = 0;
int ret;
while (i <= L2->last){
ret = list_locate(L1, L2->data[i]);
if (ret == -1 ){
if (list_insert(L1, L2->data[i], L1->last+1) == -1)
return -1;
}
i++;
}
return 0;
}
int list_purge(sqlink L){
int i,j;
if (L->last == 0)
return 0;
i = 1;
while (i <= L->last){
j = i-1;
while (j >= 0){
if (L->data[i] == L->data[j]){
list_delete_pos(L, i);
break;
}
else{
j--;
}
}
if (j < 0) {
i++;
}
}
return 0;
}
test.c
主要在主函数内加入代码:test_purge();
记得声明函数和主函数的具体实现:void test_purge();
test.c完整代码如下:
#include <stdio.h>
#include "sqlist.h"
void test_insert();
void test_delete_pos();
void test_merge();
void test_purge();
int main(int argc, const char *argv[])
{
// test_insert();
// test_delete_pos();
// test_merge();
test_purge();
return 0;
}
void test_insert(){
sqlink L;
L = list_create();
if (L == NULL)
return;
list_insert(L, 10, 0);
list_insert(L, 20, 0);
list_insert(L, 30, 0);
list_insert(L, 40, 0);
list_show(L);
//list_insert(L, 70, list_length(L));
list_insert(L, 100, -1000);
list_show(L);
list_delete(L);
}
void test_delete_pos(){
sqlink L;
L = list_create();
if (L == NULL)
return;
list_insert(L, 50, 0);
list_insert(L, 20, 0);
list_insert(L, 50, 0);
list_insert(L, 40, 0);
list_show(L);
list_delete_pos(L, 2);
list_show(L);
}
void test_merge(){
sqlink L1, L2;
L1 = list_create();
if (L1 == NULL)
return;
L2 = list_create();
if (L2 == NULL)
return;
list_insert(L1, 10, 0);
list_insert(L1, 20, 0);
list_insert(L1, 30, 0);
list_insert(L1, 40, 0);
list_insert(L2, 20, 0);
list_insert(L2, 50, 0);
list_insert(L2, 40, 0);
list_insert(L2, 90, 0);
list_show(L1);
list_show(L2);
printf("************\n");
list_merge(L1, L2);
list_show(L1);
}
void test_purge(){
sqlink L;
L = list_create();
if (L == NULL)
return;
list_insert(L, 10, 0);
list_insert(L, 20, 0);
list_insert(L, 30, 0);
list_insert(L, 40, 0);
list_insert(L, 40, 0);
list_insert(L, 40, 0);
list_show(L);
list_purge(L);
list_show(L);
}
编译运行:
gcc *.c
./a.out
运行结果:
由上述结果图可知L线性表中重复的元素被成功删除。

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐
所有评论(0)