简体   繁体   中英

Array list to linked list nodes in C

Arraylist mapping to linkedlist nodes

I was wondering if linked list in O(1) time was possible and found the link above. I do not know java but do know a bit of C, not C++. Can someone explain an approach to this concept? How would you code the array to point to the linked list? Based on my knowledge, I think I understand how to store the nodes in a **array but not sure how it can be connected to the *list that also has those same nodes.

Here is a simple example for this.

#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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM