pta数据结构与算法题目集(中文)错题分析与记录

文章目录

    • 6-5 链式表操作集

6-5 链式表操作集

题目链接:https://pintia.cn/problem-sets/15/exam/problems/728
本题要求实现链式表的操作集。

函数接口定义:
Position Find( List L, ElementType X );
List Insert( List L, ElementType X, Position P );
List Delete( List L, Position P );
其中List结构定义如下:

typedef struct LNode *PtrToLNode;
struct LNode {
ElementType Data;
PtrToLNode Next;
};
typedef PtrToLNode Position;
typedef PtrToLNode List;
各个操作函数的定义为:

Position Find( List L, ElementType X ):返回线性表中首次出现X的位置。若找不到则返回ERROR;

List Insert( List L, ElementType X, Position P ):将X插入在位置P指向的结点之前,返回链表的表头。如果参数P指向非法位置,则打印“Wrong Position for Insertion”,返回ERROR;

List Delete( List L, Position P ):将位置P的元素删除并返回链表的表头。若参数P指向非法位置,则打印“Wrong Position for Deletion”并返回ERROR。

裁判测试程序样例:
#include
#include

#define ERROR NULL
typedef int ElementType;
typedef struct LNode *PtrToLNode;
struct LNode {
ElementType Data;
PtrToLNode Next;
};
typedef PtrToLNode Position;
typedef PtrToLNode List;

Position Find( List L, ElementType X );
List Insert( List L, ElementType X, Position P );
List Delete( List L, Position P );

int main()
{
List L;
ElementType X;
Position P, tmp;
int N;

L = NULL;
scanf("%d", &N);
while ( N-- ) {
    scanf("%d", &X);
    L = Insert(L, X, L);
    if ( L==ERROR ) printf("Wrong Answer\n");
}
scanf("%d", &N);
while ( N-- ) {
    scanf("%d", &X);
    P = Find(L, X);
    if ( P == ERROR )
        printf("Finding Error: %d is not in.\n", X);
    else {
        L = Delete(L, P);
        printf("%d is found and deleted.\n", X);
        if ( L==ERROR )
            printf("Wrong Answer or Empty List.\n");
    }
}
L = Insert(L, X, NULL);
if ( L==ERROR ) printf("Wrong Answer\n");
else
    printf("%d is inserted as the last element.\n", X);
P = (Position)malloc(sizeof(struct LNode));
tmp = Insert(L, X, P);
if ( tmp!=ERROR ) printf("Wrong Answer\n");
tmp = Delete(L, P);
if ( tmp!=ERROR ) printf("Wrong Answer\n");
for ( P=L; P; P = P->Next ) printf("%d ", P->Data);
return 0;

}

/* 你的代码将被嵌在这里 */
输入样例:
6
12 2 4 87 10 2
4
2 12 87 5
输出样例:
2 is found and deleted.
12 is found and deleted.
87 is found and deleted.
Finding Error: 5 is not in.
5 is inserted as the last element.
Wrong Position for Insertion
Wrong Position for Deletion
10 4 2 5
思考:本题难点在于要考虑多种情况,如输入是否合法,合法后又要根据表的长度来考虑不同的可能性,此外我错的一个点在于函数内容变量的使用,在使用一个变量时先看看这个变量是否之前使用过,若用过则它的值改变没,是否现在的值是你想用的值,(使用insert函数的L 时候忘记了前面遍历过,后面再用L的时候就是直接从NULL开始,导致运行出错,找了好久的bug)

