线性表元素的区间删除

image.png

函数代码:


List Delete(List L, ElementType minD, ElementType maxD) {
	List NewL = (List)malloc(sizeof(struct LNode));
	int j = 0;
	for (int i = 0; i <= L->Last; i++) {
		if (L->Data[i] > minD && L->Data[i] < maxD) {
			continue;
		}
		NewL->Data[j++] = L->Data[i];
	}
	NewL->Last = j - 1;
	return NewL;
}

全部代码:

#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 20
typedef int ElementType;

typedef int Position;
typedef struct LNode *List;
struct LNode {
	ElementType Data[MAXSIZE];
	Position Last; /* 保存线性表中最后一个元素的位置 */
};

List ReadInput(); /* 裁判实现,细节不表。元素从下标0开始存储 */
void PrintList(List L); /* 裁判实现,细节不表 */
List Delete(List L, ElementType minD, ElementType maxD);

int main()
{
	List L;
	ElementType minD, maxD;
	int i;

	L = ReadInput();
	scanf("%d %d", &minD, &maxD);
	L = Delete(L, minD, maxD);
	PrintList(L);

	return 0;
}

/* 你的代码将被嵌在这里 */

List ReadInput() {
	List L = (List)malloc(sizeof(struct LNode));  // 分配内存空间
	int n;  // 元素个数
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &(L->Data[i]));
	}
	L->Last = n - 1;
	return L;
}

void PrintList(List L) {
	for (int i = 0; i <= L->Last; i++) {
		printf("%d ", L->Data[i]);
	}
	printf("\n");
}

List Delete(List L, ElementType minD, ElementType maxD) {
	List NewL = (List)malloc(sizeof(struct LNode));
	int j = 0;
	for (int i = 0; i <= L->Last; i++) {
		if (L->Data[i] > minD && L->Data[i] < maxD) {
			continue;
		}
		NewL->Data[j++] = L->Data[i];
	}
	NewL->Last = j - 1;
	return NewL;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值