Write an algorithm to delete "redundant" data elements in the sorted list, even if all elements in the sorted list after the operation have different values

Write an algorithm to remove "redundant" data elements in the sorted list, even if all elements in the sorted list after the operation have different values.

code show as below:

#include <iostream>
#include <stdlib.h>

using namespace std;
#define LENGHT 999
//顺序表
typedef struct
{
    
    
   int date[LENGHT];
   int len;
}sqlist;

void Create(sqlist *&list, int len);

void Delete(sqlist *&lsit);

void display(sqlist *&list);

int main(int argc, int  *argv[])
{
    
    
    sqlist *list;
    int n;
    cout <<"请输入表长:\n";
    cin >>n;
    cout <<"请输入数据(用空格隔开):\n";
    Create(list,n);
    Delete(list);
    cout <<"结果为:\n";
    display(list);
    cout <<"\n";
    return 0;
}
void Create(sqlist *&list, int len)
{
    
    
    list = (sqlist*)malloc(sizeof(sqlist));
    for (int i = 0; i < len; i++) {
    
    
        cin >>list->date[i];
    }
    list->len = len;
}
void Delete(sqlist *&lsit)
{
    
    
    int j = 0;
    for (int i = 1; i < lsit->len; i++) {
    
    
       if (lsit->date[j]!=lsit->date[i]) {
    
    
           lsit->date[++j] = lsit->date[i];
           lsit->date[j]   = lsit->date[i];
       }
    }
    lsit->len = j+1;
}
void display(sqlist *&list)
{
    
    
    for (int i = 0; i < list->len; i++) {
    
    
        cout <<list->date[i] <<" ";
    }
}

Guess you like

Origin blog.csdn.net/weixin_51732593/article/details/121842528