Position Find(List L, ElementType X) {
	struct LNode* add;//元素x在的位置
	add = NULL;
	if (L == NULL) return  ERROR;
	else {
		while (L != NULL) {
			if (L->Data == X) {
				add = L;
				break;
			}
			else {
				L = L->Next;
			}
		}
		if (add == NULL)return ERROR;
		else return add;
	}

}
List Insert(List L, ElementType X, Position P) {
	int hefa = 0;//0表示不合法 
	//插入之前先检查p是否是合法位置
	struct LNode* cL;
	cL = L;
	if (cL == NULL && P != NULL) {
		hefa = 0;

	}
	else if (cL == NULL && P == NULL) {
		hefa = 1;
	}
	else if (cL != NULL && P != NULL) {
		while (cL != NULL) {
			if (cL != P) {
				cL = cL->Next;
			}
			else {
				//合法 
				hefa = 1;
				break;
			}
		}
	}
	else if (cL != NULL && P == NULL) {
		hefa = 1;
	}
	//合法判断完成
	if (hefa == 0) {
		printf("Wrong Position for Insertion\n");
		return ERROR;
	}
	else {
		struct LNode* node = (struct LNode*)malloc(sizeof(struct LNode));
		node->Data = X;
		node->Next = NULL;
		//准备插入
		//建立一个头结点便于插入,头结点不存储值
		struct LNode* head = (struct LNode*)malloc(sizeof(struct LNode));
		head->Next = NULL;
		if (L == NULL && P == NULL) {
			node->Next = P;
			head->Next = node;
			return head->Next;
		}
		else if (L != NULL && P == NULL) {
			head->Next = L;
			struct LNode* p = (struct LNode*)malloc(sizeof(struct LNode));//指向前一个节点 
			p = head;
			while (L != NULL) {
				p = L;
				L = L->Next;
			}
			p->Next = node;
			node->Next = NULL;
			return head->Next;

		}
		else if (L != NULL && P != NULL) {
			head->Next = L;
			struct LNode* p1 = (struct LNode*)malloc(sizeof(struct LNode));//指向前一个节点 
			p1 = head;
			while (L != NULL) {
				if (L == P) {
					node->Next = P;
					p1->Next = node;
					return head->Next;
				}
				else {
					p1 = L;
					L = L->Next;
				}
			}
			//head->Next;

		}
	}




}
List Delete(List L, Position P) {
	int hefa = 0;//0表示不合法,1表示合法
	struct LNode* cL = L;
	if (cL == NULL && P == NULL) hefa = 0;
	else if (cL == NULL && P != NULL) hefa = 0;
	else if (cL != NULL && P == NULL) hefa = 0;
	else if (cL != NULL && P != NULL) {
		while (cL != NULL) {
			if (cL != P) {
				cL = cL->Next;
			}
			else {
				//合法 
				hefa = 1;
				break;
			}
		}
	}
	//合法判定完成
	//准备删除
	if (hefa == 0) {
		printf("Wrong Position for Deletion\n");
		return ERROR;
	}
	else {
		//建立一个头结点便于插入,头结点不存储值
		struct LNode* head1 = (struct LNode*)malloc(sizeof(struct LNode));
		head1->Next = L;
		if (L == P) {//第一个节点就是要删除的节点 
			head1->Next = L->Next;
			return head1->Next;
		}
		else {
			struct LNode* pre;//一个指向当前节点的前一个节点的指针
			pre = head1;
			while (L != NULL) {
				if (L != P) {
					pre = L;
					L = L->Next;
				}
				else {
					pre->Next = L->Next;
					free(L);
					break;
				}
			}
			return head1->Next;
		}
	}
}

本题完整代码:

#include 
#include 

#define ERROR NULL
typedef int ElementType;
typedef struct LNode* PtrToLNode;
struct LNode {
	ElementType Data;
	PtrToLNode Next;
};
typedef PtrToLNode Position;
typedef PtrToLNode List;

