FACTOID # 27: If you're itching to live in a trailer park, hitch up your home and head to South Carolina, where a whopping 18% of residences are mobile homes.
 
 Home   Encyclopedia   Statistics   States A-Z   Flags   Maps   FAQ   About 
   
 
WHAT'S NEW
 

SEARCH ALL

FACTS & STATISTICS    Advanced view

Search encyclopedia, statistics and forums:

 

 

(* = Graphable)

 

 


Encyclopedia > Linked list
Linear data structures

Array
Deque
Linked list
Queue
Stack
A binary tree, a simple type of branching linked data structure. ... This article does not cite any references or sources. ... In computer science, a deque (short for double-ended queue) is a data structure for which elements can be added to or removed from the front or back. ... In providing services in computer science, transport, and operations research a queue (pronounced kyew) is a buffer where various entities such as data, objects, persons, or events are stored and waiting to be processed. ... Simple representation of a stack In computer science, a stack is a temporary abstract data type and data structure based on the principle of Last In First Out (LIFO). ...

In computer science, a linked list is one of the fundamental data structures, and can be used to implement other data structures. It consists of a sequence of nodes, each containing arbitrary data fields and one or two references ("links") pointing to the next and/or previous nodes. The principal benefit of a linked list over a conventional array is that the order of the linked items may be different from the order that the data items are stored in memory or on disk, allowing the list of items to be traversed in a different order. A linked list is a self-referential datatype because it contains a pointer or link to another datum of the same type. Linked lists permit insertion and removal of nodes at any point in the list in constant time,[1] but do not allow random access. Several different types of linked list exist: singly-linked lists, doubly-linked lists, and circularly-linked lists. Computer science, or computing science, is the study of the theoretical foundations of information and computation and their implementation and application in computer systems. ... A binary tree, a simple type of branching linked data structure. ... A node is a basic unit used to build data structures, such as linked lists and tree data structures. ... In computer science, data that has several parts can be divided into fields. ... In general, a reference is something that refers to or designates something else, or acts as a connection or a link between two things. ... This article does not cite any references or sources. ... In computer science, random access is the ability to access a random element of a group in equal time. ...


Linked lists can be implemented in most languages. Languages such as Lisp and Scheme have the data structure built in, along with operations to access the linked list. Procedural or object-oriented languages such as C, C++, and Java typically rely on mutable references to create linked lists. Lisp is a family of computer programming languages with a long history and a distinctive fully-parenthesized syntax. ... Scheme is a multi-paradigm programming language. ... C is a general-purpose, block structured, procedural, imperative computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system. ... C++ (pronounced see plus plus, IPA: ) is a general-purpose programming language with high-level and low-level capabilities. ... “Java language” redirects here. ... This article discusses a general notion of reference in computing. ...

Contents

History

Linked lists were developed in 1955-56 by Allen Newell, Cliff Shaw and Herbert Simon at RAND Corporation as the primary data structure for their Information Processing Language. IPL was used by the authors to develop several early artificial intelligence programs, including the Logic Theory Machine, the General Problem Solver, and a computer chess program. Reports on their work appeared in IRE Transactions on Information Theory in 1956, and several conference proceedings from 1957-1959, including Proceedings of the Western Joint Computer Conference in 1957 and 1958, and Information Processing (Proceedings of the first UNESCO International Conference on Information Processing) in 1959. The now-classic diagram consisting of blocks representing list nodes with arrows pointing to successive list nodes appears in "Programming the Logic Theory Machine" by Newell and Shaw in Proc. WJCC, February 1957. Newell and Simon were recognized with the ACM Turing Award in 1975 for having "made basic contributions to artificial intelligence, the psychology of human cognition, and list processing". Allen Newell (March 19, 1927 - July 19, 1992) was a researcher in computer science and cognitive psychology at the RAND corporation and at Carnegie-Mellon’s School of Computer Science. ... J.C. (Cliff) Shaw was a systems programmer at the RAND Corporation. ... Herbert Alexander Simon (June 15, 1916 – February 9, 2001) was an American political scientist whose research ranged across the fields of cognitive psychology, computer science, public administration, economics, management, and philosophy of science and a professor, most notably, at Carnegie Mellon University. ... The RAND Corporation is a nonprofit global policy think tank first formed to offer research and analysis to the United States armed forces. ... Information Processing Language (IPL) is a programming language developed by Allen Newell, Cliff Shaw, and Herbert Simon at RAND Corporation and the Carnegie Institute of Technology from about 1956. ... Garry Kasparov playing against Deep Blue, the first machine to win a chess game against a reigning world champion. ... General Problem Solver (GPS) was a computer program created in 1957 by Herbert Simon and Allen Newell to build a universal problem solver machine. ... UNESCO (United Nations Educational, Scientific and Cultural Organization) is a specialized agency of the United Nations established in 1945. ... The A.M. Turing Award is given annually by the Association for Computing Machinery to a person selected for contributions of a technical nature made to the computing community. ...


