N0vice17 / DataStructures-And-Algorithm

This Repository covers all the topics of dsa which will help you to learn dsa in a better way

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can we improve C implementation of linked list?

letsaguiar opened this issue · comments

I have some ideas to improve the C implementation of linked list.

  1. Create a type LinkedList with a head and a tail. This will improve the performance of insert operation, since we'll don't need to traverse the list anymore.
typedef struct
{
 Node *head;
 Node *tail;
} LinkedList;
  1. In the insert method, there's no need to receive a pointer to pointer as the head node
void insert(int value, Node *head)
{
    Node    *node;
    node->data = value;
    node->next = NULL;

    while (head->next)
        head = head->next;

    if (!head)
        head = node;
    else
        head->next = node;
}
  1. Linked Lists should support search, insert at and delete at operations.

  2. We should create a destroy method to free all nodes of a list.

Yes go ahead