首页 > 其他分享 >链表(列表)

链表(列表)

时间:2022-10-15 23:14:59浏览次数:65  
标签:node Node int List list next 链表 列表

线性结构:一对一

非线性结构:一对多(树),多对多(图)

链表节点:数据,指针域

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define COLOR(b, a) "\033[" #b "m" a "\033[0m" // 字符串的拼接:#b 表示将 b 字符串化

#define RED(a) COLOR(31, a)
#define GREEN(a) COLOR(32, a)

//结构定义:链表节点
typedef struct Node {
    int data;
    Node *next;
} Node;

//结构定义:链表
typedef struct List {
    Node head; // 定义虚拟头节点 (index = -1) for 插入和删除时可以统一操作
    int length;
} List;

//结构操作
Node *getNewNode(int val);
List *init();
void clear_node(Node *node);
void clear(List *list);
int insert(List *list, int index, int val);
int erase(List *list, int index);
void output(List *list);
void reverse(List *list);

int main() {
    srand(time(0));
    #define MAX_N 20
    List *list = init();
    for (int i = 0; i < MAX_N; i++) {
        int op = rand() % 4;
        int index = rand() % (list->length + 1);
        int val = rand() % 100;
        switch (op) {
            case 0: {
                printf(GREEN("reverse the list!\n"));
                reverse(list);
            } break;
            case 1:
            case 2: {
                printf("insert %d at %d to the List = %d\n", val, index, insert(list, index, val));
            } break;
            case 3: {
                printf("erase a item at %d from the List = %d\n", index, erase(list, index));
            } break;
        }
        output(list), printf("\n");
    }
    #undef MAX_N
    clear(list);
    return 0;
}

//结构操作:获得一个新节点
Node *getNewNode(int val) {
    Node *node = (Node *)malloc(sizeof(Node));
    node->data = val;
    node->next = NULL;
    return node;
}

//结构操作:初始化链表
List *init() {
    List *list = (List *)malloc(sizeof(List));
    list->head.next = NULL;
    list->length = 0;
    return list;
}

//结构操作:释放一个节点
void clear_node(Node *node) {
    if (node == NULL) return;
    free(node);
    return;
}

//结构操作:销毁链表
void clear(List *list) {
    if (list == NULL) return;
    Node *node = list->head.next, *post_node;
    while (node != NULL) {
        post_node = node->next;
        clear_node(node);
        node = post_node;
    }
    free(list);
    return;
}

//结构操作:插入元素
int insert(List *list, int index, int val) {
    if (list == NULL) return 0;
    if (index < 0 || index > list->length) return 0;
    Node *pre_node = &(list->head);
    while (index--) pre_node = pre_node->next;
    Node *node = getNewNode(val);
    node->next = pre_node->next;
    pre_node->next = node;
    list->length += 1;
    return 1;
}

//结构操作:删除元素
int erase(List *list, int index) {
    if (list == NULL) return 0;
    if (index < 0 || index >= list->length) return 0;
    Node *pre_node = &(list->head);
    while (index--) pre_node = pre_node->next;
    Node *node = pre_node->next;
    pre_node->next = node->next;
    clear_node(node);
    list->length -= 1;
    return 1;
}

//结构操作:输出所有元素
void output(List *list) {
    if (list == NULL) return;
    for (Node *node = list->head.next; node != NULL; node = node->next) {
        printf("%d->", node->data);
    }
    printf("NULL\n");
    return;
}

//结构操作:反转链表
void reverse(List *list) {
    if (list == NULL) return;
    Node *pre_node = NULL, *node = list->head.next, *post_node;
    while (node != NULL) {
        post_node = node->next; //暂存
        node->next = pre_node; //反转
        pre_node = node; //下一个
        node = post_node;
    }
    list->head.next = pre_node;
    return;
}

双向链表

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define COLOR(b, a) "\033[" #b "m" a "\033[0m" // 字符串的拼接:#b 表示将 b 字符串化

