Bootstrap

HJ48删除链表

HJ48 从指定单向链表中删除指定值的节点

在这里插入图片描述

这个代码只写了一版,虽然实际调试过程中也修改了基础,但是整体的逻辑没有太大的问题。然后在牛客编辑器上执行一次通过,太爽了。

下面的是代码

#include <stdio.h>

struct ListNodeStruct
{
    int data;
    struct ListNodeStruct* pNext;
};

typedef struct ListNodeStruct ListNode;

void PrintList(ListNode *pListNode)
{
    int index = 0;
    while(pListNode)
    {
#ifdef TEST
        printf("index:%d data:%d\n", index, pListNode->data);
#endif
        printf("%d ", pListNode->data);
        pListNode = pListNode->pNext;
        index++;
    }
}

int main()
{
    int length = 0;
    int headNodeData = 0;
    int nodeData = 0, nodePosition = 0;
    int deleteNode = 0;
    scanf("%d %d",&length, &headNodeData);
    ListNode* pListHead = (ListNode*)malloc(sizeof(ListNode));
    pListHead->data = headNodeData;
    pListHead->pNext = NULL;
    for(int i = 0; i < length - 1; i++)
    {
        scanf("%d %d",&nodeData, &nodePosition);
#ifdef TEST
        printf("%d %d\n",nodeData, nodePosition);
#endif
        ListNode *listNodeTemp = pListHead;
        while(listNodeTemp)
        {
            if(listNodeTemp->data == nodePosition)
            {
                ListNode* pListNode = (ListNode*)malloc(sizeof(ListNode));
                pListNode->data = nodeData;
                pListNode->pNext = listNodeTemp->pNext;
                listNodeTemp->pNext = pListNode;
                break;
            }
            listNodeTemp = listNodeTemp->pNext;
        }
    }

    scanf("%d", &deleteNode);
#ifdef TEST
    PrintList(pListHead);
#endif
    ListNode *preListNodeTemp = pListHead;
    if(preListNodeTemp->data == deleteNode)
    {
        ListNode *listTemp = pListHead;
        pListHead = pListHead->pNext;
        free(listTemp);
    }
    else
    {
        ListNode *curListNode = preListNodeTemp->pNext;
        while(curListNode)
        {

            if(curListNode->data == deleteNode)
            {
                preListNodeTemp->pNext = curListNode->pNext;
                free(curListNode);
                break;
            }
            preListNodeTemp = curListNode;
            curListNode = curListNode->pNext;
        }
    }
    PrintList(pListHead);
    return 0;
}

上述代码通过了,实际上还是有修改的地方的,就是代码规范相关,可以将相关代码封装成函数。一开始是纠结于要传参,但是因为执行传参导致程序异常,所以没有传参,可修改的地方就是在这里。

然后想起来有个代码可视化工具。将代码写成兼容可视化工具的形式

#include <stdio.h>

#define Visualize_Code

struct ListNodeStruct {
    int data;
    struct ListNodeStruct* pNext;
};

typedef struct ListNodeStruct ListNode;

#ifdef Visualize_Code
struct DataNodeStruct {
    int data;
    int nodePosition;
};

typedef struct DataNodeStruct DataNode;
#endif

void PrintList(ListNode* pListNode)
{
    int index = 0;
    while (pListNode) {
#ifdef TEST
        printf("index:%d data:%d\n", index, pListNode->data);
#endif
        printf("%d ", pListNode->data);
        pListNode = pListNode->pNext;
        index++;
    }
}

int main()
{
    int length = 0;
    int headNodeData = 0;
    int nodeData = 0, nodePosition = 0;
    int deleteNode = 0;
#ifndef Visualize_Code
    scanf("%d %d",&length, &headNodeData);
#else
    length = 6;
    headNodeData = 2;
    DataNode dataNode[6] = {{1, 2},
                            {3, 2},
                            {5, 1},
                            {4, 5},
                            {7, 2}};
#endif
    ListNode* pListHead = (ListNode*) malloc(sizeof(ListNode));
    pListHead->data = headNodeData;
    pListHead->pNext = nullptr;
    for (int i = 0; i < length - 1; i++) {
#ifndef Visualize_Code
        scanf("%d %d",&nodeData, &nodePosition);
#else
        nodeData = dataNode[i].data;
        nodePosition = dataNode[i].nodePosition;
#endif
#ifdef TEST
        printf("%d %d\n",nodeData, nodePosition);
#endif
        ListNode* listNodeTemp = pListHead;
        while (listNodeTemp) {
            if (listNodeTemp->data == nodePosition) {
                ListNode* pListNode = (ListNode*) malloc(sizeof(ListNode));
                pListNode->data = nodeData;
                pListNode->pNext = listNodeTemp->pNext;
                listNodeTemp->pNext = pListNode;
                break;
            }
            listNodeTemp = listNodeTemp->pNext;
        }
    }
#ifndef Visualize_Code
    scanf("%d", &deleteNode);
#else
    deleteNode = 2;
#endif
#ifdef TEST
    PrintList(pListHead);
#endif
    ListNode* preListNodeTemp = pListHead;
    if (preListNodeTemp->data == deleteNode) {
        ListNode* listTemp = pListHead;
        pListHead = pListHead->pNext;
        free(listTemp);
    } else {
        ListNode* curListNode = preListNodeTemp->pNext;
        while (curListNode) {

            if (curListNode->data == deleteNode) {
                preListNodeTemp->pNext = curListNode->pNext;
                free(curListNode);
                break;
            }
            preListNodeTemp = curListNode;
            curListNode = curListNode->pNext;
        }
    }
    PrintList(pListHead);
    return 0;
}

实际效果也实现了

在这里插入图片描述

在这里插入图片描述

;