The problem of machine translation for natural language processing led Victor Yngve at Massachusetts Institute of Technology (MIT) to use linked lists as data structures in his COMIT programming language for computer research in the field of linguistics. A report on this language entitled "A programming language for mechanical translation" appeared in Mechanical Translation in 1958. Machine translation, sometimes referred to by the acronym MT, is a sub-field of computational linguistics that investigates the use of computer software to translate text or speech from one natural language to another. ... Natural language processing (NLP) is a subfield of artificial intelligence and linguistics. ... To meet Wikipedias quality standards, this article or section may require cleanup. ... The Massachusetts Institute of Technology (MIT) is a private, coeducational research university located in Cambridge, Massachusetts. ... COMIT was the first string processing language (compare SNOBOL, TRAC, and Perl), developed on the IBM 700/7000 series computers by Dr. Victor Yngve and collaborators at MIT from 1957-1965. ... Linguistics is the scientific study of language, which can be theoretical or applied. ...


LISP, standing for list processor, was created by John McCarthy in 1958 while he was at MIT and in 1960 he published its design in a paper in the Communications of the ACM, entitled "Recursive Functions of Symbolic Expressions and Their Computation by Machine, Part I". One of LISP's major data structures is the linked list. Lisp is a family of computer programming languages with a long history and a distinctive fully-parenthesized syntax. ... John McCarthy (born September 4, 1927, in Boston, Massachusetts, sometimes known affectionately as Uncle John McCarthy), is a prominent computer scientist who received the Turing Award in 1971 for his major contributions to the field of Artificial Intelligence. ... Communications of the ACM (CACM) is the flagship monthly magazine of the Association for Computing Machinery. ...


By the early 1960s, the utility of both linked lists and languages which use these structures as their primary data representation was well established. Bert Green of the MIT Lincoln Laboratory published a review article entitled "Computer languages for symbol manipulation" in IRE Transactions on Human Factors in Electronics in March 1961 which summarized the advantages of the linked list approach. A later review article, "A Comparison of list-processing computer languages" by Bobrow and Raphael, appeared in Communications of the ACM in April 1964. Mapúa Institute of Technology (MIT, MapúaTech or simply Mapúa) is a private, non-sectarian, Filipino tertiary institute located in Intramuros, Manila. ... MIT Lincoln Laboratory, also known as Lincoln Lab, is a federally funded research and development center managed by the Massachusetts Institute of Technology and funded by the United States Department of Defense. ...


Several operating systems developed by Technical Systems Consultants (originally of West Lafayette Indiana, and later of Raleigh, North Carolina) used singly linked lists as file structures. A directory entry pointed to the first sector of a file, and succeeding portions of the file were located by traversing pointers. Systems using this technique included Flex (for the Motorola 6800 CPU), mini-Flex (same CPU), and Flex9 (for the Motorola 6809 CPU). A variant developed by TSC for and marketed by Smoke Signal Broadcasting in California, used doubly linked lists in the same manner.


The TSS operating system, developed by IBM for the System 360/370 machines, used a double linked list for their file system catalog. The directory structure was similar to Unix, where a directory could contain files and/or other directories and extend to any depth. A utility flea was created to fix file system problems after a crash, since modified portions of the file catalog were sometimes in memory when a crash occurred. Problems were detected by comparing the forward and backward links for consistency. If a forward link was corrupt, then if a backward link to the infected node was found, the forward link was set to the node with the backward link. A humorous comment in the source code where this utility was invoked stated "Everyone knows a flea caller gets rid of bugs in cats".


Types of linked lists

Linearly-linked list

Singly-linked list

The simplest kind of linked list is a singly-linked list (or slist for short), which has one link per node. This link points to the next node in the list, or to a null value or empty list if it is the final node. In computer programming, null is a special value for a pointer (or other kind of reference) used to signify that the pointer intentionally does not have a target. ...

Image:Singly-linked-list.svg
A singly-linked list containing three integer values

Image File history File links This is a lossless scalable vector image. ...

Doubly-linked list

A more sophisticated kind of linked list is a doubly-linked list or two-way linked list. Each node has two links: one points to the previous node, or points to a null value or empty list if it is the first node; and one points to the next, or points to a null value or empty list if it is the final node. In computer programming, null is a special value for a pointer (or other kind of reference) used to signify that the pointer intentionally does not have a target. ... In computer programming, null is a special value for a pointer (or other kind of reference) used to signify that the pointer intentionally does not have a target. ...

Image:Doubly-linked-list.svg
A doubly-linked list containing three integer values

In some very low level languages, Xor-linking offers a way to implement doubly-linked lists using a single word for both links, although the use of this technique is usually discouraged. A similar link storage preserving technique is the subtraction edge. Image File history File links This is a lossless scalable vector image. ... XOR linked lists are a curious use of the bitwise exclusive disjunction (XOR) operation to decrease storage requirements for doubly-linked lists. ... A subtraction edge is a particular form of link address in a linked list. ...


Circularly-linked list

In a circularly-linked list, the first and final nodes are linked together. This can be done for both singly and doubly linked lists. To traverse a circular linked list, you begin at any node and follow the list in either direction until you return to the original node. Viewed another way, circularly-linked lists can be seen as having no beginning or end. This type of list is most useful for managing buffers for data ingest, and in cases where you have one object in a list and wish to see all other objects in the list.


The pointer pointing to the whole list may be called the access pointer.

Image:Circularly-linked-list.svg
A circularly-linked list containing three integer values

Image File history File links This is a lossless scalable vector image. ...

Singly-circularly-linked list

In a singly-circularly-linked list, each node has one link, similar to an ordinary singly-linked list, except that the next link of the last node points back to the first node. As in a singly-linked list, new nodes can only be efficiently inserted after a node we already have a reference to. For this reason, it's usual to retain a reference to only the last element in a singly-circularly-linked list, as this allows quick insertion at the beginning, and also allows access to the first node through the last node's next pointer. [2]