Position Find(List L, ElementType X) {
	struct LNode* add;//元素x在的位置
	add = NULL;
	if (L == NULL) return  ERROR;
	else {
		while (L != NULL) {
			if (L->Data == X) {
				add = L;
				break;
			}
			else {
				L = L->Next;
			}
		}
		if (add == NULL)return ERROR;
		else return add;
	}

}
List Insert(List L, ElementType X, Position P) {
	int hefa = 0;//0表示不合法 
	//插入之前先检查p是否是合法位置
	struct LNode* cL;
	cL = L;
	if (cL == NULL && P != NULL) {
		hefa = 0;

	}
	else if (cL == NULL && P == NULL) {
		hefa = 1;
	}
	else if (cL != NULL && P != NULL) {
		while (cL != NULL) {
			if (cL != P) {
				cL = cL->Next;
			}
			else {
				//合法 
				hefa = 1;
				break;
			}
		}
	}
	else if (cL != NULL && P == NULL) {
		hefa = 1;
	}
	//合法判断完成
	if (hefa == 0) {
		printf("Wrong Position for Insertion\n");
		return ERROR;
	}
	else {
		struct LNode* node = (struct LNode*)malloc(sizeof(struct LNode));
		node->Data = X;
		node->Next = NULL;
		//准备插入
		//建立一个头结点便于插入,头结点不存储值
		struct LNode* head = (struct LNode*)malloc(sizeof(struct LNode));
		head->Next = NULL;
		if (L == NULL && P == NULL) {
			node->Next = P;
			head->Next = node;
			return head->Next;
		}
		else if (L != NULL && P == NULL) {
			head->Next = L;
			struct LNode* p = (struct LNode*)malloc(sizeof(struct LNode));//指向前一个节点 
			p = head;
			while (L != NULL) {
				p = L;
				L = L->Next;
			}
			p->Next = node;
			node->Next = NULL;
			return head->Next;

		}
		else if (L != NULL && P != NULL) {
			head->Next = L;
			struct LNode* p1 = (struct LNode*)malloc(sizeof(struct LNode));//指向前一个节点 
			p1 = head;
			while (L != NULL) {
				if (L == P) {
					node->Next = P;
					p1->Next = node;
					return head->Next;
				}
				else {
					p1 = L;
					L = L->Next;
				}
			}
			//head->Next;

		}
	}




}
List Delete(List L, Position P) {
	int hefa = 0;//0表示不合法,1表示合法
	struct LNode* cL = L;
	if (cL == NULL && P == NULL) hefa = 0;
	else if (cL == NULL && P != NULL) hefa = 0;
	else if (cL != NULL && P == NULL) hefa = 0;
	else if (cL != NULL && P != NULL) {
		while (cL != NULL) {
			if (cL != P) {
				cL = cL->Next;
			}
			else {
				//合法 
				hefa = 1;
				break;
			}
		}
	}
	//合法判定完成
	//准备删除
	if (hefa == 0) {
		printf("Wrong Position for Deletion\n");
		return ERROR;
	}
	else {
		//建立一个头结点便于插入,头结点不存储值
		struct LNode* head1 = (struct LNode*)malloc(sizeof(struct LNode));
		head1->Next = L;
		if (L == P) {//第一个节点就是要删除的节点 
			head1->Next = L->Next;
			return head1->Next;
		}
		else {
			struct LNode* pre;//一个指向当前节点的前一个节点的指针
			pre = head1;
			while (L != NULL) {
				if (L != P) {
					pre = L;
					L = L->Next;
				}
				else {
					pre->Next = L->Next;
					free(L);
					break;
				}
			}
			return head1->Next;
		}
	}
}

int main()
{
	List L;
	ElementType X;
	Position P, tmp;
	int N;

	L = NULL;
	scanf("%d", &N);
	while (N--) {
		scanf("%d", &X);
		L = Insert(L, X, L);
		if (L == ERROR) printf("Wrong Answer\n");
	}
	printf("验证输入\n");//2 10 87 4 2 12
	struct LNode* cl;
	cl = L;
	while (cl != NULL) {
		printf("%d ", cl->Data);
		cl = cl->Next;
	}
	scanf("%d", &N);
	while (N--) {
		scanf("%d", &X);
		P = Find(L, X);
		if (P == ERROR)//删除2 12 87 5 
			printf("Finding Error: %d is not in.\n", X);
		else {
			L = Delete(L, P);
			printf("%d is found and deleted.\n", X);
			printf("删后的链表\n");
			cl = L;
			while (cl != NULL) {
				printf("%d ", cl->Data);
				cl = cl->Next;
			}
			printf("\n");
			if (L == ERROR)
				printf("Wrong Answer or Empty List.\n");
		}
	}
	L = Insert(L, X, NULL);
	if (L == ERROR) printf("Wrong Answer\n");
	else {
		printf("%d is inserted as the last element.\n", X);
		printf("插入后的链表\n");
		cl = L;
		while (cl != NULL) {
			printf("%d ", cl->Data);
			cl = cl->Next;
		}
	}
	P = (Position)malloc(sizeof(struct LNode));
	tmp = Insert(L, X, P);
	if (tmp != ERROR) printf("Wrong Answer\n");
	tmp = Delete(L, P);
	if (tmp != ERROR) printf("Wrong Answer\n");
	for (P = L; P; P = P->Next) printf("%d ", P->Data);
	return 0;
}

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

你可能感兴趣的:(数据结构,算法,c语言)