将重复的数删除C语言,产生N个随机数字,删除其中重复的数据(C语言实现)

#include

#include

#include

#define MAXSIZE 100

#define DATA_TYPE int

typedef struct

{

DATA_TYPE data[MAXSIZE];

int length;

}SeqList;

SeqList initData();  //初始化数据

void output(SeqList list);  //输出数据

int SeqList_Locate_e(SeqList *list, DATA_TYPE data, int pos);  //定位重复的数据

int SeqList_Delete(SeqList *list, int pos); //删除数据

int main(void)

{

int i = 0, pos = 0;

SeqList list = initData();

printf("\noriginal data\n");

output(list);

for (i = 0; i < list.length; i++)

{

do

{

pos = SeqList_Locate_e(&list, list.data[i], i);

SeqList_Delete(&list, pos);

} while(pos < list.length && pos != -1);

}

printf("\nresult data:\n");

output(list);

printf("\n");

return 0;

}

SeqList initData()

{

SeqList list;

int i = 0;

list.length = 0;

srand((unsigned) time(NULL));

for (i = 0; i< 10; i++)

{

list.length++;

list.data[i] = rand() % 20 + 10;

}

return list;

}

void output(SeqList list)

{

int i = 0;

for (i = 0; i < list.length; i++)

{

if ((i+1) % 5 == 0)

{

printf("%d\n", list.data[i]);

}

else

{

printf("%d\t", list.data[i]);

}

}

}

int SeqList_Locate_e(SeqList *list, DATA_TYPE data, int pos)

{

int i = pos + 1;

while (i < list->length && (*list).data[i] != data)

{

i++;

}

if (i == list->length)

{

return -1;

}

else

{

return i;

}

}

int SeqList_Delete(SeqList *list, int pos)

{

int i = pos;

if (pos < 0 || pos > list->length)

{

return -1;

}

for (i = pos; i < list->length; i++)

{

list->data[i]= list->data[i+1];

}

list->length --;

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值