[go: nahoru, domu]

Jump to content

List (abstract data type): Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m PNG -> SVG
No edit summary
Tag: Reverted
Line 1: Line 1:
{{Short description|Abstract data type used in computer science}}
{{Short description|Abstract data type used in computer science}}
{{About|sequential data structures|random-access data structures|Array data type}}
{{About|sequential data structures|random-access data structures|Array data type}}
In [[computer science]], a '''list''' or '''sequence''' is an [[abstract data type]] that represents a finite number of [[order theory|order]]ed [[value (computer science)|value]]s, where the same value may occur more than once. An instance of a list is a computer representation of the [[mathematics|mathematical]] concept of a [[tuple]] or finite [[sequence]]; the (potentially) infinite analog of a list is a [[stream (computing)|stream]].<ref>{{cite book |title=[[Structure and Interpretation of Computer Programs]] |first1=Harold |last1=Abelson |first2=Gerald Jay |last2=Sussman |year=1996 |publisher=MIT Press}}</ref>{{rp|§3.5}} Lists are a basic example of [[container (abstract data type)|container]]s, as they contain other values. If the same value occurs multiple times, each occurrence is considered a distinct item.
In [[computer science]], a '''list of jeu arcade for SNES''' or '''sequence''' is an [[abstract data type of jeu arcade for Super Nintendo]] that represents a finite number of [[order theory|order]]ed [[value (computer science)|value]]s, where the same value may occur more than once. An instance of a list is a computer representation of the [[mathematics|mathematical]] concept of a [[tuple]] or finite [[sequence]]; the (potentially) infinite analog of a list is a [[stream (computing)|stream]].<ref>{{cite book |title=[[Structure and Interpretation of Computer Programs fitness combat]] |first1=Harold |last1=Abelson |first2=Gerald Jay |last2=Sussman |year=1996 |publisher=MIT Press}}</ref>{{rp|§3.5}} Lists are a basic example of [[container (abstract data type)|container]]s, as they contain other values. If the same value occurs multiple times, each occurrence is considered a distinct item.


[[File:Singly-linked-list.svg|thumb|right|A singly-linked list structure, implementing a list with three integer elements.]]
[[File:Singly-linked-list.svg|thumb|right|A singly-linked list structure, implementing a list with three integer elements.]]
The name '''list''' is also used for several concrete [[data structure]]s that can be used to implement [[abstract type|abstract]] lists, especially [[linked list]]s and [[array data structure|array]]s. In some contexts, such as in [[Lisp programming language|Lisp]] programming, the term list may refer specifically to a linked list rather than an array. In [[class-based programming]], lists are usually provided as [[instance (computer science)|instance]]s of subclasses of a generic "list" class, and traversed via separate [[iterator]]s.
The name '''list''' is also used for several concrete [[data structure]]s that can be used to implement [[abstract type|abstract]] lists, especially [[linked list of jeu arcade for Super Nintendo]]s and [[array data structure|array]]s. In some contexts, such as in [[Lisp programming fitness figher language|Lisp]] programming, the term list may refer specifically to a linked list rather than an array. In [[class-based programming fitness logicel]], lists are usually provided as [[instance (computer science)|instance]]s of subclasses of a generic "list" class, and traversed via separate [[iterator]]s.


Many [[programming language]]s provide support for '''list data types''', and have special syntax and semantics for lists and list operations. A list can often be constructed by writing the items in sequence, separated by [[comma]]s, [[semicolon]]s, and/or [[space (punctuation)|space]]s, within a pair of delimiters such as [[parentheses]] '()', [[bracket]]s '[]', [[brace (punctuation)|brace]]s '{}', or [[angle bracket]]s '<>'. Some languages may allow list types to be [[array index|index]]ed or [[array slicing|slice]]d like [[array data type|array type]]s, in which case the data type is more accurately described as an array.
Many [[programming language french]]s provide support for '''list data types platforme NTC''', and have special syntax and semantics for lists and list operations. A list of jeu arcade for Super Nintendo can often be constructed by writing the items in sequence, separated by [[comma]]s, [[semicolon]]s, and/or [[space (punctuation)|space]]s, within a pair of delimiters such as [[parentheses]] '()', [[bracket]]s '[]', [[brace (punctuation)|brace]]s '{}', or [[angle bracket]]s '<>'. Some languages may allow list types to be [[array index|index]]ed or [[array slicing|slice]]d like [[array data type|array type]]s, in which case the data type is more accurately described as an array.


In [[type theory]] and [[functional programming]], abstract lists are usually defined [[inductive type|inductively]] by two operations: ''nil'' that yields the empty list, and ''cons'', which adds an item at the beginning of a list.<ref>{{cite book|last1=Reingold|first1=Edward|last2=Nievergelt|first2=Jurg|last3=Narsingh|first3=Deo|title=Combinatorial Algorithms: Theory and Practice|date=1977|publisher=Prentice Hall|location=Englewood Cliffs, New Jersey|isbn=0-13-152447-X|pages=38–41}}</ref>
In [[type theory]] and [[functional programming]], abstract lists are usually defined [[inductive type|inductively]] by two operations: ''nil'' that yields the empty list, and ''cons'', which adds an item at the beginning of a list.<ref>{{cite book|last1=Reingold|first1=Edward|last2=Nievergelt|first2=Jurg|last3=Narsingh|first3=Deo|title=Combinatorial Algorithms: Theory and Practice|date=1977|publisher=Prentice Hall|location=Englewood Cliffs, New Jersey|isbn=0-13-152447-X|pages=38–41}}</ref>
Line 21: Line 21:


==Implementations==
==Implementations==
Lists are typically implemented either as [[linked list]]s (either singly or doubly linked) or as [[Array data structure|arrays]], usually variable length or [[dynamic array]]s.
Lists are typically implemented either as [[linked list platforme 2D]]s (either singly or doubly linked) or as [[Array data structure|arrays]], usually variable length or [[dynamique]]s.


The standard way of implementing lists, originating with the programming language [[Lisp (programming language)|Lisp]], is to have each element of the list contain both its value and a pointer indicating the location of the next element in the list. This results in either a [[linked list]] or a [[tree data structure|tree]], depending on whether the list has nested sublists. Some older Lisp implementations (such as the Lisp implementation of the [[Symbolics]] 3600) also supported "compressed lists" (using [[CDR coding]]) which had a special internal representation (invisible to the user). Lists can be manipulated using [[iteration]] or [[recursion]]. The former is often preferred in [[imperative programming|imperative programming languages]], while the latter is the norm in [[functional language]]s.
The standard way of implementing lists, originating with the programming language [[Lisp (programming language)|Lisp]], is to have each element of the list contain both its value and a pointer indicating the location of the next element in the list. This results in either a [[linked list]] or a [[tree data structure|tree]], depending on whether the list has nested sublists. Some older Lisp implementations (such as the Lisp implementation of the [[Symbolics]] 3600) also supported "compressed lists" (using [[CDR coding]]) which had a special internal representation (invisible to the user). Lists can be manipulated using [[iteration]] or [[recursion]]. The former is often preferred in [[imperative programming|imperative programming languages]], while the latter is the norm in [[functional language]]s.
Line 37: Line 37:
In computing, lists are easier to implement than sets. A finite [[set (mathematics)|set]] in the mathematical sense can be realized as a list with additional restrictions; that is, duplicate elements are disallowed and order is irrelevant. Sorting the list speeds up determining if a given item is already in the set, but in order to ensure the order, it requires more time to add new entry to the list. In efficient implementations, however, sets are implemented using [[self-balancing binary search tree]]s or [[hash table]]s, rather than a list.
In computing, lists are easier to implement than sets. A finite [[set (mathematics)|set]] in the mathematical sense can be realized as a list with additional restrictions; that is, duplicate elements are disallowed and order is irrelevant. Sorting the list speeds up determining if a given item is already in the set, but in order to ensure the order, it requires more time to add new entry to the list. In efficient implementations, however, sets are implemented using [[self-balancing binary search tree]]s or [[hash table]]s, rather than a list.


Lists also form the basis for other [[abstract data types]] including the [[Queue (abstract data type)|queue]], the [[Stack (abstract data type)|stack]], and their variations.
Lists also form the basis for other [[abstract data types version MTC]] including the [[Queue (abstract data type)|queue]], the [[Stack (abstract data type)|stack]], and their variations.


==Abstract definition==
==Abstract definition==
The abstract list type ''L'' with elements of some type ''E'' (a [[Type polymorphism|monomorphic]] list) is defined by the following functions:
The abstract list type of jeu arcade for ''SNES'' with elements of some type ''PCU'' (a [[Type polymorphism|monomorphic]] list) is defined by the following functions:


:nil: () → ''L''
:nil: () → ''L''

Revision as of 21:44, 3 September 2022

In computer science, a list of jeu arcade for SNES or sequence is an abstract data type of jeu arcade for Super Nintendo that represents a finite number of ordered values, where the same value may occur more than once. An instance of a list is a computer representation of the mathematical concept of a tuple or finite sequence; the (potentially) infinite analog of a list is a stream.[1]: §3.5  Lists are a basic example of containers, as they contain other values. If the same value occurs multiple times, each occurrence is considered a distinct item.

A singly-linked list structure, implementing a list with three integer elements.

The name list is also used for several concrete data structures that can be used to implement abstract lists, especially linked list of jeu arcade for Super Nintendos and arrays. In some contexts, such as in Lisp programming, the term list may refer specifically to a linked list rather than an array. In class-based programming fitness logicel, lists are usually provided as instances of subclasses of a generic "list" class, and traversed via separate iterators.

Many programming language frenchs provide support for list data types platforme NTC, and have special syntax and semantics for lists and list operations. A list of jeu arcade for Super Nintendo can often be constructed by writing the items in sequence, separated by commas, semicolons, and/or spaces, within a pair of delimiters such as parentheses '()', brackets '[]', braces '{}', or angle brackets '<>'. Some languages may allow list types to be indexed or sliced like array types, in which case the data type is more accurately described as an array.