Doubly-circularly-linked list

In a doubly-circularly-linked list, each node has two links, similar to a doubly-linked list, except that the previous link of the first node points to the last node and the next />


Sentinel nodes

Linked lists sometimes have a special dummy or sentinel node at the beginning and/or at the end of the list, which is not used to store data. Its purpose is to simplify or speed up some operations, by ensuring that every data node always has a previous and/or next node, and that every list (even one that contains no data elements) always has a "first" and "last" node. Lisp has such a design - the special value nil is used to mark the end of a 'proper' singly-linked list, or chain of cons cells as they are called. A list does not have to end in nil, but a list that did not would be termed 'improper'. The sentinel lymph node is the first lymph node reached by metastasizing cancer cells from a tumor. ... Lisp is a family of computer programming languages with a long history and a distinctive fully-parenthesized syntax. ... CONS, Connection-Oriented Network Service, is one of the two OSI stack network layer protocols, the other being CLNS (Connectionless Network Service). ...


Applications of linked lists

Linked lists are used as a building block for many other data structures, such as stacks, queues and their variations. Simple representation of a stack In computer science, a stack is a temporary abstract data type and data structure based on the principle of Last In First Out (LIFO). ... In providing services in computer science, transport, and operations research a queue (pronounced kyew) is a buffer where various entities such as data, objects, persons, or events are stored and waiting to be processed. ...


The "data" field of a node can be another linked list. By this device, one can construct many linked data structures with lists; this practice originated in the Lisp programming language, where linked lists are a primary (though by no means the only) data structure, and is now a common feature of the functional programming style. Lisp is a family of computer programming languages with a long history and a distinctive fully-parenthesized syntax. ...


Sometimes, linked lists are used to implement associative arrays, and are in this context called association lists. There is very little good to be said about this use of linked lists; they are easily outperformed by other data structures such as self-balancing binary search trees even on small data sets (see the discussion in associative array). However, sometimes a linked list is dynamically created out of a subset of nodes in such a tree, and used to more efficiently traverse that set. An associative array (also map, hash, dictionary, finite map, lookup table, and in query-processing an index or index file) is an abstract data type composed of a collection of keys and a collection of values, where each key is associated with one value. ... In computing, a self-balancing binary search tree or height-balanced binary search tree is a binary search tree that attempts to keep its height, or the number of levels of nodes beneath the root, as small as possible at all times, automatically. ... An associative array (also map, hash, dictionary, finite map, lookup table, and in query-processing an index or index file) is an abstract data type composed of a collection of keys and a collection of values, where each key is associated with one value. ...


Tradeoffs

As with most choices in computer programming and design, no method is well suited to all circumstances. A linked list data structure might work well in one case, but cause problems in another. This is a list of some of the common tradeoffs involving linked list structures. In general, if you have a dynamic collection, where elements are frequently being added and deleted, and the location of new elements added to the list is significant, then benefits of a linked list increase.


Linked lists vs. arrays

Array Linked list
Indexing O(1) O(n)
Inserting / Deleting at end O(1) O(1) or O(n)[3]
Inserting / Deleting in middle (with iterator) O(n) O(1)
Persistent No Singly yes
Locality Great Bad

Linked lists have several advantages over arrays. Elements can be inserted into linked lists indefinitely, while an array will eventually either fill up or need to be resized, an expensive operation that may not even be possible if memory is fragmented. Similarly, an array from which many elements are removed may become wastefully empty or need to be made smaller. In computing, a persistent data structure is a data structure which always preserves the previous version of itself when it is modified; such data structures are effectively immutable, as their operations do not (visibly) update the structure in-place, but instead always yield a new updated structure. ... It has been suggested that this article or section be merged with Memory locality. ... This article does not cite any references or sources. ...


Further memory savings can be achieved, in certain cases, by sharing the same "tail" of elements among two or more lists — that is, the lists end in the same sequence of elements. In this way, one can add new elements to the front of the list while keeping a reference to both the new and the old versions — a simple example of a persistent data structure. In computing, a persistent data structure is a data structure which always preserves the previous version of itself when it is modified; such data structures are effectively immutable, as their operations do not (visibly) update the structure in-place, but instead always yield a new updated structure. ...


On the other hand, arrays allow random access, while linked lists allow only sequential access to elements. Singly-linked lists, in fact, can only be traversed in one direction. This makes linked lists unsuitable for applications where it's useful to look up an element by its index quickly, such as heapsort. Sequential access on arrays is also faster than on linked lists on many machines due to locality of reference and data caches. Linked lists receive almost no benefit from the cache. In computer science, random access is the ability to access a random element of a group in equal time. ... In computer science sequential access means that a group of elements (e. ... A run of the heapsort algorithm sorting an array of randomly permuted values. ... It has been suggested that this article or section be merged with Memory locality. ...


Another disadvantage of linked lists is the extra storage needed for references, which often makes them impractical for lists of small data items such as characters or boolean values. It can also be slow, and with a naïve allocator, wasteful, to allocate memory separately for each new element, a problem generally solved using memory pools. In mathematics, a Boolean function is usually a function F(b1, b2, ... , bn) of a number n of Boolean variables bi from the two-element Boolean algebra {0,1}, and such that F also takes values in {0, 1}. A function on a general domain of a function taking values... Memory pools allow dynamic memory allocation comparable to malloc or the Operator new in C++. As those implementations suffer from fragmentation because of variable block sizes, it can be impossible to use them in a real time system due to performance. ...


