Bootstrap

PTA-数据结构与算法题目集(中文)— 函数题

目录

6-1 ~ 6-5

6-1 单链表逆转

6-2 顺序表操作集

6-3 求链式表的表长

6-4 链式表的按序号查找

6-5 链式表操作集

6-6 ~ 6-10

6-6 带头结点的链式表操作集

6-7 在一个数组中实现两个堆栈

6-8 求二叉树高度

6-9 二叉树的遍历

6-10 二分查找

6-11 ~ 6-12

6-11 先序输出叶结点

6-12 二叉搜索树的操作集


即使你没有在PTA上做过题,也强烈建议你收藏下来,因为些代码都非常适合作为你学习数据结构的模板

题集链接:PTA | 程序设计类实验辅助教学平台 (pintia.cn)

6-1 ~ 6-5

6-1 单链表逆转

List Reverse( List L ) {
    if (!L) return NULL;
    PtrToNode l = NULL, mid = L, r = L->Next;
    while (r) {
        mid->Next = l;
        l = mid;
        mid = r;
        r = r->Next;
    }
    mid->Next = l;
    return mid;
}

6-2 顺序表操作集

List MakeEmpty() {
    List list = (List)malloc(sizeof(struct LNode));
    list->Last = 0; // 列表的末尾
    return list;
}

Position Find( List L, ElementType X ) {
    for (int i = 0; i < L->Last; i++) {
        if (L->Data[i] == X) return i;
    }
    return ERROR;
}

bool Insert( List L, ElementType X, Position P ) {
    if (L->Last == MAXSIZE) {
        printf("FULL");
        return false;
    }
    if (P < 0 || P > L->Last) {
        printf("ILLEGAL POSITION");
        return false;
    }
    for (int i = L->Last++; i > P; i--)
        L->Data[i] = L->Data[i - 1];
    L->Data[P] = X;
    return true;
}

bool Delete( List L, Position P ) {
    if (P < 0 || P >= L->Last) {
        printf("POSITION %d EMPTY", P);
        return false;
    }
    for (int i = P; i < L->Last - 1; i++)
        L->Data[i] = L->Data[i + 1];
    L->Last--;
    return true;
}

6-3 求链式表的表长

 int Length( List L ) {
    int len = 0;
    PtrToLNode p = L;
    while (p)
        len++, p = p->Next;
    return len;
}

6-4 链式表的按序号查找

ElementType FindKth( List L, int K ) {
    if (!L || K <= 0) return ERROR;
    PtrToLNode p = L;
    while (p && --K) p = p->Next;
    if (K) return ERROR;
    return p->Data;
}

6-5 链式表操作集

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

List Insert( List L, ElementType X, Position P ) {
    PtrToLNode ptrToNewNode = (PtrToLNode)malloc(sizeof(struct LNode));
    ptrToNewNode->Data = X;
    if (P == L) {
        ptrToNewNode->Next = L;
        return ptrToNewNode;
    }
    PtrToLNode p = L, pre = NULL;
    while (p && p != P) pre = p, p = p->Next;
    if (p != P) {
        printf("Wrong Position for Insertion\n");
        return ERROR;
    }
    pre->Next = ptrToNewNode;
    ptrToNewNode->Next = p;
    return L;
}

List Delete( List L, Position P ) {
    if (P == L) {
        PtrToLNode res = L->Next;
        free(L);
        return res;
    }
    PtrToLNode p = L, pre = NULL;
    while (p && p != P) pre = p, p = p->Next;
    if (p != P) {
        printf("Wrong Position for Deletion\n");
        return ERROR;
    }
    pre->Next = p->Next;
    free(p);
    return L;
}

6-6 ~ 6-10

6-6 带头结点的链式表操作集

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

Position Find( List L, ElementType X ) {
    PtrToLNode p = L->Next;
    while (p && p->Data != X) p = p->Next;
    return p ? p : ERROR;
}

bool Insert( List L, ElementType X, Position P ) {
    PtrToLNode p = L->Next, pre = L;
    while (p && p != P) pre = p, p = p->Next;
    if (p != P) {
        printf("Wrong Position for Insertion\n");
        return false;
    }
    PtrToLNode ptrToNewNode = (PtrToLNode)malloc(sizeof(struct LNode));
    ptrToNewNode->Data = X;
    ptrToNewNode->Next = P;
    pre->Next = ptrToNewNode;
    return true;
}

bool Delete( List L, Position P ) {
    PtrToLNode p = L->Next, pre = L;
    while (p && p != P) pre = p, p = p->Next;
    if (p != P) {
        printf("Wrong Position for Deletion\n");
        return false;
    }
    pre->Next = P->Next;
    return true;
}

6-7 在一个数组中实现两个堆栈

Stack CreateStack( int MaxSize ) {
    Stack stack = (Stack)malloc(sizeof(struct SNode));
    stack->Data = (ElementType*)calloc(MaxSize, sizeof(ElementType));
    stack->Top1 = -1, stack->Top2 = MaxSize;
    stack->MaxSize = MaxSize;
    return stack;
}

bool Push( Stack S, ElementType X, int Tag ) {
    if (S->Top1 + 1 == S->Top2) {
        printf("Stack Full\n");
        return false;
    }
    if (Tag == 1)
        S->Data[++S->Top1] = X;
    else
        S->Data[--S->Top2] = X;
    return true;
}

