Alternative data structure and algorithm of linked list

Source: Internet
Author: User
Tags definition empty header prev

In general, we use a linked list simply to hold the pointer to the next element in the linked list in the linked list node. If you want to remove the convenience, you may need to save the previous element of the pointer, that is, two-way linked list, so that when you delete a node can be quickly positioned to the front and back of the node, and change their corresponding points. In these operations, pointers to linked-list elements are undoubtedly the most critical data.

Consider such a problem, if two processes are communicating, the a process is responsible for managing the linked list, and the B process issues a request to the a process to assign or delete the linked list elements. In this case, as described above, a process directly to the B process to return the list elements of the pointer is not possible, very natural, You can think of returning another key to mark the list element. When you need to find or delete the elements of the linked list, you cannot immediately navigate to the location of the linked list elements as above, and you need to traverse the entire list. The original constant-level time complexity algorithm, after the use of the situation has changed to the O (n) level of complexity.

There are other ways to solve the problem. The first can return a key to mark the list element, and the second guarantees the complexity of the time, where a new data structure and algorithm need to be defined to solve the problem.

First, we use an array in which the elements in the array are pointers to the linked list elements, and the index of the pointer is the ID of each linked list element, so that you can immediately navigate to the corresponding list element by ID.

But here's another problem, the ID is zero-based, and if you need to allocate a new list element after a period of time, how do you know which position in the array can be allocated? Here, an array of integer data is used, Each element in the array is the ID of the next node in the list that corresponds to the ID of the linked list element (a bit clumsy). We use the two-list header, one that connects the linked list element IDs that are already in use, and the other connects the unused list element IDs. So, when the program is initialized, All IDs are saved in the unused linked list. The list that has been used is empty. Each time a new linked list element is assigned, its ID is placed at the beginning of the list, and each time a linked list element is released, its ID is placed in the unused linked list header.

Also, change the definition of the original list element, in which the saved is no longer a pointer, but an array index ID of the previous element in the list. Its next element ID is stored in the array above.

If the above explanation is not clear enough, you can look at the following code:

#include <stdio.h>

#define LIST_NODE_NULL-1
#define ARRAY_SIZE 200

/* List element definition */
typedef struct LIST_NODE
{
int prev; /* ID of the next linked list element in List_array * *
}list_node;

/* The array containing the element pointers for the linked list * *
list_node* List_array[array_size];
/* Not using the header node ID of the linked list * *
int top_of_free;
/* The head node ID of the linked list is used/*
int top_of_used;
/* Save the list of the next element node ID of the linked list.
int next_list[array_size];

void Init_list ()
{
int i;

for (i = 0; i < array_size; ++i)
{
List_array[i] = NULL;
/* Initially, the value of each node in the next_list is the next ID */
Next_list[i] = i + 1;
}

/* The last node is empty * *
Next_list[i-1] = List_node_null;

Top_of_free = 0;
top_of_used = List_node_null;
}

int Alloc_list_node ()
{
int id;

/* Never use a list header to remove an ID * *
id = top_of_free;

if (list_node_null = = ID)
{
return list_node_null;
}

/* Not using the linked header node to go down a step.
Top_of_free = Next_list[top_of_free];

if (NULL = = List_array[id])
{
List_array[id] = (list_node*) malloc (sizeof (List_node));
if (NULL = = List_array[id])
{
return list_node_null;
}
}

if (List_node_null = = top_of_used)
{
Next_list[id] = top_of_used;
top_of_used = ID;
}
Else
{
/* Modify prev and Next * *
List_array[top_of_used]->prev = ID;
List_array[id]->prev = List_node_null;

Next_list[id] = top_of_used;
top_of_used = ID;
}

return ID;
}

void Free_list_node (int id)
{
int prev, Next;

Prev = list_array[id]->prev;
Next = Next_list[id];

/* Modify Next and prev * *
if (list_node_null!= prev)
{
Next_list[prev] = Next_list[id];
}
if (list_node_null!= next && NULL!= List_array[next])
{
List_array[next]->prev = prev;
}

if (id = = top_of_used)
{
top_of_used = Next_list[id];
}

/* Returns the list ID to the free list header and modifies the free link head node.
Next_list[id] = Top_of_free;
Top_of_free = ID;
}

int main ()
{
int id;

Init_list ();

id = Alloc_list_node ();
Free_list_node (ID);

return 0;
}

The idea is ingenious, effectively avoiding the overhead of finding and deleting array elements. I don't know where the specific source is, if you know, please tell me:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.