List MakeEmpty(); Position Find( List L, ElementType X ); boolInsert( List L, ElementType X, Position P ); boolDelete( List L, Position P );
intmain() { List L; ElementType X; Position P; int N; bool flag;
L = MakeEmpty(); scanf("%d", &N); while ( N-- ) { scanf("%d", &X); flag = Insert(L, X, L->Next); if ( flag==false ) 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 { flag = Delete(L, P); printf("%d is found and deleted.\n", X); if ( flag==false ) printf("Wrong Answer.\n"); } } flag = Insert(L, X, NULL); if ( flag==false ) printf("Wrong Answer\n"); else printf("%d is inserted as the last element.\n", X); P = (Position)malloc(sizeof(struct LNode)); flag = Insert(L, X, P); if ( flag==true ) printf("Wrong Answer\n"); flag = Delete(L, P); if ( flag==true ) printf("Wrong Answer\n"); for ( P=L->Next; P; P = P->Next ) printf("%d ", P->Data); return0; } /* 你的代码将被嵌在这里 */
输入样例:
1 2 3 4
6 12 2 4 87 10 2 4 2 12 87 5
输出样例:
1 2 3 4 5 6 7 8
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
List MakeEmpty() { List list = (List)malloc(sizeof(struct LNode)); list->Data = 0; list->Next = NULL;
returnlist; } Position Find(List L, ElementType X) { List list = L->Next; int flag = 0; for (int i = 0; i < L->Data; i++){ if (list->Data == X) { flag = 1; break; } elselist = list->Next; } return flag == 1 ? list : ERROR;
} boolInsert(List L, ElementType X, Position P) { List list = L; int flag = 0; List temp = (List)malloc(sizeof(struct LNode)); temp->Data = X; temp->Next = NULL;
for (int i = 0; i <= L->Data; i++, list = list->Next) if (list->Next == P) { flag = 1; break; }
if (flag){ temp->Next = list->Next; list->Next = temp; L->Data++; returntrue; } printf("Wrong Position for Insertion\n"); returnfalse;
} boolDelete(List L, Position P) { List list = L; int flag = 0; for (int i = 0; i < L->Data; i++, list = list->Next) if (list->Next == P) { flag = 1; break; }
if (flag){ list->Next = list->Next->Next; L->Data--; returntrue; } printf("Wrong Position for Deletion\n"); returnfalse; }