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

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#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;
}

你可能感兴趣的:(产生N个随机数字,删除其中重复的数据(C语言实现))