Chapter 3- Abstract Data Types
Chapter 3- Abstract Data Types
1
Abstract data types
2
•An abstract data type or ADT (sometimes called an abstract
data type) is a mathematical model of a data structure.
•It describes a container which holds a finite number of
objects where the objects may be associated through a given
binary relationship.
•The operations which may be performed on the container
may be basic (e.g., insert, remove, etc.) or may be based on the
relationship (e.g, given an object (possibly already in the
container), find the next largest object).
• Thus, to describe an abstract data structure we must:
Give the relationship between the objects being stored
Specify the operations which are to be optimized with respect
to time and/or space.
• Array and the linked list are typical examples. 3
• A linear array is a list of finite number n of homogeneous data
elements such that :
a) The elements of the array are referenced respectively by an
index set consisting of n consecutive numbers.
b) The elements of the array are stored respectively in
successive memory locations.
• The number n of elements is called the length or
size of the
array.
6
Example :
An automobile company uses an array AUTO to record the
number of auto mobile sold each year from 1932 through 1984.
AUTO[k] = Number of auto mobiles sold in the year K
LB = 1932
UB = 1984
Length = UB – LB+1 = 1984 – 1930+1 =55
7
Let LA be a linear array in the memory of the computer. The
memory of the computer is a sequence of addressed locations.
The computer does not need to keep track
LA of the address of every element of LA, but
100 needs to keep track only of the first
0 element of LA, denoted by
100
1 Base(LA)
100
2 Called the base address of LA. Using this
100 address Base(LA), the computer calculates
3 the address of any element of LA by the
100
4
following formula :
100 LOC(LA[k]) = Base(LA) + w(K – lower
5
bound)
Fig : Computer
Where w is the number of words per
memory
20
0
Example :
20 An automobile company uses an array
1 AUTO[193
2] AUTO to record the number of auto
20
2
mobile sold each year from 1932
through 1984. Suppose AUTO appears
20
3 AUTO[193 in memory as pictured in fig A . That is
20 3] Base(AUTO) = 200, and w = 4 words per
4 memory cell for AUTO. Then,
20 LOC(AUTO[1932]) = 200,
AUTO[193 LOC(AUTO[1933]) =204
5
4]
20 LOC(AUTO[1934]) = 208
6 the address of the array element for
20 the year K = 1965 can be :
7
20
8 Fig : 1
20 Example :
0 An automobile company uses an array
20 AUTO to record the number of auto
1 AUTO[193 mobile sold each year from 1932
20 2] through 1984. Suppose AUTO appears
2 in memory as pictured in fig A . That is
20 Base(AUTO) = 200, and w = 4 words per
3 AUTO[193 memory cell for AUTO. Then,
20 3] LOC(AUTO[1932]) = 200,
4 LOC(AUTO[1933]) =204
20 LOC(AUTO[1934]) = 208
AUTO[193
5 the address of the array element for the
4]
20 year K = 1965 can be obtained by using
6
:
20 LOC(AUTO[1965]) = Base(AUTO) +
7
w(1965 – lower bound)
20
8 Fig :
=200+4(1965-1932)=332 1
Example: We have an array AAA(5:50). Find the Location of
AAA(5)
11
Example: We have an array AAA(5:50). Find the Location
of AAA(5) where
By the Formula
12
Example: We have an array AAA(5:50). Find the Location of
AAA(5) where
By the Formula
4(5-5) = 300
13
Example: We have an array AAA(5:50). Find the Location of
AAA(15)
where
Base=300, and w=4
14
Example: We have an array AAA(5:50). Find the Location of
AAA(15) where
Base=300, and w=4
By the Formula
15
Example: We have an array AAA(5:50). Find the Location of
AAA(35) where
By the Formula
Loc (LA[K]) =
Base(LA)+w(K-LB)
Loc(AAA[15])= 300
+ 4(15-5) = 340
16
Example: We have an array AAA(5:50). Find the Location of
AAA(35) where
By the Formula
Loc (LA[K]) =
Base(LA)+w(K-LB)
Loc(AAA[15])= 300
+ 4(35-5) = 420
17
Example: We have an array BBB(-5:50), ) where Base=300,
and w=4
(i) BBB(0)
(ii) BBB(5)
(iii) BBB(8)
(iv) BBB(18)
(v) BBB (20)
18
Example: We have an array BBB(-5:50), ) where Base=300, and
w=4
20
•Print the contents of each element of DATA or Count
the number of elements of DATA with a given property.
•This can be accomplished by traversing DATA, That is,
by accessing and processing (visiting) each element of
DATA exactly once.
21
Algorithm 1: Given DATA is a linear array with lower bound LB
and upper bound UB . This algorithm traverses DATA applying an
operation PRINT to each element of DATA.
22
Algorithm 4.1: Given DATA is a linear array with lower bound
LB and upper bound UB . This algorithm traverses DATA applying
an operation PRINT to each element of DATA.
1. Set K : = LB.
2. Repeat steps 3 and 4 while K<=UB:
3. Write: DATA[K]
4. Set K : = K+1.
5. Exit.
23
Algorithm 4.1: Given DATA is a linear array with lower bound
LB and upper bound UB . This algorithm traverses DATA applying
an operation PRINT to each element of DATA.
1. Set K : = LB.
2. Repeat steps 3 and 4 while K<=UB:
3. Write: DATA[K]
4. Set K : = K+1.
5. Exit.
1. Set K : = LB.
2. Repeat steps 3 and 4 while K<=UB:
3. Write: DATA[K]
4. Set K : = K+1.
5. Exit.
a) Find the number NUM of years during which more than 300
automobiles were sold.
26
Example 1 :
An automobile company uses an array AUTO to record the number
of auto mobile sold each year from 1932 through 1984.
a)Find the number NUM of years during which more than
300 automobiles were sold.
b) Print each year and the number of automobiles sold in that year
1. Set NUM : = 0.
2. Repeat for K = 1932 to 1984:
if AUTO[K]> 300, then : set NUM : =
NUM+1
3. Exit.
27
Example 1:
An automobile company uses an array AUTO to record the number
of auto mobile sold each year from 1932 through 1984.
a) Find the number NUM of years during which more than 300
automobiles were sold.
b)Print each year and the number of automobiles sold in
that year
1. Set NUM : = 0.
2. Repeat for K = 1932 to 1984:
if AUTO[K]> 300, then : set NUM : =
NUM+1
3. Exit.
1. Repeat for K = 1932 to
1984:
Write : K, AUTO[K]
2. Exit.
28
Example :
An automobile company uses an array AUTO to record the number
of auto mobile sold each year from 1932 through 1984.
a) Find the number NUM of years during which more than 300
automobiles were sold.
b)Print each year and the number of automobiles sold in
that year
1. Set NUM : = 0.
2. Repeat for K = 1932 to 1984:
if AUTO[K]> 300, then : set NUM : =
NUM+1
3. Exit.
1. Repeat for K = 1932 to
1984:
Write : K, AUTO[K]
2. Exit.
Draw Flowchart for the above algorithms 3
Example :
An automobile company uses an array AUTO to record the number
of auto mobile sold each year from 1932 through 1984.
a) Find the number NUM of years during which more than 300
automobiles were sold.
b)Print each year and the number of automobiles sold in
that year
1. Set NUM : = 0.
2. Repeat for K = 1932 to 1984:
if AUTO[K]> 300, then : set NUM : =
NUM+1
3. Exit. 1.Repeat for K = 1932 to
1984: Write : K, AUTO[K]
2. Exit.
Rewrite the above algorithms using Repeat-while loop
and also draw flow chart. 3
•Inserting refers to the operation of adding another element to the Array
• Inserting an element somewhere in the middle of the array
require that each subsequent element be moved downward to new
locations to accommodate the new element and keep the order of the
other elements. Fig shows E Inserted.
STUDENT
STUDENT
A 1 A
1
B 2 B
2
C 3 E
3
D 4 C
4
5 D
5
6
6
31
Algorithm: 2. INSERTING AN ELEMENT INTO AN ARRAY:
Insert (LA, N, K, ITEM)
32
Algorithm: 2. INSERTING AN ELEMENT INTO AN ARRAY:
Insert (LA, N, K, ITEM)
Paragraph:
Here LA is linear array with N elements and K is a positive
integer such that K<=N. This algorithm inserts an element ITEM
into the Kth position in LA.
33
Algorithm: 2. INSERTING AN ELEMENT INTO AN ARRAY:
Insert (LA, N, K, ITEM)
Paragraph:
Here LA is linear array with N elements and K is a positive
integer such that K<=N. This algorithm inserts an element ITEM
into the Kth position in LA.
ALGORITHM
Step 1. [Initialize counter] Set J:=N
Step 2. Repeat Steps 3 and 4] while J>=K
Step 3. [Move Jth element downward] Set LA [J+1]: =LA [J]
Step 4. [Decrease counter] Set J:=J-1
[End of step 2 loop]
Step 5 [Insert element] Set LA [K]: =ITEM
Step 6. [Reset N] Set N:=N+1
Step 7. Exit
34
Algorithm: 2. INSERTING AN ELEMENT INTO AN ARRAY:
Insert (LA, N, K, ITEM)
Paragraph:
Here LA is linear array with N elements and K is a positive
integer such that K<=N. This algorithm inserts an element ITEM
into the Kth position in LA.
ALGORITHM
Step 1. [Initialize counter] Set J:=N
Step 2. Repeat Steps 3 and 4] while J>=K
Step 3. [Move Jth element downward] Set LA [J+1]: =LA [J]
Step 4. [Decrease counter] Set J:=J-1
[End of step 2 loop]
Step 5 [Insert element] Set LA [K]: =ITEM
Step 6. [Reset N] Set N:=N+1
Step 7. Exit
Draw flowchart for above algorithm
35
Algorithm: 4.3. DELETING AN ELEMENT FROM A LINEAR ARRAY
Delete (LA, N, K, ITEM)
Paragraph:
Here LA is linear array with N elements and K is a positive integer
such that K<=N. This algorithm deletes an element ITEM from the
Kth position in LA.
36
•Deleting refers to the operation of removing one element from the Array
• Deleting an element somewhere from the middle of the array
require that each subsequent element be moved one location upward in
order to “fill up” the array. Fig shows B deleted.
STUDENT
STUDENT
A
1 A
B
1
2
C 2
3 C
D 3
4 D
4
5
5
6
6
37
Algorithm: 3. DELETING AN ELEMENT FROM A LINEAR ARRAY
Delete (LA, N, K, ITEM)
Paragraph:
Here LA is linear array with N elements and K is a positive integer
such that K<=N. This algorithm deletes an element ITEM from the
Kth position in LA.
ALGORITHM
Step 1. Set ITEM: = LA [K]
Step 2. Repeat for J=K to N-1
[Move J+1st element upward] Set LA [J]: =LA [J+1]
[End of loop]
Step 3 [Reset the number N of elements in LA] Set N:=N-
1 Step 4. Exit
38
Algorithm: 3. DELETING AN ELEMENT FROM A LINEAR ARRAY
Delete (LA, N, K, ITEM)
Paragraph:
Here LA is linear array with N elements and K is a positive integer
such that K<=N. This algorithm deletes an element ITEM from the
Kth position in LA.
ALGORITHM
Step 1. Set ITEM: = LA [K]
Step 2. Repeat for J=K to N-1
[Move J+1st element upward] Set LA [J]: =LA [J+1]
[End of loop]
Step 3 [Reset the number N of elements in LA] Set N:=N-
1 Step 4. Exit
Selection Sort
Bubble Sort
Insertion Sort
Merge Sort
Quick Sort
Topological Sort
40
⦿ There are a variety of situations that we can
encounter
◾ Do we have randomly ordered keys?
◾ Are all keys distinct?
◾ How large is the set of keys to be ordered?
◾ Need guaranteed performance?
4
•Many applications require that data be stored in more
than one dimension.
• One common example is a table, which is an array
that
consists of rows and columns.
•Two dimensional arrays are declared similarly to one
dimensional arrays:
The first subscript gives the row number, and the second
subscript specifies column number.
42
FIGURE: Two-dimensional Array FIGURE : Array Of Arrays
43
44
45
46
47
•Multidimensional arrays can have three, four, or more
dimensions.
•The first dimension is called a plane, which consists of rows and
columns.
•The C language considers the three-dimensional array to be an
array of two-dimensional arrays.
FIGURE A Three-dimensional
Array (3 x 5 x 4)
48
FIGURE C View of Three-dimensional Array
49