用C语言实现了链表的基本操作:增删查改……

开始学习数据结构了,为了逆向,为了CTF!
C语言实现了一份简陋的链表,直接上代码叭
List.h

//
// Created by Mason on 2021-08-09.
//


#ifndef C90_LIST_H
#define C90_LIST_H

typedef int ListType; // type

typedef struct Node List;




struct Node {
    ListType elem;
    List* nextNode;
};

#endif //C90_LIST_H

main


#include "List.h"
#include <stdlib.h>
#include <stdio.h>


List Header = {-1, NULL};

const List* addElem(List* root, ListType elem) {
    List* node = malloc(sizeof(List));
    if (node == NULL) perror("malloc error.");
    node->nextNode = NULL;
    node->elem = elem;

    List* endNode = root;
    while (endNode->nextNode != NULL) {endNode = endNode->nextNode;}
    endNode->nextNode = node;
    return node;
}

const List* delElem(List* root, ListType elem) {
    List* endNode = root->nextNode;
    while (endNode != NULL) {
        if (endNode->nextNode->elem == elem) {
            free(endNode->nextNode);
            endNode->nextNode = NULL;
            break;
        }
        endNode = endNode->nextNode;
    }
    return endNode;
}

const List* insertElem(List* root, ListType insertElem, ListType elem) {
    List* endNode = root->nextNode;
    List* node = malloc(sizeof(List));
    if (node == NULL) perror("malloc error.");
    node->nextNode = NULL;
    node->elem = elem;

    while (endNode != NULL) {
        if (endNode->elem == insertElem) {
            node->nextNode = endNode->nextNode;
            endNode->nextNode = node;
            break;
        }
        endNode = endNode->nextNode;
    }

    if (node->nextNode == NULL) perror("insert fail, not find elem.");
    return node;
}

const List* ameElem(List* root, ListType rawElem, ListType newElem) {
    List* endNode = root->nextNode;

    while (endNode != NULL) {
        if (endNode->elem == rawElem) {
            endNode->elem = newElem;
            break;
        }
        endNode = endNode->nextNode;
    }
    return endNode;
}

const List* lastNode(List* root) {
    List* endNode = root->nextNode;

    while (endNode->nextNode != NULL) {
        endNode = endNode->nextNode;
    }
    return endNode;
}

int getListLength(List* root) {
    List* endNode = root->nextNode;
    int length = 1;
    while (endNode != NULL) {
        length++;
        endNode = endNode->nextNode;
    }
    return length;
}

// show banner list
void showList(List* root) {
    int cout = 0;
    printf("\n------------List-----------\n");
    printf("| Index | Elem |    Addr    |\n");
    for (List* i = root->nextNode; i != NULL; i = i->nextNode) {
        printf("| %05d | %04d | 0x%08x |\n", cout++, i->elem, i);
    }
    printf("------------Last------------\n");
}


void slectSort(List* root, int mode) {

    /* 选择排序
     * mode = 0 升序
     * mode = 1 降序
     * */
    switch (mode) {
        case 0:
            for (List* i = root->nextNode; i->nextNode != NULL; i = i->nextNode) {
                for (List* j = i->nextNode; j != NULL; j = j->nextNode) {

                    if (i->elem < j->elem) {
                        ListType t = i->elem;
                        i->elem = j->elem;
                        j->elem = t;
                    }
                }
            }
            break;
        case 1:
            for (List* i = root->nextNode; i->nextNode != NULL; i = i->nextNode) {
                for (List* j = i->nextNode; j != NULL; j = j->nextNode) {

                    if (i->elem > j->elem) {
                        ListType t = i->elem;
                        i->elem = j->elem;
                        j->elem = t;
                    }
                }
            }
            break;
        default:
            perror("mode error.");
            break;
    }

}

int main() {

    for (int i = 1; i < 100; ++i) {
        addElem(&Header,  i);
    }

    slectSort(&Header, 0);
    showList(&Header);

    slectSort(&Header, 1);
    showList(&Header);

    insertElem(&Header, 4, 8);
    printf("lastNode elem: %i\n", lastNode(&Header)->elem);
    delElem(&Header, 55);
    printf("lastNode elem: %i\n", lastNode(&Header)->elem);
    ameElem(&Header, 5, 100);

//    showList(&Header);

    printf("%i", Header.nextNode->nextNode->nextNode->elem);
}