Algorithm 2_C language: Knowing that L is a non-decreasing sequence table, please design an algorithm to delete repeated elements in L (that is, make the L table an increasing table after deletion).

#include<stdio.h>
#include<stdlib.h> 
#define list_size 100
#define listincreasement 100
typedef int element;  
typedef struct   
{
    element* elem;
    int length;
    int listsize;
}Sqlist;
bool initializer_list(Sqlist& L);//建表初始化 
bool creatnewlist(Sqlist& L, int n);   // 建立顺序表 
void sort_L(Sqlist& L);   //输出顺序表 
int main()
{
    Sqlist L;
    int n, N;
    printf("请输入要建立的顺序表数字个数:\n");
    scanf_s("%d", &n);
    initializer_list(L);
    creatnewlist(L, n);
    sort_L(L);
    return 0;
}
bool initializer_list(Sqlist& L)  //建表初始化 
{
    L.elem = (int*)malloc(sizeof(int));
    if (!L.elem)
    {
        return 0;
    }
    L.length = 0;
    L.listsize = list_size;
    return 0;
}
bool creatnewlist(Sqlist& L, int n)  // 插入n并输出 
{
    int i;
    L.elem = (int*)malloc(sizeof(int) * list_size);
    if (!L.elem)
    {
        return 0;
    }
    else
    {
        printf("请输入数据:\n");
        for (i = 0; i < n; i++)
        {
            scanf_s("%d", &L.elem[i]);
            L.length++;
        }
    }
    return 0;
}
void sort_L(Sqlist& L)  //输出顺序表 
{
    int i, j,key;
    for (i = 0; i < L.length-1; i++)
    {
        for (j = 0; j < L.length-1-i; j++)
        {
            if (L.elem[j] > L.elem[j+1])
            {
                key= L.elem[j];
                L.elem[j] = L.elem[j + 1];
                L.elem[j + 1] = key;
            }
        }
    }
    printf("%d ", L.elem[0]);
    for (i = 1; i < L.length;i++)
    {
        if(L.elem[i]!=L.elem[i-1])
        printf("%d ", L.elem[i]);
    }
}

Guess you like

Origin blog.csdn.net/qq_51224492/article/details/110374104