Array as a linked list

Source: Internet
Author: User
ArticleDirectory
    • Id Allocator
    • Double linked list

Generally, the physical structure of a traditional linked list is composed of pointers connecting nodes one by one:

 
Struct node {datatype data; node * Previous; node * Next ;}

It features distribution of nodes on demand and flexible and dynamic growth.

However, another method is to implement the linked list using arrays. All nodes are in the pre-allocated array, and no pointer is used, the array subscript is used to indicate the first and next elements:

 
Struct node {datatype data; int previous; int next ;}

It features pre-allocated nodes, and if the length of the linked list needs to increase as needed, reallocation is required, similar to vector.

Next, let's talk about some of my own advantages and disadvantages and applications.

What are the advantages of using arrays as linked lists?

Can I save some memory? Not necessarily; is the speed faster? Why is this less intuitive method used to implement the linked list?

    • The memory layout of the array is relatively compact and can occupy some local light.
    • Implement the linked list structure in languages that do not provide pointers, such as VB.
    • Inter-process communication, using index is more reliable than using a pointer-the pointer address in one process is meaningless in another process
    • For some memory-intensive applications, when the value type is integer and the value range is consistent with the array index, the next pointer and data can be reused to save some memory
    • Zero integer storage to prevent the generation of memory fragments (Thanks for adding Emacs)
Implementation and Application ID Allocator

The first example is for the fourth point above. Its main application lies in ID allocation and collection. For example, each record in the database table needs a unique ID, after you add, subtract, and create a new table item, which ID should you assign to it?

    • Maintain an ID, add 1 for each added row, and do not recycle the ID for deleting the row. This way, the ID will be added infinitely, which is a waste.
    • Traverse each allocation and find the smallest unused ID. This is a waste of time.
A reasonable practice is to maintain a "available ID" linked list. Each increase will take one from the linked list. Each deletion will link the deleted ID to the linked list. However, for the traditional linked list structure, the node definition is similar:
 
Struct idnode {int availableid; idnode * Next ;};
Here, because the values of the linked list are of the same type and value range as the index of the array, We can reuse their values and next so that we only need an int array instead of an idnode array, the value of an element in the array represents the value of the linked list node and the subscript of the next node in the linked list. The following is an implementation of idallocator. The allocate and free functions are mainly provided to meet the above requirements:
Const static char list_end =-1; Template <typename integer> class idallocator {public: idallocator (INT _ num): num (_ num) {array = new integer [num]; // initialize the linked list. initially, it is a full linked list with all elements linked one after another. head = 0; For (integer I = 0; I <num; I ++) {array [I] = I + 1;} array [num-1] = list_end; count = num ;}~ Idallocator () {Delete [] array;} integer allocate () // pop_front, remove a node from the front {int id = head; If (ID! = List_end) {head = array [head]; count --;} return ID;} // push_front, add a new node to the frontvoid free (const integer & ID) {array [ID] = head; count ++; head = ID;} // push_front, add a new node to the frontbool free_s (const integer & ID) {// make sure this ID is not in the list before we add ITIF (exist (ID) return false; free (ID); Return true;} size_t size () {return count;} PRIVATE: bool exist (const integer & ID) {int I = head; while (I! = List_end) {if (I = ID) return true; I = array [I];} return false;} PRIVATE: integer * array; int num; // totall number of the arrayint count; // number in the linked listint head; // index of the head of the linked list };
Double linked list

Using arrays to implement a linked list is similar to a traditional linked list in most cases. It is nothing more than adjusting the direction of the previous and next fields after adding or deleting nodes. However, when I add a new node, how can I get a node that has not been used from the array? There are two methods:

    • If you understand the above ID Allocator, you may have realized that using the above idallocator class can simply implement this requirement.
    • Another method is similar in principle. It maintains two linked lists in the double linked list class. One is used and the other is not used.

First, because the implementation of a tool class looks much simpler, but idallocator consumes additional memory, the second method is relatively better.

Here is a rough implementation: arraylist.

Reference
    • AlgorithmIntroduction 10.3
    • Alternative linked list data structures and algorithms
    • Linked List structure principle and array simulation linked list application

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.