6-6 带头结点的链式表操作集 (20 分)

题目地址:https://pintia.cn/problem-sets/15/problems/729

头节点创建但并不储存信息,操作与普通链式表基本相同

List MakeEmpty() {
    List L = (List)malloc(sizeof(struct LNode));
    L ->Next = NULL;
    return L;
}

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

bool Insert(List L, ElementType X, Position P) {
    if(P == L->Next) {
        List p = (List)malloc(sizeof(struct LNode));
        p->Data = X;
        p->Next = L->Next;
        L->Next = p;
        return true;
    }

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

bool Delete(List L, Position P) {
    List q = L;
    while(q) {
        if(q->Next == P) {
            q->Next = P->Next;
            return true;
        }
        q = q->Next;
    }
    printf("Wrong Position for Deletion\n");
    return false;
}
View Code

猜你喜欢

转载自www.cnblogs.com/mile-star/p/11458151.html