Csi 09
Csi 09
DATA
STRUCTURES
Content
9.1 Arrays
9.2 Records
9.3 Linked List
9.4 Introduction: Stack, Queue, Tree, graph
Objectives
Define an array as a data structure and how it is used to store a list of data items.
Distinguish between the name of an array and the names of the elements in an array.
Define a record as a data structure and how it is used to store attributes belonging to a
single data element.
Distinguish between the name of a record and the names of its fields.
Define a linked list as a data structure and how it is implemented using pointers.
An array is a sequenced collection of elements, of the same data type. We can refer to the elements
in the array as the first element, the second element, and so forth until we get to the last element.
If we were to put our 100 scores into an array, we could designate the elements as scores[1],
scores[2], and so on.
The index indicates the ordinal number of the element, counting from the beginning of the array. The
elements of the array are individually addressed through their subscripts (Figure 11.3).
The array as a whole has a name, scores, but each score can be accessed individually using its
subscript.
The indexes in a one-dimensional array directly define the relative positions of the
element in actual memory. A two-dimensional array, however, represents rows and
columns. How each element is stored in memory depends on the computer.
Most computers use row-major storage, in which an entire row of an array is
stored in memory before the next row. However, a computer may store the array
using column-major storage, in which the entire column is stored before the next
column.
Just like in an array, we have two types of identifier in a record: the name of
the record and the name of each individual field inside the record. The
name of the record is the name of the whole structure, while the name of
each field allows us to refer to that field.
For example, in the student record of Figure 11.7, the name of the record is
student, the name of the fields are student.id, student.name, and
student.grade
A linked list is a collection of data in which each element contains the location of
the next element—that is, each element contains two parts: data and link. The data
part holds the value information: the data to be processed.
The link is used to chain the data together, and contains a pointer (an address)
that identifies the next element in the list. In addition, a pointer variable identifies
the first element in the list. The name of the list is the same as the name of this
pointer variable.
Both an array and a linked list are representations of a list of items in memory. The
only difference is the way in which the items are linked together. In an array of
records, the linking tool is the index.
The element scores[3] is linked to the element scores[4] because the integer 4
comes after the integer 3. In a linked list, the linking tool is the link that points to
the next element—the pointer or the address of the next element.
Figure 11.11 compares the two representations for a list of five integers.
As for arrays and records, we need to distinguish between the name of the
linked list and the names of the nodes, the elements of a linked list. A linked
list must have a name.
A stack is a restricted linear list in which all additions and deletions are
made at one end, the top. If we insert a series of data into a stack and then
remove it, the order of the data is reversed. Data input as 5, 10, 15, 20, for
example, would be removed as 20, 15, 10, and 5.
Although we can define many operations for a stack, there are four basic
operations, stack, push, pop, and empty, which we define in this
chapter.
A queue is a linear list in which data can only be inserted at one end, called
the rear, and deleted from the other end, called the front. These restrictions
ensure that the data are processed through the queue in the order in which
it is received. In other words, a queue is a first in, first out (FIFO) structure.
The queue operation creates an empty queue. The following shows the format:
The dequeue operation deletes the item at the front of the queue. The following
shows the format:
The empty operation checks the status of the queue. The following shows the
format:
Queue ADT
At the ADT level, we use the queue and its four operations (queue,
enqueue, dequeue, and empty): at the implementation level, we need
to choose a data structure to implement it.
A queue ADT can be implemented using either an array or a linked list
A tree consists of a finite set of elements, called nodes (or vertices), and a
finite set of directed lines, called arcs, that connect pairs of the nodes. If the
tree is not empty, one of the nodes, called the root, has no incoming arcs.
The other nodes in a tree can be reached from the root by following a
unique path, which is a sequence of consecutive arcs. Tree structures are
normally drawn upside down with the root at the top
A binary tree is a tree in which no node can have more than two subtrees.
In other words, a node can have zero, one, or two subtrees. These subtrees
are designated as the left subtree and the right subtree.
Figure 12.22 shows a binary tree with its two subtrees
A binary tree traversal requires that each node of the tree be processed once and
only once in a predetermined sequence. The two general approaches to the
traversal sequence are depth-first and breadth-first traversal
A binary search tree (BST) is a binary tree with one extra property: the key
value of each node is greater than the key values of all nodes in each left
subtree and smaller than the value of all nodes in each right subtree. Figure
12.28 shows the idea
A graph is an ADT made of a set of nodes, called vertices, and set of lines
connecting the vertices, called edges or arcs. Whereas a tree defines a hierarchical
structure in which a node can have only one single parent, each node in a graph can
have one or more parents.
Graphs may be either directed or undirected. In a directed graph, or digraph,
each edge, which connects two vertices, has a direction (shown in the figure by an
arrowhead) from one vertex to the other. In an undirected graph, there is no
direction. Figure 12.32 shows an example of both a directed graph (a) and an
undirected graph (b).
Graphs are directly applicable to real-world scenarios. For example, we could use
graphs to model a transportation network where nodes would represent facilities that
send or receive products and edges would represent roads or paths that connect them
(see below).
Use weighted graphs, in which each edge has a weight that represent the distance
between two cities connected by that edge.