Table 6-9 chain operation set (20 points) of PTA

This set of operations required to achieve the title chain table.

Function interface definition:

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

Wherein List structure is defined as:

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

It defines the operation of each function is:

Position Find (List L, ElementType X): Returns the linear position of the first occurrence of X in the table. If no return ERROR;

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

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

Referee test program Example:

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

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

Here Insert Picture Description
AC Code:
must pay attention to insert and delete illegal judgments P position! And code position "display"

Position Find( List L, ElementType X )
{
    List p=L;
    while(p!=NULL&&p->Data!=X)
    {
        p=p->Next;
    }
    if(p==NULL)
        return ERROR;
    return p;
}
List Insert( List L, ElementType X, Position P )
{
    List tmp=(List)malloc(sizeof(struct LNode));
    tmp->Data=X;
    if(L==P)
    {
        tmp->Next=L;
        L=tmp;
        return L;
    }
    List a=L;
    while(a!=NULL&&a->Next!=P)
        a=a->Next;
    if(a==NULL)
    {
        printf("Wrong Position for Insertion\n");
        return ERROR;
    }
    else
    {
        tmp->Next=P;
        a->Next=tmp;
    }
    return L;
}
List Delete( List L, Position P )
{
    if(L==P)
    {
        L=L->Next;
        return L;
    }
    List a=L;
    while(a!=NULL&&a->Next!=P)
        a=a->Next;
    if(a==NULL)
    {
        printf("Wrong Position for Deletion\n");
        return ERROR;
    }
    else
    {
        a->Next=a->Next->Next;
        return L;
    }
}
Published 32 original articles · won praise 12 · views 1375

Guess you like

Origin blog.csdn.net/qq_18873031/article/details/103074893