Skip to content

Latest commit

 

History

History

链表

链表算法题目

题目列表

基础函数

函数声明

函数实现

typedef struct Node {
    int value;
    struct Node *next;
} Node;
void List_Init(Node nodeList[], int count, int baseVal = 0) {
    for (int i = 0; i < count - 1; ++i) {
        nodeList[i].value = baseVal + i;
        nodeList[i].next = &nodeList[i + 1];
    }
    nodeList[count - 1].value = baseVal + count - 1;
    nodeList[count - 1].next = nullptr;
}
void List_InitLoop(Node nodeList[], int count, int loopNodeIndex, int baseVal = 0) {
    for (int i = 0; i < count - 1; ++i) {
        nodeList[i].value = baseVal + i;
        nodeList[i].next = &nodeList[i + 1];
    }
    nodeList[count - 1].value = baseVal + count - 1;
    nodeList[count - 1].next = &nodeList[loopNodeIndex];
}
string List_GetListString(Node *head) {
    Node *tmp = head;
    stringstream text;
    while (tmp != nullptr) {
        text << tmp->value << " ";
        tmp = tmp->next;
    }
    return text.str();
}
string List_GetSequenceString(int start, int end, int step) {
    stringstream text;
    if(start <= end) {
        for (int i = start; i <= end; i += step) {
            text <<  i << " ";
        }
    } else {
        for (int i = start; i >= end; i -= step) {
            text << i << " ";
        }
    }
    return text.str();
}