In computer science, a linked list is a data structure
consisting of a group of nodes which together represent a sequence. Under the simplest
form, each node is composed of a datum and a reference (in other words, a link) to the next node in the
sequence; more complex variants add additional links. This structure allows for
efficient insertion or removal of elements from any position in the sequence.
A linked list whose nodes contain two fields: an integer value and a link to the next node. The last node is linked to a terminator used to signify the end of the list.
Linked lists are among the simplest
and most common data structures. They can be used to implement several other
common abstract data types, including lists (the abstract data type), stacks, queues, associative arrays, and S-expressions,
though it is not uncommon to implement the other data structures directly
without using a list as the basis of implementation.
The principal benefit of a linked
list over a conventional array is that the list elements can easily be inserted or removed
without reallocation or reorganization of the entire structure because the data
items need not be stored contiguously in memory or on disk. Linked lists allow
insertion and removal of nodes at any point in the list, and can do so with a
constant number of operations if the link previous to the link being added or
removed is maintained during list traversal.
On the other hand, simple linked
lists by themselves do not allow random access
to the data, or any form of efficient indexing. Thus, many basic
operations — such as obtaining the last node of the list (assuming that
the last node is not maintained as separate node reference in the list
structure), or finding a node that contains a given datum, or locating the
place where a new node should be inserted — may require scanning most or all
of the list elements.
Linked lists were developed in
1955-56 by Allen Newell, Cliff Shaw and Herbert A. 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 to 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". 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.
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. 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.
Several operating systems developed
by Technical
Systems Consultants (originally of West Lafayette
Indiana, and later of Chapel Hill, 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/360 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 collar gets rid of bugs in cats".
Basic
concepts and nomenclature
The field of each node that contains
the address of the next node is usually called the next link or next
pointer. The remaining fields are known as the data, information,
value, cargo, or payload fields.
The head of a list is its
first node. The tail of a list may refer either to the rest of the list
after the head, or to the last node in the list. In Lisp and some derived languages, the next node may be called the
cdr
(pronounced could-er) of the list, while the payload of the head node
may be called the car.
Bob (bottom) has the key to box 201, which contains the first
half of the book and a key to box 102, which contains the rest of the book.
Post
office box analogy
The concept of a linked list can be
explained by a simple analogy to real-world post office boxes. Suppose Alice
is a spy who wishes to give a codebook to Bob by putting it in a post office
box and then giving him the key. However, the book is too thick to fit in a
single post office box, so instead she divides the book into two halves and
purchases two post office boxes. In the first box, she puts the first half of
the book and a key to the second box, and in the second box she puts the second
half of the book. She then gives Bob a key to the first box. No matter how
large the book is, this scheme can be extended to any number of boxes by always
putting the key to the next box in the previous box.
In this analogy, the boxes
correspond to elements or nodes, the keys correspond to pointers,
and the book itself is the data. The key given to Bob is the head
pointer, while those stored in the boxes are next pointers. The scheme
as described above is a singly linked list (see below).
Singly
linked list
Singly linked lists contain nodes
which have a data field as well as a next field, which points to the
next node in the linked list.
Doubly
linked list
Main article: Doubly linked list
In a doubly linked list, each
node contains, besides the next-node link, a second link field pointing to the previous
node in the sequence. The two links may be called forward(s) and backwards,
or next and prev(ious).
A doubly linked list whose nodes contain three fields: an integer value, the link forward to the next node, and the link backward to the previous node
A technique known as XOR-linking
allows a doubly linked list to be implemented using a single link field in each
node. However, this technique requires the ability to do bit operations on
addresses, and therefore may not be available in some high-level languages.
Multiply
linked list
In a multiply linked list,
each node contains two or more link fields, each field being used to connect
the same set of data records in a different order (e.g., by name, by
department, by date of birth, etc.). While doubly linked lists can be seen as
special cases of multiply linked list, the fact that the two orders are
opposite to each other leads to simpler and more efficient algorithms, so they
are usually treated as a separate case.
Circular
list
In the last node of a list, the link field often contains a null
reference, a special value used to indicate the lack of further nodes. A less
common convention is to make it point to the first node of the list; in that
case the list is said to be circular or circularly linked;
otherwise it is said to be open or linear.
In the case of a circular doubly
linked list, the only change that occurs is that the end, or "tail",
of the said list is linked back to the front, or "head", of the list
and vice versa.
Sentinel
nodes
Main article: Sentinel node
In some implementations, an extra sentinel
or dummy node may be added before the first data record and/or after the
last one. This convention simplifies and accelerates some list-handling
algorithms, by ensuring that all links can be safely dereferenced and that
every list (even one that contains no data elements) always has a
"first" and "last" node.
Empty
lists
An empty list is a list that
contains no data records. This is usually the same as saying that it has zero
nodes. If sentinel nodes are being used, the list is usually said to be empty
when it has only sentinel nodes.
Hash
linking
The link fields need not be
physically part of the nodes. If the data records are stored in an array and
referenced by their indices, the link field may be stored in a separate array
with the same indices as the data records.
List
handles
Since a reference to the first node
gives access to the whole list, that reference is often called the address,
pointer, or handle of the list. Algorithms that manipulate linked
lists usually get such handles to the input lists and return the handles to the
resulting lists. In fact, in the context of such algorithms, the word
"list" often means "list handle". In some situations,
however, it may be convenient to refer to a list by a handle that consists of
two links, pointing to its first and last nodes.
Combining
alternatives
The alternatives listed above may be
arbitrarily combined in almost every way, so one may have circular doubly
linked lists without sentinels, circular singly linked lists with sentinels,
etc.
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.
0 komentar:
Posting Komentar