A number of linked list variants exist that aim to ameliorate some of the above problems. Unrolled linked lists store several elements in each list node, increasing cache performance while decreasing memory overhead for references. CDR coding does both these as well, by replacing references with the actual data referenced, which extends off the end of the referencing record. An unrolled linked list is a variation on the linked list which stores multiple elements in each node. ... In computer science CDR coding is a compressed data representation for Lisp linked lists. ...


A good example that highlights the pros and cons of using arrays vs. linked lists is by implementing a program that resolves the Josephus problem. The Josephus problem is an election method that works by having a group of people stand in a circle. Starting at a predetermined person, you count around the circle n times. Once you reach nth person, take them out of the circle and have the members close the circle. Then count around the circle the same n times and repeat the process, until only one person is left. That person wins the election. This shows the strengths and weaknesses of a linked list vs. an array, because if you view the people as connected nodes in a circular linked list then it shows how easily the linked list is able to delete nodes (as it only has to rearrange the links to the different nodes). However, the linked list will be poor at finding the next person to remove and will need to recurse through the list until it finds that person. An array, on the other hand, will be poor at deleting nodes (or elements) as it cannot remove one node without individually shifting all the elements up the list by one.However, it is exceptionally easy to find the nth person in the circle by directly referencing them by their position in the array. The Josephus problem is a theoretic problem occurring in computer science and mathematics. ...


Doubly-linked vs. singly-linked

Double-linked lists require more space per node (unless one uses xor-linking), and their elementary operations are more expensive; but they are often easier to manipulate because they allow sequential access to the list in both directions. In particular, one can insert or delete a node in a constant number of operations given only that node's address. (Compared with singly-linked lists, which require the previous node's address in order to correctly insert or delete.) Some algorithms require access in both directions. On the other hand, they do not allow tail-sharing, and cannot be used as persistent data structures. XOR linked lists are a curious use of the bitwise exclusive disjunction (XOR) operation to decrease storage requirements for doubly-linked lists. ...


Circularly-linked vs. linearly-linked

Circular linked lists are most useful for describing naturally circular structures, and have the advantage of regular structure and being able to traverse the list starting at any point. They also allow quick access to the first and last records through a single pointer (the address of the last element). Their main disadvantage is the complexity of iteration, which has subtle special cases.


Sentinel nodes

Sentinel nodes may simplify certain list operations, by ensuring that the next and/or previous nodes exist for every element. However sentinel nodes use up extra space (especially in applications that use many short lists), and they may complicate other operations. To avoid the extra space requirement the sentinel nodes can often be reused as references to the first and/or last node of the list.


Linked list operations

When manipulating linked lists in-place, care must be taken to not use values that you have invalidated in previous assignments. This makes algorithms for inserting or deleting linked list nodes somewhat subtle. This section gives pseudocode for adding or removing nodes from singly, doubly, and circularly linked lists in-place. Throughout we will use null to refer to an end-of-list marker or sentinel, which may be implemented in a number of ways. Pseudocode (derived from pseudo and code) is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of programming languages, but omits detailed subroutines, variable declarations or language-specific syntax. ... In computer programming, a sentinel value (also referred to as a flag value, rogue value, or signal value) is a special value that is used to terminate a loop that processes structured (especially sequential) data. ...


Linearly-linked lists