#define RED(a) COLOR(31, a)
#define GREEN(a) COLOR(32, a)

//结构定义:链表节点
typedef struct Node {
    int data;
    Node *pre, *next;
} Node;

//结构定义:链表
typedef struct List {
    Node head; // 定义虚拟头节点 (index = -1) for 插入和删除时可以统一操作
    int length;
} List;

//结构操作
Node *getNewNode(int val);
List *init();
void clear_node(Node *node);
void clear(List *list);
int insert(List *list, int index, int val);
int erase(List *list, int index);
void loutput(List *list);
void routput(List *list);

int main() {
    srand(time(0));
    #define MAX_N 20
    List *list = init();
    for (int i = 0; i < MAX_N; i++) {
        int op = rand() % 4;
        int index = rand() % (list->length + 1);
        int val = rand() % 100;
        switch (op) {
            case 0:
            case 1:
            case 2: {
                printf("insert %d at %d to the List = %d\n", val, index, insert(list, index, val));
            } break;
            case 3: {
                printf("erase a item at %d from the List = %d\n", index, erase(list, index));
            } break;
        }
        loutput(list);
        routput(list);
        printf("\n");
    }
    #undef MAX_N
    clear(list);
    return 0;
}

//结构操作:获得一个新节点
Node *getNewNode(int val) {
    Node *node = (Node *)malloc(sizeof(Node));
    node->data = val;
    node->pre = NULL;
    node->next = NULL;
    return node;
}

//结构操作:初始化链表
List *init() {
    List *list = (List *)malloc(sizeof(List));
    list->head.next = NULL;
    list->length = 0;
    return list;
}

//结构操作:释放一个节点
void clear_node(Node *node) {
    if (node == NULL) return;
    free(node);
    return;
}

//结构操作:销毁链表
void clear(List *list) {
    if (list == NULL) return;
    Node *node = list->head.next, *post_node;
    while (node != NULL) {
        post_node = node->next;
        clear_node(node);
        node = post_node;
    }
    free(list);
    return;
}

//结构操作:插入元素
int insert(List *list, int index, int val) {
    if (list == NULL) return 0;
    if (index < 0 || index > list->length) return 0;
    Node *pre_node = &(list->head);
    while (index--) pre_node = pre_node->next;
    Node *node = getNewNode(val);
    node->next = pre_node->next;
    pre_node->next = node;
    node->pre = pre_node;
    if (node->next != NULL) node->next->pre = node;
    list->length += 1;
    return 1;
}

//结构操作:删除元素
int erase(List *list, int index) {
    if (list == NULL) return 0;
    if (index < 0 || index >= list->length) return 0;
    Node *pre_node = &(list->head);
    while (index--) pre_node = pre_node->next;
    Node *node = pre_node->next;
    pre_node->next = node->next;
    if (node->next != NULL) node->next->pre = pre_node;
    clear_node(node);
    list->length -= 1;
    return 1;
}

//结构操作:从前向后输出所有元素
void loutput(List *list) {
    if (list == NULL) return;
    printf("loutput: ");
    for (Node *node = list->head.next; node != NULL; node = node->next) {
        printf("%d->", node->data);
    }
    printf("null\n");
    return;
}

//结构操作:从后向前输出所有元素
void routput(List *list) {
    if (list == NULL) return;
    printf("routput: ");
    Node *node = list->head.next;
    //防止没有元素时产生段错误
    if (node == NULL) {
        printf("null\n");
        return;
    }
    while (node->next) node = node->next;
    for (; node != &(list->head); node = node->pre) {
        printf("%d->", node->data);
    }
    printf("head\n");
    return;
}

顺序表与链表对比:

顺序表优点:改(知道索引的情况下)查(有序的情况下)方便。连续存储,cache命中的概率高

链表优点:增删方便。非连续存储,无需扩容

标签:node,Node,int,List,list,next,链表,列表
From: https://www.cnblogs.com/Kelvin-Wu/p/16795295.html

相关文章