6-2-顺序表操作集-函数题

6-2-顺序表操作集-函数题

解题代码

List MakeEmpty() {
	List new = (List)malloc(sizeof(struct LNode));
	new->Last = -1;
	return new;
}
Position Find(List L, ElementType X) {
	int ret=ERROR;
	int i;
	for (i = 0; i <= L->Last; i++) {
		if (X == L->Data[i]) {
			ret = i;
			break;
		}
	}
	return ret;
}
bool Insert(List L, ElementType X, Position P) {
	int i;
	bool ret = false;
	if (L->Last == MAXSIZE - 1) {
		printf("FULL");
	}
	else {
		if (P < 0 || P > L->Last+1) {
			printf("ILLEGAL POSITION");
		}
		else {
			for (i = L->Last; i >= P; i--) {
				L->Data[i + 1] = L->Data[i];
			}
			L->Data[P] = X;
			L->Last++;
			ret = true;
		}
	}
	return ret;
}
bool Delete(List L, Position P) {
	bool ret = false;
	int i;
	if (P < 0 || P > L->Last) {
		printf("POSITION %d EMPTY", P);
	}
	else {
		for (i = P; i <= L->Last - 1; i++) {
			L->Data[i] = L->Data[i + 1];
		}
		ret = true;
		L->Last--;
	}
	return ret;
}

测试结果

在这里插入图片描述

问题整理

1.除了L->Last会和MAXSIZE比较以及判断是否满的情况,一般变量是不会和MAXSIZE比较的。
2.插入时判断legal的条件的一端注意是P>L->Last+1。
发布了47 篇原创文章 · 获赞 2 · 访问量 1339

猜你喜欢

转载自blog.csdn.net/Aruasg/article/details/105048398
今日推荐