📜  二元链表的十进制等价物(1)

📅  最后修改于: 2023-12-03 15:06:19.676000             🧑  作者: Mango

二元链表的十进制等价物

二元链表是一种特殊的双向链表,每个节点有两个指针,一个指向前驱节点,一个指向后继节点。而且,每个节点还有一个值域,值域的取值为0或1。

功能描述

二元链表的主要功能有以下几个:

  1. 创建二元链表;
  2. 在二元链表中插入节点;
  3. 在二元链表中删除节点;
  4. 遍历二元链表,并输出每个节点的值;
  5. 将二元链表转换为十进制数。
创建二元链表

我们可以通过以下代码创建一个空的二元链表:

struct Node {
    int value;
    struct Node *prev;
    struct Node *next;
};

struct LinkedList {
    struct Node *head;
    struct Node *tail;
    int size;
};

struct LinkedList *createLinkedList() {
    struct LinkedList *list = (struct LinkedList *)malloc(sizeof(struct LinkedList));
    list->head = NULL;
    list->tail = NULL;
    list->size = 0;
    return list;
}
在二元链表中插入节点

我们可以通过以下代码在二元链表的指定位置插入一个节点:

void addNode(struct LinkedList *list, int index, int value) {
    if (index < 0 || index > list->size) {
        printf("Invalid index!\n");
        return;
    }
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    newNode->value = value;
    if (index == 0) {
        newNode->prev = NULL;
        newNode->next = list->head;
        if (list->head != NULL) {
            list->head->prev = newNode;
        }
        list->head = newNode;
        if (list->tail == NULL) {
            list->tail = newNode;
        }
    } else if (index == list->size) {
        newNode->prev = list->tail;
        newNode->next = NULL;
        if (list->tail != NULL) {
            list->tail->next = newNode;
        }
        list->tail = newNode;
    } else {
        struct Node *current = list->head;
        for (int i = 0; i < index; i++) {
            current = current->next;
        }
        current->prev->next = newNode;
        newNode->prev = current->prev;
        newNode->next = current;
        current->prev = newNode;
    }
    list->size++;
}
在二元链表中删除节点

我们可以通过以下代码在二元链表的指定位置删除一个节点:

void removeNode(struct LinkedList *list, int index) {
    if (index < 0 || index >= list->size) {
        printf("Invalid index!\n");
        return;
    }
    if (index == 0) {
        struct Node *toDelete = list->head;
        list->head = toDelete->next;
        if (list->head != NULL) {
            list->head->prev = NULL;
        }
        if (toDelete == list->tail) {
            list->tail = NULL;
        }
        free(toDelete);
    } else if (index == list->size - 1) {
        struct Node *toDelete = list->tail;
        list->tail = toDelete->prev;
        if (list->tail != NULL) {
            list->tail->next = NULL;
        }
        free(toDelete);
    } else {
        struct Node *current = list->head;
        for (int i = 0; i < index; i++) {
            current = current->next;
        }
        current->prev->next = current->next;
        current->next->prev = current->prev;
        free(current);
    }
    list->size--;
}
遍历二元链表,并输出每个节点的值

我们可以通过以下代码遍历二元链表,并输出每个节点的值:

void traverseLinkedList(struct LinkedList *list) {
    struct Node *current = list->head;
    while (current != NULL) {
        printf("%d ", current->value);
        current = current->next;
    }
    printf("\n");
}
将二元链表转换为十进制数

我们可以通过以下代码将二元链表转换为十进制数:

int convertToDecimal(struct LinkedList *list) {
    struct Node *current = list->tail;
    int decimal = current->value;
    current = current->prev;
    int factor = 2;
    while (current != NULL) {
        decimal += current->value * factor;
        factor *= 2;
        current = current->prev;
    }
    return decimal;
}
总结

二元链表是一种非常有用的数据结构,我们可以使用它来存储二进制数,并将其转换为十进制数。它的主要优点是可以很方便地在其中插入和删除节点。二元链表的缺点是需要额外的存储空间来存储每个节点的值。