Lesson2a - LINEAR LIST
Lesson2a - LINEAR LIST
Lesson2a - LINEAR LIST
A list is a finite sequence of zero or more elements. If the elements are all of type T,
then we say that the type of the list is “list of T.” Thus we can have lists of integers,
lists of real numbers, lists of structures, lists of lists of integers, and so on. We
generally expect the elements of a list to be of one type. However, since a type can
be the union of several types, the restriction to a single “type” can be circumvented.
A list is often written with its elements separated by commas and enclosed in
parentheses:
(a1, a2 , . . . , an ), where the ai ’s are the elements of the list.
Student ages:
18, 17, 17, 19, 18, 20
Character string
Operations on Lists
a) Sorting and concatenation (merging)
A great variety of operations can be performed on lists. Formally, the operation of sorting a
list (a1, a2, . . . , an ) amounts to replacing the list with a list consisting of a permutation of its
elements, (b1 , b2, . . . , bn ), such that b1 ≤ b2 ≤ ≤ bn . Here, as before, ≤ represents an
ordering of the elements, such as “less than or equal to” on integers or reals, or lexicographic
order on strings. The operation of concatenating two sorted lists consists of constructing from
them a sorted list containing the same elements as the two given lists. Multiplicity must be
preserved; that is, if there are k occurrences of element ‘a’ among the two given lists, then the
resulting list has k occurrences of ‘a’.
Examples:
1) Let L be the list (1, 2, 3, 2). The result of
insert(1, L)
could be the list
(1, 1, 2, 3, 2), if we chose to push 1, that is, to insert 1 at the beginning. We
could also insert the new 1 at the end, yielding (1, 2, 3, 2, 1). Alternatively, the
new 1 could be placed in any of three positions interior to the list L.
2) The result of delete(2, L) is the list (1, 3, 2) if we delete the first occurrence of 2
3) If we ask search(x, L), the answer is TRUE if x is 1, 2, or 3, but
FALSE if x is any other value.
c) Concatenation
We concatenate two lists L and M by forming the list that begins with the
elements of L and then continues with the elements of M .
That is, if
M = (b1, b2, . . . , bk ),
Note that the empty list is the identity for concatenation. That is, for any list L, we
have ǫL = Lǫ = L.
Example:
If L is the list (1, 2, 3) and M is the list (3, 1), then LM is the list (1, 2, 3, 3, 1). If L is
the character string dog and M is the character string house, then LM is the
character string doghouse.
a) The operation first(L) returns the first element of list L, and last(L) returns the last element
of L. Both cause an error if L is an empty list
b) The operation retrieve(i, L) returns the element at the ith position of list L.
It is an error if L has length less than i.
c) length(L), which returns the length of list L.
d) isEmpty(L), which returns TRUE if L is an empty list and returns FALSE if not.
V [i] = X
8) We also increase the length of the list by one using statement: n = n + 1. At this point, we can
bring the process to a stop (End).