==== Singly-linked lists ====eep a variable firstNode which always points to the first node in the list, or is null for an empty list.

 record Node { data // The data being stored in the node next // A reference to the next node, null for last node } 
 record List { Node firstNode // points to first node of list; null for empty list } 

Traversal of a singly-linked list is simple, beginning at the first node and following each next link until we come to the end: In general, a reference is something that refers to or designates something else, or acts as a connection or a link between two things. ...

 node := list.firstNode while node not null { (do something with node.data) node := node.next } 

The following code inserts a node after an existing node in a singly linked list. The diagram shows how it works. Inserting a node before an existing one cannot be done; instead, you have to locate it while keeping track of the previous node.

 function insertAfter(Node node, Node newNode) { // insert newNode after node newNode.next := node.next node.next := newNode } 

Inserting at the beginning of the list requires a separate function. This requires updating firstNode. Diagram of inserting a node into a singly linked list, for linked list article. ...

 function insertBeginning(List list, Node newNode) { // insert node before current first node newNode.next := list.firstNode list.firstNode := newNode } 

Similarly, we have functions for removing the node after a given node, and for removing a node from the beginning of the list. The diagram demonstrates the former. To find and remove a particular node, one must again keep track of the previous element.

 function removeAfter(Node node) { // remove node past this one obsoleteNode := node.next node.next := node.next.next destroy obsoleteNode } 
 function removeBeginning(List list) { // remove first node obsoleteNode := list.firstNode list.firstNode := list.firstNode.next // point past deleted node destroy obsoleteNode } 

Notice that removeBeginning() sets list.firstNode to null when removing the last node in the list. Diagram of deleting a node from a singly linked list, created and released into the public domain by Derrick Coetzee in Adobe Photoshop and Illustrator. ...


Since we can't iterate backwards, efficient "insertBefore" or "removeBefore" operations are not possible.


Appending one linked list to another can be inefficient unless a reference to the tail is kept as part of the List structure, because we must traverse the entire first list in order to find the tail, and then append the second list to this. Thus, if two linearly-linked lists are each of length n, list appending has asymptotic time complexity of O(n). In the Lisp family of languages, list appending is provided by the append procedure. In computer programming, append is the name of a procedure for concatenating (linked) lists or arrays in some high-level programming languages. ...


Many of the special cases of linked list operations can be eliminated by including a dummy element at the front of the list. This ensures that there are no special cases for the beginning of the list and renders both insertBeginning() and removeBeginning() unnecessary. In this case, the first useful data in the list will be found at list.firstNode.next.


Doubly-linked lists

With doubly-linked lists there are even more pointers to update, but also less information is needed, since we can use backwards pointers to observe preceding elements in the list. This enables new operations, and eliminates special-case functions. We will add a prev field to our nodes, pointing to the previous element, and a lastNode field to our list structure which always points to the last node in the list. Both list.firstNode and list.lastNode are null for an empty list.

 record Node { data // The data being stored in the node next // A reference to the next node; null for last node prev // A reference to the previous node; null for first node } 
 record List { Node firstNode // points to first node of list; null for empty list Node lastNode // points to last node of list; null for empty list } 

Iterating through a doubly linked list can be done in either direction. In fact, direction can change many times, if desired. In general, a reference is something that refers to or designates something else, or acts as a connection or a link between two things. ...


Forwards

 node := list.firstNode while node ≠ null <do something with node.data> node := node.next 

Backwards

 node := list.lastNode while node ≠ null <do something with node.data> node := node.prev 

These symmetric functions add a node either after or before a given node, with the diagram demonstrating after:

 function insertAfter(List list, Node node, Node newNode) newNode.prev := node newNode.next := node.next if node.next = null list.lastNode := newNode else node.next.prev := newNode node.next := newNode 
 function insertBefore(List list, Node node, Node newNode) newNode.prev := node.prev newNode.next := node if node.prev is null list.firstNode := newNode else node.prev.next := newNode node.prev := newNode 

We also need a function to insert a node at the beginning of a possibly-empty list: Image of inserting a node into a doubly-linked list, created and granted into the public domain by User: Dcoetzee in Adobe Photoshop and Illustrator. ...

 function insertBeginning(List list, Node newNode) if list.firstNode = null list.firstNode := newNode list.lastNode := newNode newNode.prev := null newNode.next := null else insertBefore(list, list.firstNode, newNode) 

A symmetric function inserts at the end:

 function insertEnd(List list, Node newNode) if list.lastNode = null insertBeginning(list, newNode) else insertAfter(list, list.lastNode, newNode) 

Removing a node is easier, only requiring care with the firstNode and lastNode:

 function remove(List list, Node node) if node.prev = null list.firstNode := node.next else node.prev.next := node.next if node.next = null list.lastNode := node.prev else node.next.prev := node.prev destroy node 

One subtle consequence of this procedure is that deleting the last element of a list sets both firstNode and lastNode to null, and so it handles removing the last node from a one-element list correctly. Notice that we also don't need separate "removeBefore" or "removeAfter" methods, because in a doubly-linked list we can just use "remove(node.prev)" or "remove(node.next)" where these are valid.


Circularly-linked lists

Circularly-linked lists can be either singly or doubly linked. In a circularly linked list, all nodes are linked in a continuous circle, without using null. For lists with a front and a back (such as a queue), one stores a reference to the last node in the list. The next node after the last node is the first node. Elements can be added to the back of the list and removed from the front in constant time.


Both types of circularly-linked lists benefit from the ability to traverse the full list beginning at any given node. This often allows us to avoid storing firstNode and lastNode, although if the list may be empty we need a special representation for the empty list, such as a lastNode variable which points to some node in the list or is null if it's empty; we use such a lastNode here. This representation significantly simplifies adding and removing nodes with a non-empty list, but empty lists are then a special case.


Doubly-circularly-linked lists

Assuming that someNode is some node in a non-empty list, this code iterates through that list starting with someNode (any node will do):


Forwards

 node := someNode do do something with node.value node := node.next while node ≠ someNode 

Backwards

 node := someNode do do something with node.value node := node.prev while node ≠ someNode 

Notice the postponing of the test to the end of the loop. This is important for the case where the list contains only the single node someNode.


This simple function inserts a node into a doubly-linked circularly-linked list after a given element:

 function insertAfter(Node node, Node newNode) newNode.next := node.next newNode.prev := node node.next.prev := newNode node.next := newNode 

To do an "insertBefore", we can simply "insertAfter(node.prev, newNode)". Inserting an element in a possibly empty list requires a special function:

 function insertEnd(List list, Node node) if list.lastNode = null node.prev := node node.next := node else insertAfter(list.lastNode, node) list.lastNode := node 

To insert at the beginning we simply "insertAfter(list.lastNode, node)". Finally, removing a node must deal with the case where the list empties:

 function remove(List list, Node node) if node.next = node list.lastNode := null else node.next.prev := node.prev node.prev.next := node.next if node = list.lastNode list.lastNode := node.prev; destroy node 

As in doubly-linked lists, "removeAfter" and "removeBefore" can be implemented with "remove(list, node.prev)" and "remove(list, node.next)".


Linked lists using arrays of nodes

Languages that do not support any type of reference can still create links by replacing pointers with array indices. The approach is to keep an array of records, 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 not supported as well, parallel arrays can often be used instead. This article discusses a general notion of reference in computing. ... This article does not cite any references or sources. ... This article is about the data structure. ... A parallel array is a simple data structure for representing arrays of records. ...


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; } 

By creating an array of these structures, and an integer variable to store the index of the first element, a linked list can be built:

 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:

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, ListHead 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 ListFree 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. A free list is a data structure used in a scheme for dynamic memory allocation. ...


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 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 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: In computer science, in the context of data storage and transmission, serialization is the process of saving an object onto a storage medium (such as a file, or a memory buffer) or to transmit it across a network connection link, either in binary form, or in some human-readable text... It has been suggested that this article or section be merged with Memory locality. ... In computer science, dynamic memory allocation is the allocation of memory storage for use in a computer program during the runtime of that program. ...

  • It increase 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 (O(n)) instead of constant time (although it's still an 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. The Big O notation is a mathematical notation used to describe the asymptotic behavior of functions. ... In computational complexity theory, amortized analysis is the time per operation averaged over a worst_case sequence of operations. ...


Language support

Many programming languages such as Lisp and Scheme have singly linked lists built in. In many functional languages, these lists are constructed from nodes, each called a cons or cons cell. The cons has two fields: the car, a reference to the data for that node, and the cdr, a reference to the next node. Although cons cells can be used to build other data structures, this is their primary purpose. A programming language is an artificial language that can be used to control the behavior of a machine, particularly a computer. ... Lisp is a family of computer programming languages with a long history and a distinctive fully-parenthesized syntax. ... Scheme is a multi-paradigm programming language. ... Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions. ... CONS, Connection-Oriented Network Service, is one of the two OSI stack network layer protocols, the other being CLNS (Connectionless Network Service). ... Introduced in the Lisp programming language, car (IPA [kar], just like the English word car) and cdr (IPA [kʌ dər] or [ku dər]) are primitive operations upon linked lists composed of cons cells. ... Introduced in the Lisp programming language, car (IPA [kar], just like the English word car) and cdr (IPA [kʌ dər] or [ku dər]) are primitive operations upon linked lists composed of cons cells. ...


In languages that support Abstract data types or templates, linked list ADTs or templates are available for building linked lists. In other languages, linked lists are typically built using references together with records. Here is a complete example in C: In computing, an abstract data type (ADT) is a specification of a set of data and the set of operations that can be performed on the data. ... In general, a reference is something that refers to or designates something else, or acts as a connection or a link between two things. ... This article is about the data structure. ... C is a general-purpose, block structured, procedural, imperative computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system. ...

 #include <stdio.h> /* for printf */ #include <stdlib.h> /* for malloc */ typedef struct ns { int data; struct ns *next; } node; node *list_add(node **p, int i) { node *n = malloc(sizeof(node)); /* you normally don't cast a return value for malloc */ n->next = *p; *p = n; n->data = i; return n; } void list_remove(node **p) { /* remove head */ if (*p != NULL) { node *n = *p; *p = (*p)->next; free(n); } } node **list_search(node **n, int i) { while (*n != NULL) { if ((*n)->data == i) { return n; } n = &(*n)->next; } return NULL; } void list_print(node *n) { if (n == NULL) { printf("list is emptyn"); } while (n != NULL) { printf("print %p %p %dn", n, n->next, n->data); n = n->next; } } int main(void) { node *n = NULL; list_add(&n, 0); /* list: 0 */ list_add(&n, 1); /* list: 1 0 */ list_add(&n, 2); /* list: 2 1 0 */ list_add(&n, 3); /* list: 3 2 1 0 */ list_add(&n, 4); /* list: 4 3 2 1 0 */ list_print(n); list_remove(&n); /* remove first (4) */ list_remove(&n->next); /* remove new second (2) */ list_remove(list_search(&n, 1)); /* remove cell containing 1 (first) */ list_remove(&n->next); /* remove second to last node (0) */ list_remove(&n); /* remove last (3) */ list_print(n); return 0; } 

Internal and external storage

When constructing a linked list, one is faced with the choice of whether to store the data of the list directly in the linked list nodes, called internal storage, or merely to store a reference to the data, called external storage. Internal storage has the advantage of making access to the data more efficient, requiring less storage overall, having better locality of reference, and simplifying memory management for the list (its data is allocated and deallocated at the same time as the list nodes). It has been suggested that this article or section be merged with Memory locality. ...


External storage, on the other hand, has the advantage of being more generic, in that the same data structure and machine code can be used for a linked list no matter what the size of the data is. It also makes it easy to place the same data in multiple linked lists. Although with internal storage the same data can be placed in multiple lists by including multiple next references in the node data structure, it would then be necessary to create separate routines to add or delete cells based on each field. It is possible to create additional linked lists of elements that use internal storage by using external storage, and having the cells of the additional linked lists store references to the nodes of the linked list containing the data.


In general, if a set of data structures needs to be included in multiple linked lists, external storage is the best approach. If a set of data structures need to be included in only one linked list, then internal storage is slightly better, unless a generic linked list package using external storage is available. Likewise, if different sets of data that can be stored in the same data structure are to be included in a single linked list, then internal storage would be fine.


Another approach that can be used with some languages involves having different data structures, but all have the initial fields, including the next (and prev if double linked list) references in the same location. After defining separate structures for each type of data, a generic structure can be defined that contains the minimum amount of data shared by all the other structures and contained at the top (beginning) of the structures. Then generic routines can be created that use the minimal structure to perform linked list type operations, but separate routines can then handle the specific data. This approach is often used in message parsing routines, where several types of messages are received, but all start with the same set of fields, usually including a field for message type. The generic routines are used to add new messages to a queue when they are received, and remove them from the queue in order to process the message. The message type field is then used to call the correct routine to process the specific type of message.


Example of internal and external storage

Suppose you wanted to create a linked list of families and their members. Using internal storage, the structure might look like the following:

 record member { // member of a family member next string firstName integer age } record family { // the family itself family next string lastName string address member members // head of list of members of this family } 

To print a complete list of families and their members using internal storage, we could write:

 aFamily := Families // start at head of families list while aFamily ≠ null { // loop through list of families print information about family aMember := aFamily.members // get head of list of this family's members while aMember ≠ null { // loop through list of members print information about member aMember := aMember.next } aFamily := aFamily.next } 

Using external storage, we would create the following structures:

 record node { // generic link structure node next pointer data // generic pointer for data at node } record member { // structure for family member string firstName integer age } record family { // structure for family string lastName string address node members // head of list of members of this family } 

To print a complete list of families and their members using external storage, we could write:

 famNode := Families // start at head of families list while famNode ≠ null { // loop through list of families aFamily = (family)famNode.data // extract family from node print information about family memNode := aFamily.members // get list of family members while memNode ≠ null { // loop through list of members aMember := (member)memNode.data // extract member from node print information about member memNode := memNode.next } famNode := famNode.next } 

Notice that when using external storage, an extra step is needed to extract the record from the node and cast it into the proper data type. This is because both the list of families and the list of members within the family are stored in two linked lists using the same data structure (node), and this language does not have parametric types.


As long as the number of families that a member can belong to is known at compile time, internal storage works fine. If, however, a member needed to be included in an arbitrary number of families, with the specific number known only at run time, external storage would be necessary.


Speeding up search

Finding a specific element in a linked list, even if it is sorted, normally requires O(n) time (linear search). This is one of the primary disadvantages of linked lists over other data structures. In addition to some of the variants discussed in the above section, there are number of simple ways of improving search time. In computer science, linear search is a search algorithm, also known as sequential search, that is suitable for searching a set of data for a particular value. ...


In an unordered list, one simple heuristic for decreasing average search time is the move-to-front heuristic, which simply moves an element to the beginning of the list once it is found. This scheme, handy for creating simple caches, ensures that the most recently used items are also the quickest to find again.


Another common approach is to "index" a linked list using a more efficient external data structure. For example, one can build a red-black tree or hash table whose elements are references to the linked list nodes. Multiple such indexes can be built on a single list. The disadvantage is that these indexes may need to be updated each time a node is added or removed (or at least, before that index is used again). It has been suggested that Bitmap index be merged into this article or section. ... A red-black tree is a type of self-balancing binary search tree, a data structure used in computer science, typically used to implement associative arrays. ... In computer science, a hash table is a data structure that speeds up searching for information by a particular aspect of that information, called a key. ...


Related data structures

Both stacks and queues are often implemented using linked lists, and simply restrict the type of operations which are supported. Simple representation of a stack In computer science, a stack is a temporary abstract data type and data structure based on the principle of Last In First Out (LIFO). ... In providing services in computer science, transport, and operations research a queue (pronounced kyew) is a buffer where various entities such as data, objects, persons, or events are stored and waiting to be processed. ...


The skip list is a linked list augmented with layers of pointers for quickly jumping over large numbers of elements, and then descending to the next layer. This process continues down to the bottom layer, which is the actual list. Invented in 1990 by William Pugh, a skip list is a probabilistic data structure, based on parallel linked lists, with efficiency comparable to a binary search tree (order O(log n) average time for most operations). ...


A binary tree can be seen as a type of linked list where the elements are themselves linked lists of the same nature. The result is that each node may include a reference to the first node of one or two other linked lists, which, together with their contents, form the subtrees below that node. In computer science, a binary tree is a tree data structure in which each node has at most two children. ...


An unrolled linked list is a linked list in which each node contains an array of data values. This leads to improved cache performance, since more list elements are contiguous in memory, and reduced memory overhead, because less metadata needs to be stored for each element of the list. An unrolled linked list is a variation on the linked list which stores multiple elements in each node. ...


A hash table may use linked lists to store the chains of items that hash to the same position in the hash table. In computer science, a hash table is a data structure that speeds up searching for information by a particular aspect of that information, called a key. ...


References

  1. ^ The actual insertion and deletion of nodes can be done in constant time, however searching for a particular node takes linear time.
  2. ^ Preiss, Bruno R. (1999), Data Structures and Algorithms with Object-Oriented Design Patterns in Java, Wiley, pp. page 97, 165, ISBN 0471-34613-6, <http://www.brpreiss.com/books/opus5/html/page97.html>
  3. ^ If maintaining a link to the tail of the list, time is O(1); if the entire link must be searched to locate the tail link, O(n)
  • National Institute of Standards and Technology (August 16, 2004). Definition of a linked list. Retrieved December 14, 2004.
  • Antonakos, James L. and Mansfield, Kenneth C., Jr. Practical Data Structures Using C/C++ (1999). Prentice-Hall. ISBN 0-13-280843-9, pp. 165–190
  • Collins, William J. Data Structures and the Java Collections Framework (2002,2005) New York, NY: McGraw Hill. ISBN 0-07-282379-8, pp. 239–303
  • Cormen, Thomas H.; Leiserson, Charles E.; Rivest, Ronald L.; Stein, Clifford Introductions to Algorithms (2003). MIT Press. ISBN 0-262-03293-7, pp. 205–213, 501–505
  • Green, Bert F. Jr. (1961). Computer Languages for Symbol Manipulation. IRE Transactions on Human Factors in Electronics. 2 pp. 3-8.
  • McCarthy, John (1960). Recursive Functions of Symbolic Expressions and Their Computation by Machine, Part I. Communications of the ACM. [1] HTML DVI PDF PostScript
  • Donald Knuth. Fundamental Algorithms, Third Edition. Addison-Wesley, 1997. ISBN 0-201-89683-4. Sections 2.2.3–2.2.5, pp.254–298.
  • Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to Algorithms, Second Edition. MIT Press and McGraw-Hill, 2001. ISBN 0-262-03293-7. Section 10.2: Linked lists, pp.204–209.
  • Newell, Allen and Shaw, F. C. (1957). Programming the Logic Theory Machine. Proceedings of the Western Joint Computer Conference. pp. 230-240.
  • Parlante, Nick (2001). Linked list basics. Stanford University. PDF
  • Sedgewick, Robert Algorithms in C (1998). Addison Wesley. ISBN 0-201-31452-5, pp. 90–109
  • Shaffer, Clifford A. A Practical Introduction to Data Structures and Algorithm Analysis (1998). NJ: Prentice Hall. ISBN 0-13-660911-2, pp. 77–102
  • Wilkes, Maurice Vincent (1964). An Experiment with a Self-compiling Compiler for a Simple List-Processing Language. Annual Review in Automatic Programming 4, 1. Published by Pergamon Press.
  • Wilkes, Maurice Vincent (1964). Lists and Why They are Useful. Proceeds of the ACM National Conference, Philadelphia 1964 (ACM Publication P-64 page F1-1); Also Computer Journal 7, 278 (1965).
  • Kulesh Shanmugasundaram (April 4, 2005). Linux Kernel Linked List Explained.

NIST logo The National Institute of Standards and Technology (NIST, formerly known as The National Bureau of Standards) is a non-regulatory agency of the United States Department of Commerce’s Technology Administration. ... is the 228th day of the year (229th in leap years) in the Gregorian calendar. ... Year 2004 (MMIV) was a leap year starting on Thursday of the Gregorian calendar. ... is the 348th day of the year (349th in leap years) in the Gregorian calendar. ... Year 2004 (MMIV) was a leap year starting on Thursday of the Gregorian calendar. ... John McCarthy (born September 4, 1927, in Boston, Massachusetts, sometimes known affectionately as Uncle John McCarthy), is a prominent computer scientist who received the Turing Award in 1971 for his major contributions to the field of Artificial Intelligence. ... Communications of the ACM (CACM) is the flagship monthly magazine of the Association for Computing Machinery. ... Donald Ervin Knuth ( or Ka-NOOTH[1], Chinese: [2]) (b. ... Thomas H. Cormen is the co-author of Introduction to Algorithms, along with Charles Leiserson, Ron Rivest, and Cliff Stein. ... Charles E. Leiserson is a computer scientist, specializing in the theory of parallel computing and distributed computing, and particularly practical applications thereof; as part of this effort, he developed the Cilk multithreaded language. ... Professor Ron Rivest Professor Ronald Linn Rivest (born 1947, Schenectady, New York) is a cryptographer, and is the Viterbi Professor of Computer Science at MITs Department of Electrical Engineering and Computer Science. ... Clifford Stein is a computer scientist, currently working as a professor at Columbia University in New York, NY. He earned his BSE from Princeton University in 1987, a MS from Massachusetts Institute of Technology in 1989, and a PhD from Massachusetts Institute of Technology in 1992. ... Cover of the second edition Introduction to Algorithms is a book by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. ... Allen Newell (March 19, 1927 - July 19, 1992) was a researcher in computer science and cognitive psychology at the RAND corporation and at Carnegie-Mellon’s School of Computer Science. ... For other men with the same name, see Robert Sedgewick Robert Sedgewick is the author of the celebrated book series Algorithms, published by Addison-Wesley. ... Maurice V. Wilkes Maurice Vincent Wilkes (born June 26, 1913 in Dudley, Staffordshire, England) is a British computer scientist, credited with several important developments in computing. ... Maurice V. Wilkes Maurice Vincent Wilkes (born June 26, 1913 in Dudley, Staffordshire, England) is a British computer scientist, credited with several important developments in computing. ... is the 94th day of the year (95th in leap years) in the Gregorian calendar. ... Year 2005 (MMV) was a common year starting on Saturday (link displays full calendar) of the Gregorian calendar. ...

External links


  Results from FactBites:
 
SVTechie :: Online Resources For Techies BY Techies - Linked List Implementation in ASIC - ANSI C (640 words)
In Linked List Implementation in ASIC, overview of dynamic memory allocation in ASIC was presented.
A typical pointer based Link List implementation in ANSI C is described and later, an array based link list software implementation is explained.
A linked list usually consists of a root or start node, allocated on the stack (an ordinary C variable) and one or more records (or node), allocated on the heap (by calling malloc).
linked list (187 words)
doubly linked list, ordered linked list, circular list.
Note: The first item, or head, is accessed from a fixed location, called a "head pointer." An ordinary linked list must be searched with a linear search.
A linked list can be used to implement other data structures, such as a queue or a stack.
  More results at FactBites »

 
 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments

Want to know more?
Search encyclopedia, statistics and forums:

 


Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms, 1022, m