CS-206 Data Structures and Algorithms Lecture 4
CS-206 Data Structures and Algorithms Lecture 4
Structures and
Algorithms
Dr. Mariam Nosheen
Presentation Agenda
Data Structures and Abstract Data Types
Data Structures, Abstract Data Types,
and Implementations
To illustrate the process of organizing and structuring data in a problem, let us consider an
example. Suppose that Trans-Fryslan Airlines operates a single l0-passenger airplane. TFA would
like to modernize its operations and, as a first step, needs a program that will determine for each
flight, which seats have been assigned and which are still available.
In this problem, it is clear that the basic object is a collection of 10 seats and, for each seat, some
indication of whether it has been assigned. We must be able to perform the following operations:
(1) Examine the collection of seats to determine which of them are available;
(2) reserve a seat; and
(3) cancel a seat assignment. To perform these operations, it is convenient to think of the
seats as organized in a list.
After organizing the data as a list of 10 seats and identifying the basic operations to be performed,
we can consider ways to implement this structure. A variety of implementations are possible. For
example, we might represent the list of seats by 10 simple variables of type char:
Data Structures, Abstract Data Types,
and Implementations
To upgrade the program that incorporates these algorithms for a
larger airplane with 25 seats
Implementation of an ADT s
Data Structures, Abstract Data Types,
and Implementations
Direct or random access means that each array element can be accessed simply by
specifying its position in the array, so that the time required to access each element
in the array is the same for all elements, regardless of their positions in the array.
A particular element of the array is then accessed by attaching to the array name one
or more indices (also called subscripts) that specify the position of that element in
the array. If only one index is used, the array is said to be one-dimensional; arrays
involving more than one index are called multidimensional arrays.
There are two categories of arrays: static arrays, for which the compiler
determines how memory will be allocated for the array, and dynamic arrays for
which memory allocation takes place during execution.
Static Arrays
Static Arrays
Static Arrays
• Out-of-range errors
• The capacity of an array cannot change during
program execution.
• An array is not an object
Multidimensional Arrays
Two-dimensional arrays
Two-dimensional arrays are useful when processing data arranged in
rows and columns. Similarly, a three-dimensional array is appropriate
when the data can be arranged in rows, columns, and ranks. When
several characteristics are associated with the data, still higher
dimensions may be appropriate, with each dimension Corresponding
to one of these characteristics
Multidimensional Arrays
Two-dimensional arrays
Two-dimensional arrays are useful when
processing data arranged in rows and
columns. Similarly, a three-dimensional
array is appropriate when the data can be
arranged in rows, columns, and ranks.
When several characteristics are associated
with the data, still higher dimensions may
be appropriate, with each dimension
Corresponding to one of these
characteristics
Multidimensional Arrays
Higher-Dimensional Arrays
The two-dimensional array scoresTable
models one page of a gradebook. To model
the entire gradebook, we can use a three-
dimensional array:
Higher-Dimensional Arrays
In some problems, arrays with even more dimensions may be useful.
For example, consider an automobile dealership that characterizes the vehicles it
sells with five attributes: year, model code, make, style, and color. We might
five-dimensional
maintain an inventory of these vehicles with a _________________array inventory
declared by
Multidimensional Arrays
Failures of new
When execution of a program begins, the program has available to it a
"storage pool" of unallocated memory locations, called the heap or free
store. The effect of the new operation is to request the operating system
to
The size of the heap is limited, however, and each execution of new causes
the pool of available memory to shrink. This means that eventually new may
request more memory than is available in the heap, so the operating system
will not be able to fill the request
Dynamic Arrays
Failures of new
Dynamic Arrays
Failures of new
Dynamic Arrays