【问题标题】:Array list to linked list nodes in C数组列表到C中的链表节点
【发布时间】:2017-10-05 04:45:06
【问题描述】:

Arraylist mapping to linkedlist nodes

我想知道 O(1) 时间内的链表是否可行,并找到了上面的链接。我不懂java,但懂一点C,而不是C++。有人可以解释这个概念的方法吗?你将如何编码数组以指向链表?根据我的知识,我想我了解如何将节点存储在 **array 中,但不确定如何将它连接到也具有这些相同节点的 *list。

【问题讨论】:

  • TBH,我看不出这种结构的意义。在什么情况下会比按节点值排序的简单节点指针列表/数组更有利?
  • 这种数据结构似乎并没有任何优势。如果您正在维护一个数组,那么您可以通过索引直接查找,但不能有效地删除项目。同时维护一个数组和一个双向链表似乎比只拥有一个数组没有任何优势。
  • 我很想知道 O(1) 中的链表在 C 中是否可能,但我想它不是......如果我想改变周围的值,它不会是 O(1 ) 对于数组方法,因为我必须遍历它。并且找到我想要在链表中移动的值也不会是 O(1),因为我也必须遍历它。
  • 什么是“O(1) 中的链表”?没有实际的操作,谈论复杂性是没有意义的。

标签: c arrays pointers linked-list


【解决方案1】:

这是一个简单的例子。

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

struct node {
        int id;
        struct node *next;
        struct node *prev;
};
struct node *nodearray[100];
static struct node *head = NULL, *tail = NULL;

void insert_node (struct node **node,int id) {
        struct node *new = NULL;
        new = malloc(sizeof(*new));
        new->id = id;
        if (head == NULL) {
                head = new;
                new->prev = NULL;
        } else {
                tail->next = new;
                new->prev = tail;
        }
        tail = new;
        printf("Added [%d] at %p\n", id, new);
        new->next = NULL;
        *node = new;
}
void delete_node (int id) {
        struct node *node_to_del , *prev, *next;
        node_to_del = nodearray[id];
        if(!node_to_del)
            return;
        printf("Del %p\n", node_to_del);
        if (node_to_del == head) {
            head = node_to_del->next;
            node_to_del->next->prev = NULL;
        } else if (node_to_del == tail) {
            tail = node_to_del->prev;
            node_to_del->prev->next = NULL;
        } else {
        prev = node_to_del->prev;
        next = node_to_del->next;
        prev->next = next;
        next->prev = prev;
        }
        free(node_to_del);
       node_to_del = NULL;
        nodearray[id] = NULL;

}
struct node* find_node (int id) {
    return nodearray[id];
}

void list_node(void) {
        struct node *ptr = head;
        while(ptr) {
                printf("ptr[%d]  %p\n", ptr->id, ptr);
                ptr = ptr->next;
        }
}

int main () {

        insert_node(&nodearray[1] ,1);
        insert_node(&nodearray[2] ,2);
        insert_node(&nodearray[3] ,3);
        insert_node(&nodearray[4] ,4);
        insert_node(&nodearray[5] ,5);

        printf("1 at %p \n", nodearray[1]);
        printf("2 at %p \n", nodearray[2]);
        printf("3 at %p \n", nodearray[3]);
        printf("4 at %p \n", nodearray[4]);
        printf("5 at %p \n", nodearray[5]);

        delete_node(1);
        delete_node(2);
        delete_node(4);

        list_node();

        printf("1 at %p \n", nodearray[1]);
        printf("2 at %p \n", nodearray[2]);
        printf("3 at %p \n", nodearray[3]);
        printf("4 at %p \n", nodearray[4]);
        printf("5 at %p \n", nodearray[5]);

        printf("node at 5 = %p\n", nodearray[5]);

        return;
}

【讨论】:

    猜你喜欢
    • 2019-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多