In
computer science
Computer science is the study of computation, information, and automation. Computer science spans Theoretical computer science, theoretical disciplines (such as algorithms, theory of computation, and information theory) to Applied science, ...
, a linked data structure is a
data structure
In computer science, a data structure is a data organization and storage format that is usually chosen for Efficiency, efficient Data access, access to data. More precisely, a data structure is a collection of data values, the relationships amo ...
which consists of a set of
data records (''
nodes'') linked together and organized by
references
A reference is a relationship between Object (philosophy), objects in which one object designates, or acts as a means by which to connect to or link to, another object. The first object in this relation is said to ''refer to'' the second object. ...
(''links'' or ''
pointers''). The link between data can also be called a connector.
In linked data structures, the links are usually treated as special
data type
In computer science and computer programming, a data type (or simply type) is a collection or grouping of data values, usually specified by a set of possible values, a set of allowed operations on these values, and/or a representation of these ...
s that can only be
dereferenced or compared for equality. Linked data structures are thus contrasted with
arrays
An array is a systematic arrangement of similar objects, usually in rows and columns.
Things called an array include:
{{TOC right
Music
* In twelve-tone and serial composition, the presentation of simultaneous twelve-tone sets such that the ...
and other data structures that require performing arithmetic operations on pointers. This distinction holds even when the nodes are actually implemented as elements of a single array, and the references are actually array
indices: as long as no arithmetic is done on those indices, the data structure is essentially a linked one.
Linking can be done in two ways using dynamic allocation and using array index linking.
Linked data structures include
linked list
In computer science, a linked list is a linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes whi ...
s,
search tree
In computer science, a search tree is a tree data structure used for locating specific keys from within a set. In order for a tree to function as a search tree, the key for each node must be greater than any keys in subtrees on the left, and les ...
s,
expression trees, and many other widely used data structures. They are also key building blocks for many efficient algorithms, such as
topological sortDonald Knuth
Donald Ervin Knuth ( ; born January 10, 1938) is an American computer scientist and mathematician. He is a professor emeritus at Stanford University. He is the 1974 recipient of the ACM Turing Award, informally considered the Nobel Prize of comp ...
, The Art of Computer Programming and
set union-find.
[ Bernard A. Galler and Michael J. Fischer. An improved equivalence algorithm. '']Communications of the ACM
''Communications of the ACM'' (''CACM'') is the monthly journal of the Association for Computing Machinery (ACM).
History
It was established in 1958, with Saul Rosen as its first managing editor. It is sent to all ACM members.
Articles are i ...
,'' Volume 7, Issue 5 (May 1964), pages 301–303. The paper originating disjoint-set forests
ACM Digital Library
/ref>
Common types of linked data structures
Linked lists
A linked list is a collection of structures ordered not by their physical placement in memory but by logical links that are stored as part of the data in the structure itself. It is not necessary that it should be stored in the adjacent memory locations. Every structure
A structure is an arrangement and organization of interrelated elements in a material object or system, or the object or system so organized. Material structures include man-made objects such as buildings and machines and natural objects such as ...
has a data field and an address field. The Address field contains the address of its successor
Successor may refer to:
* An entity that comes after another (see Succession (disambiguation))
Film and TV
* ''The Successor'' (1996 film), a film including Laura Girling
* The Successor (2023 film), a French drama film
* ''The Successor'' ( ...
.
Linked list can be singly, doubly or multiply linked and can either be linear or circular.
;Basic properties
* Objects, called nodes, are linked in a linear sequence.
* A reference to the first node of the list is always kept. This is called the 'head' or 'front'.[http://www.cs.toronto.edu/~hojjat/148s07/lectures/week5/07linked.pdf ]
''A linked list with three nodes contain two fields each: an integer value and a link to the next node''
Example in Java
This is an example of the node class used to store integers in a Java implementation of a linked list:
public class IntNode
Example in C
This is an example of the structure used for implementation of linked list in C:
struct node
;
This is an example using typedef
typedef is a reserved keyword in the programming languages C, C++, and Objective-C. It is used to create an additional name (''alias'') for another data type, but does not create a new type, except in the obscure case of a qualified typedef of ...
s:
typedef struct node node;
struct node
;
Note: A structure like this which contains a member that points to the same structure is called a self-referential structure.
Example in C++
This is an example of the node class structure used for implementation of linked list in C++:
class Node
;
Search trees
A search tree is a tree data structure in whose nodes data values can be stored from some ordered set
In mathematics, especially order theory, a partial order on a set is an arrangement such that, for certain pairs of elements, one precedes the other. The word ''partial'' is used to indicate that not every pair of elements needs to be comparable; ...
, which is such that in an in-order traversal of the tree the nodes are visited in ascending order of the stored values.
;Basic properties
* Objects, called nodes, are stored in an ordered set.
* In-order traversal
In computer science, tree traversal (also known as tree search and walking the tree) is a form of graph traversal and refers to the process of visiting (e.g. retrieving, updating, or deleting) each node in a tree data structure, exactly once. S ...
provides an ascending readout of the data in the tree.
Advantages and disadvantages
Linked list versus arrays
Compared to arrays, linked data structures allow more flexibility in organizing the data and in allocating space for it. In arrays, the size of the array must be specified precisely at the beginning, which can be a potential waste of memory, or an arbitrary limitation which would later hinder functionality in some way. A linked data structure is built dynamically and never needs to be bigger than the program requires. It also requires no guessing at creation time, in terms of how much space must be allocated. This is a feature that is key in avoiding wastes of memory.
In an array, the array elements have to be in a contiguous (connected and sequential) portion of memory. But in a linked data structure, the reference to each node gives users the information needed to find the next one. The nodes of a linked data structure can also be moved individually to different locations within physical memory without affecting the logical connections between them, unlike arrays. With due care, a certain process
A process is a series or set of activities that interact to produce a result; it may occur once-only or be recurrent or periodic.
Things called a process include:
Business and management
* Business process, activities that produce a specific s ...
or thread can add or delete nodes in one part of a data structure even while other processes or threads are working on other parts.
On the other hand, access to any particular node in a linked data structure requires following a chain of references that are stored in each node. If the structure has ''n'' nodes, and each node contains at most ''b'' links, there will be some nodes that cannot be reached in less than log''b'' ''n'' steps, slowing down the process of accessing these nodes - this sometimes represents a considerable slowdown, especially in the case of structures containing large numbers of nodes. For many structures, some nodes may require worst case
In computer science, best, worst, and average cases of a given algorithm express what the resource usage is ''at least'', ''at most'' and ''on average'', respectively. Usually the resource being considered is running time, i.e. time complexity, b ...
up to ''n''−1 steps. In contrast, many array data structures allow access to any element with a constant number of operations, independent of the number of entries.
Broadly the implementation of these linked data structure is through dynamic data structures. It gives us the chance to use particular space again. Memory can be utilized more efficiently by using these data structures. Memory is allocated as per the need and when memory is not further needed, deallocation is done.
General disadvantages
Linked data structures may also incur in substantial memory allocation
Memory management (also dynamic memory management, dynamic storage allocation, or dynamic memory allocation) is a form of resource management applied to computer memory. The essential requirement of memory management is to provide ways to dynam ...
overhead (if nodes are allocated individually) and frustrate memory paging
In computer operating systems, memory paging is a memory management scheme that allows the physical memory used by a program to be non-contiguous. This also helps avoid the problem of memory fragmentation and requiring compaction to reduce fr ...
and processor caching algorithms (since they generally have poor locality of reference
In computer science, locality of reference, also known as the principle of locality, is the tendency of a processor to access the same set of memory locations repetitively over a short period of time. There are two basic types of reference localit ...
). In some cases, linked data structures may also use more memory (for the link fields) than competing array structures. This is because linked data structures are not contiguous. Instances of data can be found all over in memory, unlike arrays.
In arrays, nth element can be accessed immediately, while in a linked data structure we have to follow multiple pointers so element access time varies according to where in the structure the element is.
In some theoretical models of computation that enforce the constraints of linked structures, such as the pointer machine, many problems require more steps than in the unconstrained random-access machine
In computer science, random-access machine (RAM or RA-machine) is a model of computation that describes an abstract machine in the general class of register machines. The RA-machine is very similar to the counter machine but with the added capab ...
model.
See also
* List of data structures
References
{{Data structures
Abstract data types
Linked lists
Trees (data structures)