题目链接:https://pintia.cn/problem-sets/15/problems/728
这个题目是比较简单的,主要是考基本操作。我刚刚学做了很久才写出来,而且运行结果是对的,可提交是编译错误,但是我认为有参考价值。
List Insert( List L, ElementType X, Position P )
{
Position tmp,t;
tmp=L;
t=NULL;
if(P==L)
{
t=(List)malloc(sizeof(struct LNode));
t->Data=X;
t->Next=P;
tmp=t;
return tmp;
}
if(L==NULL)
{
printf("Wrong Position for Insertion\n");
return ERROR;
}
while(L->Next!=P&&L->Next!=NULL)
{
L=L->Next;
}
if(L->Next==NULL&&P!=NULL)
{
printf("Wrong Position for Insertion\n");
return ERROR;
}
else
{
t=(List)malloc(sizeof(LNode));
t->Data=X;
L->Next=t;
t->Next=P;
return tmp;
}
}
Position Find( List L, ElementType X )
{
while(L!=NULL&&L->Data!=X)
{
L=L->Next;
}
if(L==NULL)
{
return ERROR;
}
else
{
return L;
}
}
List Delete( List L, Position P )
{
Position tmp,t;
tmp=L;
t=NULL;
if(L==P)
{
if(P==NULL)
{
return ERROR;
}
else
{
tmp=L->Next;
free(L);
return tmp;
}
}
if(L==NULL)
{
printf("Wrong Position for Deletion\n");
return ERROR;
}
while(L->Next!=P&&L->Next!=NULL)
{
L=L->Next;
}
if(L->Next==NULL)
{
printf("Wrong Position for Deletion\n");
return ERROR;
}
else
{
L->Next=P->Next;
free(P);
return tmp;
}
}
就是这样子了,水平不够,请多多包涵。