非递减顺序表删除重复元素(三种方法)

方法一:暴力出奇迹

方法二:批量删除,一次将所有重复元素删除完

方法三:把顺序表当作两个表处理,首先将第一个元素当作一个表1,剩下的为一个表2,然后将表2的依次取出和表1元素对比,相同舍弃,不同直接加入表1

话不多说直接上代码吧(本人较懒没写注释大家自己脑补吧)

#include 
#include  
#include   
#include    

#define TRUE        1
#define FALSE       0
#define OK          1
#define ERROR       0
#define INFEASIBLE -1
#define OVERFLOW   -2

typedef int    Status;
typedef int  ElemType;

#define LIST_INIT_SIZE  100
#define LISTINCREMENT    10

typedef struct {
	ElemType* elem;   
	int length;       
	int listsize;     
}SqList;

SqList La,Lb,Lc;
int num[10];
int sum;

void Randomize(int *array, int n, int min, int max)
{
	int i = 0;
	srand(time(NULL));
	for (i = 0; i < n; ++i) {
		array[i] = rand() % (max - min + 1) + min;  
	}
}

Status InitList_Sq(SqList &L)
{
	L.elem = (ElemType *)malloc(sizeof(ElemType)*LIST_INIT_SIZE);
	if (!L.elem)
		exit(OVERFLOW);
	L.length = 0;
	L.listsize = LIST_INIT_SIZE;
	return OK;
}

void PrintList_Sq(SqList L)
{
	if (L.length == 0) {
		printf("SqList is empty!");
		exit(ERROR);
	}
	printf("此表数据为:\n");
	for (int i = 0; i < L.length; i++)
		printf("%4d", L.elem[i]);
	printf("\n");
}

Status ListInsert_Sq(SqList &L,int i,ElemType e)
{
	if (i<1 || i>L.length + 1) 
		return ERROR;
	if(L.length>=L.listsize)
	{
		ElemType *newbase;
		newbase = (ElemType *)malloc(sizeof(ElemType)*(LIST_INIT_SIZE + LISTINCREMENT));
		if (!newbase)
			exit(OVERFLOW);
		L.elem = newbase;
		L.listsize = LIST_INIT_SIZE + LISTINCREMENT;
	}
	ElemType *p, *q;
	p = &L.elem[i - 1];
	for(ElemType *q=&L.elem[L.length-1];q>=p;q--)
	{
		*(q + 1) = *q;
	}
	*p = e;
	L.length++;
	return OK;
}

Status ListCreate_Sq()
{
	/*int n;
	scanf("%d", &n);
	if (n > L.listsize)
		return (OVERFLOW);
	for(int i=0;iL.length + 1)
		return ERROR;
	ElemType *p,*q;
	p = &L.elem[i - 1];
	e = *p;
	q = L.elem + L.length - 1;
	for(++p;p<=q;p++)
	{
		sum++;
		*(p - 1) = *p;
	}
	L.length--;
}
Status ListRepeatDelete1_Sq(SqList &L) 
{
	sum = 0;
	for (int i = 0; i < L.length; i++)
	{
		for (int j = i + 1; j < L.length;)
		{
			sum++;
			if (L.elem[i] == L.elem[j])
			{
				ListDelete_Sq(L, j + 1, L.elem[j]);
			}
			else
			{
				j++; 
				sum++;
			}
		}
	}
	printf("\n方法一执行了%d次\n", sum);
	return OK;
}

Status ListRepeatDelete2_Sq(SqList &L)
{
	sum = 0;
	int SumRepeat=0;
	for(int i=0;i

附上运行截图:

非递减顺序表删除重复元素(三种方法)_第1张图片

 

你可能感兴趣的:(非递减顺序表删除重复元素(三种方法))