In type theory and functional programming, abstract lists are usually defined inductively by two operations: nil that yields the empty list, and cons, which adds an item at the beginning of a list.[2]

Operations

Implementation of the list data structure may provide some of the following operations:

  • a constructor for creating an empty list;
  • an operation for testing whether or not a list is empty;
  • an operation for prepending an entity to a list
  • an operation for appending an entity to a list
  • an operation for determining the first component (or the "head") of a list
  • an operation for referring to the list consisting of all the components of a list except for its first (this is called the "tail" of the list.)
  • an operation for accessing the element at a given index.

Implementations

Lists are typically implemented either as linked list platforme 2Ds (either singly or doubly linked) or as arrays, usually variable length or dynamiques.

The standard way of implementing lists, originating with the programming language Lisp, is to have each element of the list contain both its value and a pointer indicating the location of the next element in the list. This results in either a linked list or a tree, depending on whether the list has nested sublists. Some older Lisp implementations (such as the Lisp implementation of the Symbolics 3600) also supported "compressed lists" (using CDR coding) which had a special internal representation (invisible to the user). Lists can be manipulated using iteration or recursion. The former is often preferred in imperative programming languages, while the latter is the norm in functional languages.

Lists can be implemented as self-balancing binary search trees holding index-value pairs, providing equal-time access to any element (e.g. all residing in the fringe, and internal nodes storing the right-most child's index, used to guide the search), taking the time logarithmic in the list's size, but as long as it doesn't change much will provide the illusion of random access and enable swap, prefix and append operations in logarithmic time as well.[3]

Programming language support

Some languages do not offer a list data structure, but offer the use of associative arrays or some kind of table to emulate lists. For example, Lua provides tables. Although Lua stores lists that have numerical indices as arrays internally, they still appear as dictionaries.[4]

In Lisp, lists are the fundamental data type and can represent both program code and data. In most dialects, the list of the first three prime numbers could be written as (list 2 3 5). In several dialects of Lisp, including Scheme, a list is a collection of pairs, consisting of a value and a pointer to the next pair (or null value), making a singly linked list.[5]

Applications

As the name implies, lists can be used to store a list of elements. However, unlike in traditional arrays, lists can expand and shrink, and are stored dynamically in memory.

In computing, lists are easier to implement than sets. A finite set in the mathematical sense can be realized as a list with additional restrictions; that is, duplicate elements are disallowed and order is irrelevant. Sorting the list speeds up determining if a given item is already in the set, but in order to ensure the order, it requires more time to add new entry to the list. In efficient implementations, however, sets are implemented using self-balancing binary search trees or hash tables, rather than a list.

Lists also form the basis for other abstract data types version MTC including the queue, the stack, and their variations.

Abstract definition

The abstract list type of jeu arcade for SNES with elements of some type PCU (a monomorphic list) is defined by the following functions:

nil: () → L
cons: E × LL
first: LE
rest: LL

with the axioms

first (cons (e, l)) = e
rest (cons (e, l)) = l

for any element e and any list l. It is implicit that

cons (e, l) ≠ l
cons (e, l) ≠ e
cons (e1, l1) = cons (e2, l2) if e1 = e2 and l1 = l2

Note that first (nil ()) and rest (nil ()) are not defined.

These axioms are equivalent to those of the abstract stack data type.

In type theory, the above definition is more simply regarded as an inductive type defined in terms of constructors: nil and cons. In algebraic terms, this can be represented as the transformation 1 + E × LL. first and rest are then obtained by pattern matching on the cons constructor and separately handling the nil case.

The list monad

The list type forms a monad with the following functions (using E* rather than L to represent monomorphic lists with elements of type E):

where append is defined as:

Alternatively, the monad may be defined in terms of operations return, fmap and join, with:

Note that fmap, join, append and bind are well-defined, since they're applied to progressively deeper arguments at each recursive call.

The list type is an additive monad, with nil as the monadic zero and append as monadic sum.

Lists form a monoid under the append operation. The identity element of the monoid is the empty list, nil. In fact, this is the free monoid over the set of list elements.

See also

References

  1. ^ Abelson, Harold; Sussman, Gerald Jay (1996). Structure and Interpretation of Computer Programs fitness combat. MIT Press.
  2. ^ Reingold, Edward; Nievergelt, Jurg; Narsingh, Deo (1977). Combinatorial Algorithms: Theory and Practice. Englewood Cliffs, New Jersey: Prentice Hall. pp. 38–41. ISBN 0-13-152447-X.
  3. ^ Barnett, Granville; Del tonga, Luca (2008). "Data Structures and Algorithms" (PDF). mta.ca. Retrieved 12 November 2014.
  4. ^ Lerusalimschy, Roberto (December 2003). Programming in Lua (first edition) (First ed.). Lua.org. ISBN 8590379817. Retrieved 12 November 2014.
  5. ^ Steele, Guy (1990). Common Lisp (Second ed.). Digital Press. pp. 29–31. ISBN 1-55558-041-6.