0

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.

4
  • TBH, I don't see the point of such a structure. Under what circumstances would it be more advantageous than a simple list/array of node pointers sorted by node value? Commented Oct 5, 2017 at 5:08
  • That data structure doesn't really seem to have any advantages. If you're maintaining an array, then you have direct lookup by index but not the ability to efficiently delete items. Maintaining both an array and a doubly-linked list doesn't seem to have any advantages over just having an array. Commented Oct 5, 2017 at 6:00
  • I was curious to see if linked list in O(1) was possible in C, but I guess it's not... If I wanted to shift the values around it wouldn't be O(1) for array method since I'd have to traverse it. And finding the values I want to shift in a linked list would not be O(1) either since I'd have to traverse it as well. Commented Oct 5, 2017 at 6:10
  • 1
    What's "linked list in O(1)"? Without an actual operation, talking about complexity doesn't make sense. Commented Oct 5, 2017 at 7:11

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.