Table 6-5 chain operation set (20 points)

Topic Address: https://pintia.cn/problem-sets/15/problems/728

According to the present title subject to the requirements carefully simulation, need to pay attention is to be inserted before the head node and the head node to delete special treatment sentence

List Insert(List L, ElementType X, Position P) {
    if(P == L) {
        List q = (List)malloc(sizeof(List));
        q->Data = X;
        q->Next = L;
        return q;
    }

    List q = L;
    while(q) {
        if(q->Next == P) {
            List p = (List)malloc(sizeof(List));
            p->Next = q->Next;
            p->Data = X;
            q->Next = p;
            return L;
        }
        q = q->Next;
    }
    printf("Wrong Position for Insertion\n");
    return ERROR;
}

List Delete(List L, Position P) {
    if(L == P) {
        return L->Next;
    }

    List q = L;
    while(q) {
        if(q->Next == P) {
            q->Next = q->Next->Next;
            return L;
        }
        q = q->Next;
    }
    printf("Wrong Position for Deletion\n");
    return ERROR;
}

Position Find(List L, ElementType X) {
    List q;
    q = L;
    while(q != NULL) {
        if(q->Data == X) {
            return q;
        }
        q = q->Next;
    }
    return ERROR;
}
View Code

 

Guess you like

Origin www.cnblogs.com/mile-star/p/11454857.html