• 【数据结构】青大王卓版 —— 笔记02


    单链表代码实现:

    #include <stdio.h>
    #include <malloc.h>
    #include <stdlib.h>
    
    typedef enum {
        true = 1,
        false = 0
    } boolean;
    
    typedef struct Data {
        int item1;
        char item2;
    } * PDATA, Data;
    
    typedef struct Node{
        Data data;
        struct Node * nextNode;
    } * PLINKLIST, LinkList, Node;
    
    /**
     * 创建链表
     * @param pLinklist
     */
    void initLinkList(PLINKLIST pLinklist) {
        pLinklist = (PLINKLIST) malloc(sizeof (LinkList) * 1);
        if (NULL == pLinklist) exit(-1);
        pLinklist -> nextNode = NULL;
    }
    
    /**
     * 判断链表是否为空
     * @param pLinklist
     * @return
     */
    boolean isEmpty(PLINKLIST pLinklist) {
        return NULL == pLinklist -> nextNode;
    }
    
    /**
     * 释放资源
     * @param pLinklist
     */
    void reliefLinkList(PLINKLIST pLinklist) {
        PLINKLIST tempPointer01 = pLinklist -> nextNode;
        PLINKLIST tempPointer02 = NULL;
        while (NULL != tempPointer01) {
            tempPointer02 = tempPointer01;
            tempPointer01 = tempPointer01 -> nextNode;
            free(tempPointer02);
        }
    }
    
    void clearLinkList(PLINKLIST pLinklist) {
        PLINKLIST nodeA = NULL, nodeB = NULL;
        nodeA = pLinklist -> nextNode;
        while (NULL != nodeA) {
            nodeB = nodeA -> nextNode;
            free(nodeA);
            nodeA = nodeB;
        }
        pLinklist -> nextNode = NULL;
    }
    
    int length(PLINKLIST pLinklist) {
        int len = 0;
        // if (isEmpty(pLinklist)) return len;
        PLINKLIST tempNode = pLinklist -> nextNode;
        while (NULL != tempNode) {
            tempNode = tempNode -> nextNode;
            ++ len;
        }
        return len;
    }
    
    /**
     * 获取指定索引,查找所在节点下的元素
     * @param pLinklist
     * @param index
     * @param pData
     * @return
     */
    boolean getElementByIndex(PLINKLIST pLinklist, int index, PDATA pData) {
        PLINKLIST tempNode = pLinklist -> nextNode;
        int i = 0;
        while (NULL != tempNode && i < index) {
            tempNode = tempNode -> nextNode;
            ++ i;
        }
        if (NULL == tempNode || i > index) return false;
        pData = &tempNode -> data;
        return true;
    }
    /**
     * 根据元素的地址来查找所在的节点
     * @param pLinklist
     * @param pData
     * @return
     */
    PLINKLIST getNodeByElement(PLINKLIST pLinklist, PDATA pData) {
        PLINKLIST tempNode = pLinklist -> nextNode;
        while (NULL != tempNode && &tempNode -> data != pData)  tempNode = tempNode -> nextNode;
        return tempNode;
    }
    
    /**
     * 在指定位置上插入节点并携带元素
     * @param pLinklist
     * @param pData
     * @param index
     * @return
     */
    boolean insertNode(PLINKLIST pLinklist, PDATA pData, int index) {
        PLINKLIST tempNode = pLinklist -> nextNode;
        int i = 0;
        while (NULL != tempNode && i < index) {
            tempNode = tempNode -> nextNode;
            ++ i;
        }
        if (NULL == tempNode || i > index) return false;
        PLINKLIST newNode = (PLINKLIST)malloc(sizeof(LinkList) * 1);
        newNode -> data = *pData;
        newNode -> nextNode = tempNode -> nextNode;
        tempNode -> nextNode = newNode;
        return true;
    }
    
    /**
     * 删除指定位置的节点且保留该节点的元素
     * @param pLinklist
     * @param pData
     * @param index
     * @return
     */
    boolean deleteNode(PLINKLIST pLinklist, PDATA pData, int index) {
        PLINKLIST tempNode = pLinklist -> nextNode;
        int i = 0;
        while (NULL != tempNode && i < index) {
            tempNode = tempNode -> nextNode;
            ++ i;
        }
        if (NULL == tempNode || i > index) return false;
        PLINKLIST tempNode2 = tempNode -> nextNode;
        tempNode -> nextNode = tempNode2 -> nextNode;
        pData = &tempNode2 -> data;
        free(tempNode2);
        return true;
    }
    
    /**
     * 遍历输出
     * @param pLinklist
     */
    void traverse(PLINKLIST pLinklist) {
        if (isEmpty(pLinklist)) {
            printf("[]
    ");
            return;
        }
        PLINKLIST iterator = pLinklist -> nextNode;
        int i = 1;
        printf("[");
        while (NULL != iterator) {
            if (NULL == iterator -> nextNode) {
                printf(
                        "(%d){%d, %c}]
    ",
                        i,
                        iterator -> data.item1,
                        iterator -> data.item2
                );
                break;
            }
            printf(
                "(%d){%d, %c}, ",
                i,
                iterator -> data.item1,
                iterator -> data.item2
            );
            iterator = iterator -> nextNode;
            ++ i;
        }
    }
    
    int main() {
        LinkList linkList;
        initLinkList(&linkList);
    
        PLINKLIST temp = &linkList;
        for (int i = 0; i < 5; ++i) {
            Node * newNode = (PLINKLIST) malloc(sizeof (Node));
            // 模拟存储的数据
            newNode -> data.item1 = rand() % 200 + 1;
            newNode -> data.item2 = (char)(rand() % 100 + 1);
    
            // 节点衔接与重置
            temp -> nextNode = newNode;
            newNode -> nextNode = NULL;
            temp = newNode;
        }
    
        traverse(&linkList);
        return 0;
    }
    

      

  • 相关阅读:
    luogu P1641 [SCOI2010]生成字符串
    luogu P2662 牛场围栏
    luogu P3193 [HNOI2008]GT考试
    luogu P3293 [SCOI2016]美味
    luogu P2048 [NOI2010]超级钢琴
    Wannafly挑战赛21 E 未来城市规划
    luogu P2770 航空路线问题
    luogu P4082 [USACO17DEC]Push a Box
    运维交流平台
    elk之[logstash-input-file]插件使用详解
  • 原文地址:https://www.cnblogs.com/mindzone/p/14642332.html
Copyright © 2020-2023  润新知