# Linked list operation set (20 points)

This problem requires to realize the operation set of linked list.

### Function interface definition:

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

The List structure is defined as follows:

```typedef struct LNode *PtrToLNode;
struct LNode {
ElementType Data;
PtrToLNode Next;
};
typedef PtrToLNode Position;
typedef PtrToLNode List;
```

Each operation function is defined as:

Position find (List L, ElementType x): returns the first occurrence of X in a linear table. If not found, ERROR will be returned;

List insert (List L, ElementType X, position P): insert X before the node pointed to by position P, and return the header of the linked list. If the parameter p points to an illegal position, print "Wrong Position for Insertion" and return ERROR;

List delete (List L, position P): delete the element of position P and return the header of the linked list. If parameter p points to an illegal location, print "Wrong Position for Deletion" and return ERROR.

### Sample referee test procedure:

```#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 )
}
}
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;
}

/* Your code will be embedded here */
```

```6
12 2 4 87 10 2
4
2 12 87 5
```

### Output example:

```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 ```

```#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 )
}
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 )
}
}
L = Insert(L, X, NULL);
if ( L==ERROR )
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 )
tmp = Delete(L, P);
if ( tmp!=ERROR )
for ( P=L; P; P = P->Next )
printf("%d ", P->Data);
return 0;
}

Position Find( List L, ElementType X )
{
int i=0;
while(L)
{
if(X==L->Data)
return L;
L=L->Next;
}
return ERROR;
}
List Insert( List L, ElementType X, Position P )
{
struct LNode *p,*head = (struct LNode*)malloc(sizeof(struct LNode));

if(L==NULL&&P==NULL)
{
struct LNode *new = (struct LNode*)malloc(sizeof(struct LNode));
new->Data=X;
new->Next=NULL;

return new;
}
else if(P==NULL&&L!=NULL)
{
while(L->Next)
{
L=L->Next;
}
struct LNode *new = (struct LNode*)malloc(sizeof(struct LNode));
new->Data=X;
new->Next=NULL;

L->Next = new;
}

while(p->Next)
{
if(p->Next==P)
{
struct LNode *new = (struct LNode*)malloc(sizeof(struct LNode));
new->Data=X;
new->Next=NULL;

new->Next= P;
p->Next=new;

}
p=p->Next;
}
printf("Wrong Position for Insertion\n");
return ERROR;
}
List Delete( List L, Position P )
{
struct LNode *p,*head = (struct LNode*)malloc(sizeof(struct LNode));

while(p->Next)
{
if(p->Next==P)
{
p->Next=p->Next->Next;