Chapter 12
Chapter 12
CHAPTER 12
Abstract Data Type
q Define a stack, the basic operations on stacks, their applications, and how
they can be implemented.
q Define a queue, the basic operations on queues, their applications, and how
they can be implemented.
q Define a general linear list, the basic operations on lists, their application
and how they can be implemented.
1
3/4/2022
2
3/4/2022
12.1.3 Definition
Let us now define an ADT. An abstract data type is a data
type packaged with the operations that are meaningful for
the data type. We then encapsulate the data and the
operations on the data and hide them from the user.
3
3/4/2022
12.1.5 Implementation
Computer languages do not provide complex ADT packages.
To create a complex ADT, it is first implemented and kept in
a library. The main purpose of this chapter is to introduce
some common user-defined ADTs and their applications.
However, we also give a brief discussion of each ADT
implementation for the interested reader. We leave the
pseudocode algorithms of the implementations as
challenging exercises.
4
3/4/2022
5
3/4/2022
6
3/4/2022
7
3/4/2022
Example 12.1
8
3/4/2022
Example 12.2
9
3/4/2022
10
3/4/2022
Example 12.3
11
3/4/2022
12
3/4/2022
13
3/4/2022
14
3/4/2022
15
3/4/2022
Example 12.4
12.32
16
3/4/2022
Example 12.5
17
3/4/2022
Example 12.6
18
3/4/2022
19
3/4/2022
20
3/4/2022
21
3/4/2022
22
3/4/2022
23
3/4/2022
Example 12.7
24
3/4/2022
Example 12.8
25
3/4/2022
Example 12.9
26
3/4/2022
27
3/4/2022
Algorithm
We can write six algorithms in pseudocode for the six
operations we defined for a list in each implementation. We
showed algorithms to handle arrays and linked lists in
Chapter 11: these algorithms can be slightly modified to
create the algorithms we need for a list. We leave these as an
exercise.
28
3/4/2022
29
3/4/2022
30
3/4/2022
31
3/4/2022
Example 12.10
32
3/4/2022
Example 12.11
Huffman coding
Huffman coding is a compression technique that uses binary
trees to generate a variable length binary code from a string
of symbols. We discuss Huffman coding in detail in Chapter
15.
33
3/4/2022
Expression trees
An arithmetic expression can be represented in three
different formats: infix, postfix, and prefix. In an infix
notation, the operator comes between the two operands. In
postfix notation, the operator comes after its two operands,
and in prefix notation it comes before the two operands.
These formats are shown below for addition of two operands
A and B.
34
3/4/2022
35
3/4/2022
Example 12.12
36
3/4/2022
37
3/4/2022
BST implementation
BSTs can be implemented using either arrays or linked lists.
However, linked list structures are more common and more
efficient. The implementation uses nodes with two pointers,
left and right.
38
3/4/2022
Example 12.13
Example 12.14
39