ElementType Pop( Stack S, int Tag ) {
    if (Tag == 1) {
        if (S->Top1 != -1) return S->Data[S->Top1--];
        else {
            printf("Stack %d Empty\n", Tag);
            return ERROR;
        }
    } else {
        if (S->Top2 != S->MaxSize) return S->Data[S->Top2++];
        else {
            printf("Stack %d Empty\n", Tag);
            return ERROR;
        }
    }
    return ERROR;
}

6-8 求二叉树高度

int GetHeight( BinTree BT ) {
    if (!BT) return 0;
    int h = 0, t = 0, height = 0;
    Position q[105];
    q[t++] = BT;
    while (h < t) {
        int n = t - h;
        while (n--) {
            if (q[h]->Left) q[t++] = q[h]->Left;
            if (q[h]->Right) q[t++] = q[h]->Right;
            h++;
        }
        height++;
    }
    return height;
}

6-9 二叉树的遍历

void InorderTraversal( BinTree BT ) {
    if (!BT) return;
    if (BT->Left) InorderTraversal(BT->Left);
    printf(" %c", BT->Data);
    if (BT->Right) InorderTraversal(BT->Right);
}

void PreorderTraversal( BinTree BT ) {
    if (!BT) return;
    printf(" %c", BT->Data);
    if (BT->Left) PreorderTraversal(BT->Left);
    if (BT->Right) PreorderTraversal(BT->Right);
}

void PostorderTraversal( BinTree BT ) {
    if (!BT) return;
    if (BT->Left) PostorderTraversal(BT->Left);
    if (BT->Right) PostorderTraversal(BT->Right);
    printf(" %c", BT->Data);
}

void LevelorderTraversal( BinTree BT ) {
    if (!BT) return;
    int h = 0, t = 0;
    Position q[105];
    q[t++] = BT;
    while (h < t) {
        printf(" %c", q[h]->Data);
        if (q[h]->Left) q[t++] = q[h]->Left;
        if (q[h]->Right) q[t++] = q[h]->Right;
        h++;
    }
}

6-10 二分查找

Position BinarySearch( List L, ElementType X ) {
    Position l = 1, r = L->Last;
    while (l <= r) {
        Position mid = l + r >> 1;
        if (L->Data[mid] < X) l = mid + 1;
        else if (L->Data[mid] > X) r = mid - 1;
        else return mid;
    }
    return NotFound;
}

6-11 ~ 6-12

6-11 先序输出叶结点

void PreorderPrintLeaves( BinTree BT ) {
    if (!BT) return;
    if (!BT->Left && !BT->Right)
        printf(" %c", BT->Data);
    if (BT->Left) PreorderPrintLeaves(BT->Left);
    if (BT->Right) PreorderPrintLeaves(BT->Right);
}

6-12 二叉搜索树的操作集

BinTree Insert( BinTree BST, ElementType X ) {
    if (!BST) {
        BinTree t = (BinTree)malloc(sizeof(struct TNode));
        t->Data = X;
        t->Left = t->Right = NULL;
        return t;
    }
    Position p = BST;
    while (1) {
        if (p->Data > X) {
            if (p->Left) {
                p = p->Left;
                continue;
            }
            p->Left = (Position)malloc(sizeof(struct TNode));
            p->Left->Data = X;
            p->Left->Left = p->Left->Right = NULL;
        } else if (p->Data < X) {
            if (p->Right) {
                p = p->Right;
                continue;
            }
            p->Right = (Position)malloc(sizeof(struct TNode));
            p->Right->Data = X;
            p->Right->Left = p->Right->Right = NULL;
        }
        break;
    }
    return BST;
}

BinTree Delete( BinTree BST, ElementType X ) {
    if (!BST) {
        printf("Not Found\n");
        return BST;
    }
    Position p;
    if (X < BST->Data) { // 从左子树递归删除
        BST->Left = Delete(BST->Left, X);
        return BST;
    }
    if (X > BST->Data) { // 从右子树递归删除
        BST->Right = Delete(BST->Right, X);
        return BST;
    }
    // 找到要删除的元素
    if (BST->Left && BST->Right) { // 被删除节点有左右两个子节点
        p = FindMin(BST->Right); // 在右子树中找最小的元素填充删除节点
        BST->Data = p->Data;
        BST->Right = Delete(BST->Right, BST->Data); // 在删除节点的右子树中删除最小元素
    } else { // 被删除节点有一个或无子节点
        p = BST;
        if (BST->Left) // 有左孩子
            BST = BST->Left; 
        else // 有右孩子或无子节点
            BST = BST->Right;
        free(p);
    }
    return BST;
}

Position Find( BinTree BST, ElementType X ) {
    if (!BST) return NULL;
    Position p = BST;
    while (1) {
        if (p->Data > X) {
            if (p->Left) p = p->Left;
            else return NULL;
        } else if (p->Data < X) {
            if (p->Right) p = p->Right;
            else return NULL;
        } else return p;
    }
}

Position FindMin( BinTree BST ) {
    if (!BST) return NULL;
    Position p = BST;
    while (p->Left) p = p->Left;
    return p;
}

Position FindMax( BinTree BST ) {
    if (!BST) return NULL;
    Position p = BST;
    while (p->Right) p = p->Right;
    return p;
}

 持续更新中~

悦读

道可道,非常道;名可名,非常名。 无名,天地之始,有名,万物之母。 故常无欲,以观其妙,常有欲,以观其徼。 此两者,同出而异名,同谓之玄,玄之又玄,众妙之门。

;