Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Linked list
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Linked lists using arrays of nodes === Languages that do not support any type of [[reference (computer science)|reference]] can still create links by replacing pointers with array indices. The approach is to keep an [[array data type|array]] of [[record (computer science)|record]]s, where each record has integer fields indicating the index of the next (and possibly previous) node in the array. Not all nodes in the array need be used. If records are also not supported, [[parallel array]]s can often be used instead. As an example, consider the following linked list record that uses arrays instead of pointers: '''record''' ''Entry'' { ''integer'' next; ''// index of next entry in array'' ''integer'' prev; ''// previous entry (if double-linked)'' ''string'' name; ''real'' balance; } A linked list can be built by creating an array of these structures, and an integer variable to store the index of the first element. ''integer'' listHead ''Entry'' Records[1000] Links between elements are formed by placing the array index of the next (or previous) cell into the Next or Prev field within a given element. For example: {| class="wikitable" |- ! Index ! Next ! Prev ! Name ! Balance |- | 0 | 1 | 4 | Jones, John | 123.45 |- | 1 | β1 | 0 | Smith, Joseph | 234.56 |- | 2 (listHead) | 4 | β1 | Adams, Adam | 0.00 |- | 3 | | | Ignore, Ignatius | 999.99 |- | 4 | 0 | 2 | Another, Anita | 876.54 |- | 5 | | | | |- | 6 | | | | |- | 7 | | | | |} In the above example, <code>ListHead</code> would be set to 2, the location of the first entry in the list. Notice that entry 3 and 5 through 7 are not part of the list. These cells are available for any additions to the list. By creating a <code>ListFree</code> integer variable, a [[free list]] could be created to keep track of what cells are available. If all entries are in use, the size of the array would have to be increased or some elements would have to be deleted before new entries could be stored in the list. The following code would traverse the list and display names and account balance: i := listHead '''while''' i β₯ 0 ''// loop through the list'' print i, Records[i].name, Records[i].balance ''// print entry'' i := Records[i].next When faced with a choice, the advantages of this approach include: * The linked list is relocatable, meaning it can be moved about in memory at will, and it can also be quickly and directly [[serialization|serialized]] for storage on disk or transfer over a network. * Especially for a small list, array indexes can occupy significantly less space than a full pointer on many architectures. * [[Locality of reference]] can be improved by keeping the nodes together in memory and by periodically rearranging them, although this can also be done in a general store. * NaΓ―ve [[dynamic memory allocation|dynamic memory allocators]] can produce an excessive amount of overhead storage for each node allocated; almost no allocation overhead is incurred per node in this approach. * Seizing an entry from a pre-allocated array is faster than using dynamic memory allocation for each node, since dynamic memory allocation typically requires a search for a free memory block of the desired size. This approach has one main disadvantage, however: it creates and manages a private memory space for its nodes. This leads to the following issues: * It increases complexity of the implementation. * Growing a large array when it is full may be difficult or impossible, whereas finding space for a new linked list node in a large, general memory pool may be easier. * Adding elements to a dynamic array will occasionally (when it is full) unexpectedly take linear ([[Big-O notation|O]](n)) instead of constant time (although it is still an [[amortized analysis|amortized]] constant). * Using a general memory pool leaves more memory for other data if the list is smaller than expected or if many nodes are freed. For these reasons, this approach is mainly used for languages that do not support dynamic memory allocation. These disadvantages are also mitigated if the maximum size of the list is known at the time the array is created.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Linked list
(section)
Add topic