|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
#include <stdlib.h>
#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;
}
/* 你的代码将被嵌在这里 */
Position Find( List L, ElementType X )
{
while(L!=NULL) //循环遍历链表查找目标值
{
if(L->Data==X)
{
return L;
}
L=L->Next; //链表向后查找
}
return ERROR;
}
List Insert( List L, ElementType X, Position P )
{
List head=L; //保存表头
List temp = (List)malloc(sizeof(struct LNode)); //创建新的节点
temp->Data=X; //将值插入该节点
temp->Next =NULL;
if(L==P) //特殊情况,若P为表头时
{
temp->Next = L; //将新节点直接插入表头前
return temp;
}
while(L!=NULL)
{
if(L->Next==P) //一般情况,新节点要插在P和P前一个节点之间,所以判断条件要找到L和L->Next
{
temp->Next = L->Next; //将P位置的节点变为新节点的后继节点
L->Next = temp; //将新节点变为原P位置前一个节点的后继节点,也就是把新节点插入在原P前一个节点和P节点之间
return head;
}
L=L->Next; //若不满足条件,继续循环遍历
}
printf("Wrong Position for Insertion\n"); //若上面条件没有找到正确位置则表示指向了非法位置
return ERROR;
}
List Delete( List L, Position P )
{
List head=L,temp;
if(L==P) //特殊情况,删除位置为表头时
{
L = L->Next;
return L;
}
while(L->Next!=NULL)
{
if(L->Next==P)
{
temp = L->Next;
L->Next = temp->Next; //将temp后继节点变为L的后继节点,表示将temp删除
return head;
}
L=L->Next;
}
printf("Wrong Position for Deletion\n");
return ERROR;
}
救命,这个main函数该怎么写啊。。。
下面是一个可以运行上面提供的代码的 `main` 函数:
int main()
{
List L = NULL;
ElementType X;
Position P, tmp;
int N;
// 插入元素并检查是否出错
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%d", &X);
L = Insert(L, X, L);
if (L == ERROR) {
printf("Wrong Answer\n");
return 0;
}
}
// 删除指定元素并打印结果
scanf("%d", &N);
for (int i = 0; i < N; i++) {
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");
return 0;
}
}
}
// 在表尾插入元素并检查是否出错
scanf("%d", &X);
L = Insert(L, X, NULL);
if (L == ERROR) {
printf("Wrong Answer\n");
return 0;
} 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");
return 0;
}
tmp = Delete(L, P);
if (tmp != ERROR) {
printf("Wrong Answer\n");
return 0;
}
// 打印最终结果
for (P = L; P != NULL; P = P->Next) {
printf("%d ", P->Data);
}
printf("\n");
return 0;
}
|
|