This document discusses data structures and abstract data types (ADTs). It begins by defining a data structure as a collection of data elements along with relationships between them. An ADT is defined as a specification of values and operations on those values, independent of any programming language. Linear lists are provided as an example ADT, with definitions, properties, and common operations like get and indexOf discussed. The document aims to introduce basic concepts around data structures and ADTs.
4. Images/cinvestav-
What is a data structure?
Intuition
A data object is a set or collection of instances!!!
Examples
integer = {0, +1, -1, +2, -2, +3, -3, ...}
daysOfWeek = {S,M,T,W,Th,F,Sa}
4 / 30
5. Images/cinvestav-
What is a data structure?
Intuition
A data object is a set or collection of instances!!!
Examples
integer = {0, +1, -1, +2, -2, +3, -3, ...}
daysOfWeek = {S,M,T,W,Th,F,Sa}
4 / 30
6. Images/cinvestav-
What is a data structure?
Intuition
A data object is a set or collection of instances!!!
Examples
integer = {0, +1, -1, +2, -2, +3, -3, ...}
daysOfWeek = {S,M,T,W,Th,F,Sa}
4 / 30
7. Images/cinvestav-
Those instance may be related!!!
Something quite basic
Instances may or may not be related.
Example
myDataObject = {apple, chair, 2, 5.2, red, green, Jack}
Thus
Data Structure ā Data object + relationships that exist among instances
and elements that comprise an instance.
5 / 30
8. Images/cinvestav-
Those instance may be related!!!
Something quite basic
Instances may or may not be related.
Example
myDataObject = {apple, chair, 2, 5.2, red, green, Jack}
Thus
Data Structure ā Data object + relationships that exist among instances
and elements that comprise an instance.
5 / 30
9. Images/cinvestav-
Those instance may be related!!!
Something quite basic
Instances may or may not be related.
Example
myDataObject = {apple, chair, 2, 5.2, red, green, Jack}
Thus
Data Structure ā Data object + relationships that exist among instances
and elements that comprise an instance.
5 / 30
10. Images/cinvestav-
Examples
Among instances of integers
369 < 370
280 + 4 = 284
Thus
The relationships are usually speciļ¬ed by specifying operations on one or
more instances.
Examples
add, subtract, predecessor, multiply
6 / 30
11. Images/cinvestav-
Examples
Among instances of integers
369 < 370
280 + 4 = 284
Thus
The relationships are usually speciļ¬ed by specifying operations on one or
more instances.
Examples
add, subtract, predecessor, multiply
6 / 30
12. Images/cinvestav-
Examples
Among instances of integers
369 < 370
280 + 4 = 284
Thus
The relationships are usually speciļ¬ed by specifying operations on one or
more instances.
Examples
add, subtract, predecessor, multiply
6 / 30
14. Images/cinvestav-
Abstract Data Type
Data Type
A data type such as int or double is a group of values and operations on
those values that is deļ¬ned within a speciļ¬c programming language.
Abstract Data Type
An Abstract Data Type, or ADT, is a speciļ¬cation for a group of values
and the operations on those values that is deļ¬ned conceptually and
independently of any programming language.
Thus
A data structure is an implementation of an ADT within a programming
language.
8 / 30
15. Images/cinvestav-
Abstract Data Type
Data Type
A data type such as int or double is a group of values and operations on
those values that is deļ¬ned within a speciļ¬c programming language.
Abstract Data Type
An Abstract Data Type, or ADT, is a speciļ¬cation for a group of values
and the operations on those values that is deļ¬ned conceptually and
independently of any programming language.
Thus
A data structure is an implementation of an ADT within a programming
language.
8 / 30
16. Images/cinvestav-
Abstract Data Type
Data Type
A data type such as int or double is a group of values and operations on
those values that is deļ¬ned within a speciļ¬c programming language.
Abstract Data Type
An Abstract Data Type, or ADT, is a speciļ¬cation for a group of values
and the operations on those values that is deļ¬ned conceptually and
independently of any programming language.
Thus
A data structure is an implementation of an ADT within a programming
language.
8 / 30
17. Images/cinvestav-
Using Abstract Data Types
Something Notable
An abstract data type is deļ¬ned indirectly, only by the operations that may
be performed on it and by mathematical constraints on the eļ¬ects (and
possibly cost) of those operations.
Using Abstract Data Types
1 An abstract object may be operated upon by the operations which
deļ¬ne its abstract type.
2 An abstract object may be passed as a parameter to a procedure.
3 An abstract object may be assigned to a variable.
9 / 30
18. Images/cinvestav-
Using Abstract Data Types
Something Notable
An abstract data type is deļ¬ned indirectly, only by the operations that may
be performed on it and by mathematical constraints on the eļ¬ects (and
possibly cost) of those operations.
Using Abstract Data Types
1 An abstract object may be operated upon by the operations which
deļ¬ne its abstract type.
2 An abstract object may be passed as a parameter to a procedure.
3 An abstract object may be assigned to a variable.
9 / 30
19. Images/cinvestav-
Using Abstract Data Types
Something Notable
An abstract data type is deļ¬ned indirectly, only by the operations that may
be performed on it and by mathematical constraints on the eļ¬ects (and
possibly cost) of those operations.
Using Abstract Data Types
1 An abstract object may be operated upon by the operations which
deļ¬ne its abstract type.
2 An abstract object may be passed as a parameter to a procedure.
3 An abstract object may be assigned to a variable.
9 / 30
20. Images/cinvestav-
Using Abstract Data Types
Something Notable
An abstract data type is deļ¬ned indirectly, only by the operations that may
be performed on it and by mathematical constraints on the eļ¬ects (and
possibly cost) of those operations.
Using Abstract Data Types
1 An abstract object may be operated upon by the operations which
deļ¬ne its abstract type.
2 An abstract object may be passed as a parameter to a procedure.
3 An abstract object may be assigned to a variable.
9 / 30
21. Images/cinvestav-
Using Abstract Data Types
Something Notable
An abstract data type is deļ¬ned indirectly, only by the operations that may
be performed on it and by mathematical constraints on the eļ¬ects (and
possibly cost) of those operations.
Using Abstract Data Types
1 An abstract object may be operated upon by the operations which
deļ¬ne its abstract type.
2 An abstract object may be passed as a parameter to a procedure.
3 An abstract object may be assigned to a variable.
9 / 30
22. Images/cinvestav-
For More
We have the following
āPROGRAMMING WITH ABSTRACT DATA TYPESā by Barbara Liskov
and Stephen Zilles
10 / 30
23. Images/cinvestav-
Abstract Data Types: An Example
We will start with
An example using Lists.
Actually in Java
An interface in the Java programming language is an abstract type that is
used to specify an interface (in the generic sense of the term) that classes
must implement.
11 / 30
24. Images/cinvestav-
Abstract Data Types: An Example
We will start with
An example using Lists.
Actually in Java
An interface in the Java programming language is an abstract type that is
used to specify an interface (in the generic sense of the term) that classes
must implement.
11 / 30
26. Images/cinvestav-
Linear (or Ordered) Lists
Deļ¬nition
A linear list is a data structure that holds a sequential list of elements.
Instances
They have the following structure, (e0, e1, e1, ..., enā1)
Properties
Where ei denotes a list element
n ā„ 0 is ļ¬nite
List size is n
13 / 30
27. Images/cinvestav-
Linear (or Ordered) Lists
Deļ¬nition
A linear list is a data structure that holds a sequential list of elements.
Instances
They have the following structure, (e0, e1, e1, ..., enā1)
Properties
Where ei denotes a list element
n ā„ 0 is ļ¬nite
List size is n
13 / 30
28. Images/cinvestav-
Linear (or Ordered) Lists
Deļ¬nition
A linear list is a data structure that holds a sequential list of elements.
Instances
They have the following structure, (e0, e1, e1, ..., enā1)
Properties
Where ei denotes a list element
n ā„ 0 is ļ¬nite
List size is n
13 / 30
29. Images/cinvestav-
Linear (or Ordered) Lists
Deļ¬nition
A linear list is a data structure that holds a sequential list of elements.
Instances
They have the following structure, (e0, e1, e1, ..., enā1)
Properties
Where ei denotes a list element
n ā„ 0 is ļ¬nite
List size is n
13 / 30
30. Images/cinvestav-
What are the relations inside of a Liner List?
Having this
L = (e0, e1, e2, ..., enā1)
Relationships
e0 is the zeroāth (or front) element
enā1 is the last element
ei immediately precedes ei+1
14 / 30
31. Images/cinvestav-
What are the relations inside of a Liner List?
Having this
L = (e0, e1, e2, ..., enā1)
Relationships
e0 is the zeroāth (or front) element
enā1 is the last element
ei immediately precedes ei+1
14 / 30
32. Images/cinvestav-
What are the relations inside of a Liner List?
Having this
L = (e0, e1, e2, ..., enā1)
Relationships
e0 is the zeroāth (or front) element
enā1 is the last element
ei immediately precedes ei+1
14 / 30
33. Images/cinvestav-
What are the relations inside of a Liner List?
Having this
L = (e0, e1, e2, ..., enā1)
Relationships
e0 is the zeroāth (or front) element
enā1 is the last element
ei immediately precedes ei+1
14 / 30
34. Images/cinvestav-
Examples
Simple ones
Students in COP3530 = (Jack, Jill, Abe, Henry, Mary, ..., Judy)
Exams in COP3530 = (exam1, exam2, exam3)
Days of Week = (S, M, T, W, Th, F, Sa)
Months = (Jan, Feb, Mar, Apr, ..., Nov, Dec)
15 / 30
35. Images/cinvestav-
Examples
Simple ones
Students in COP3530 = (Jack, Jill, Abe, Henry, Mary, ..., Judy)
Exams in COP3530 = (exam1, exam2, exam3)
Days of Week = (S, M, T, W, Th, F, Sa)
Months = (Jan, Feb, Mar, Apr, ..., Nov, Dec)
15 / 30
36. Images/cinvestav-
Examples
Simple ones
Students in COP3530 = (Jack, Jill, Abe, Henry, Mary, ..., Judy)
Exams in COP3530 = (exam1, exam2, exam3)
Days of Week = (S, M, T, W, Th, F, Sa)
Months = (Jan, Feb, Mar, Apr, ..., Nov, Dec)
15 / 30
37. Images/cinvestav-
Examples
Simple ones
Students in COP3530 = (Jack, Jill, Abe, Henry, Mary, ..., Judy)
Exams in COP3530 = (exam1, exam2, exam3)
Days of Week = (S, M, T, W, Th, F, Sa)
Months = (Jan, Feb, Mar, Apr, ..., Nov, Dec)
15 / 30
39. Images/cinvestav-
Which operations must be supported?
We need the size of a linear list
Hey, we need to know how many elements the linear list has.
Determine list size for
L = (a, b, c, d, e)
So, we need to have a operation that returns the size
size(L)=5
17 / 30
40. Images/cinvestav-
Which operations must be supported?
We need the size of a linear list
Hey, we need to know how many elements the linear list has.
Determine list size for
L = (a, b, c, d, e)
So, we need to have a operation that returns the size
size(L)=5
17 / 30
41. Images/cinvestav-
Which operations must be supported?
We need the size of a linear list
Hey, we need to know how many elements the linear list has.
Determine list size for
L = (a, b, c, d, e)
So, we need to have a operation that returns the size
size(L)=5
17 / 30
42. Images/cinvestav-
Linear List Operations: get(theIndex)
Get operations
Get element with given index.
For example
L = (a, b, c, d, e)
Thus
get(0) = a
get(2) = c
get(4) = e
get(-1) = error
get(9) = error
18 / 30
43. Images/cinvestav-
Linear List Operations: get(theIndex)
Get operations
Get element with given index.
For example
L = (a, b, c, d, e)
Thus
get(0) = a
get(2) = c
get(4) = e
get(-1) = error
get(9) = error
18 / 30
44. Images/cinvestav-
Linear List Operations: get(theIndex)
Get operations
Get element with given index.
For example
L = (a, b, c, d, e)
Thus
get(0) = a
get(2) = c
get(4) = e
get(-1) = error
get(9) = error
18 / 30
45. Images/cinvestav-
Linear List Operations: get(theIndex)
Get operations
Get element with given index.
For example
L = (a, b, c, d, e)
Thus
get(0) = a
get(2) = c
get(4) = e
get(-1) = error
get(9) = error
18 / 30
46. Images/cinvestav-
Linear List Operations: get(theIndex)
Get operations
Get element with given index.
For example
L = (a, b, c, d, e)
Thus
get(0) = a
get(2) = c
get(4) = e
get(-1) = error
get(9) = error
18 / 30
47. Images/cinvestav-
Linear List Operations: get(theIndex)
Get operations
Get element with given index.
For example
L = (a, b, c, d, e)
Thus
get(0) = a
get(2) = c
get(4) = e
get(-1) = error
get(9) = error
18 / 30
48. Images/cinvestav-
Linear List Operations: get(theIndex)
Get operations
Get element with given index.
For example
L = (a, b, c, d, e)
Thus
get(0) = a
get(2) = c
get(4) = e
get(-1) = error
get(9) = error
18 / 30
49. Images/cinvestav-
Linear List Operations: indexOf(theElement)
IndexOf operations
Determine the index of an element.
Example
L = (a, b, d, b, a)
Thus
indexOf(d) = 2
indexOf(a) = 0
indexOf(z) = -1
19 / 30
50. Images/cinvestav-
Linear List Operations: indexOf(theElement)
IndexOf operations
Determine the index of an element.
Example
L = (a, b, d, b, a)
Thus
indexOf(d) = 2
indexOf(a) = 0
indexOf(z) = -1
19 / 30
51. Images/cinvestav-
Linear List Operations: indexOf(theElement)
IndexOf operations
Determine the index of an element.
Example
L = (a, b, d, b, a)
Thus
indexOf(d) = 2
indexOf(a) = 0
indexOf(z) = -1
19 / 30
52. Images/cinvestav-
Linear List Operations: indexOf(theElement)
IndexOf operations
Determine the index of an element.
Example
L = (a, b, d, b, a)
Thus
indexOf(d) = 2
indexOf(a) = 0
indexOf(z) = -1
19 / 30
53. Images/cinvestav-
Linear List Operations: indexOf(theElement)
IndexOf operations
Determine the index of an element.
Example
L = (a, b, d, b, a)
Thus
indexOf(d) = 2
indexOf(a) = 0
indexOf(z) = -1
19 / 30
54. Images/cinvestav-
Linear List Operations: add(theIndex, theElement)
Deļ¬nition
Add an element so that the new element has a speciļ¬ed index.
If we have
L = (a, b, c, d, e, f , g)
Thus
add(0,h) =ā L = (h, a, b, c, d, e, f , g)
Thus, index of a, b, c, d, e, f , g increases by 1.
20 / 30
55. Images/cinvestav-
Linear List Operations: add(theIndex, theElement)
Deļ¬nition
Add an element so that the new element has a speciļ¬ed index.
If we have
L = (a, b, c, d, e, f , g)
Thus
add(0,h) =ā L = (h, a, b, c, d, e, f , g)
Thus, index of a, b, c, d, e, f , g increases by 1.
20 / 30
56. Images/cinvestav-
Linear List Operations: add(theIndex, theElement)
Deļ¬nition
Add an element so that the new element has a speciļ¬ed index.
If we have
L = (a, b, c, d, e, f , g)
Thus
add(0,h) =ā L = (h, a, b, c, d, e, f , g)
Thus, index of a, b, c, d, e, f , g increases by 1.
20 / 30
57. Images/cinvestav-
Example
add(2,h) using the original L
=ā L = (a, b, h, c, d, e, f , g)
index of c, d, e, f , g increases by 1
What about out of the bound
add(10,h) =ā error
add(-6,h) =ā error
21 / 30
58. Images/cinvestav-
Example
add(2,h) using the original L
=ā L = (a, b, h, c, d, e, f , g)
index of c, d, e, f , g increases by 1
What about out of the bound
add(10,h) =ā error
add(-6,h) =ā error
21 / 30
59. Images/cinvestav-
Example
add(2,h) using the original L
=ā L = (a, b, h, c, d, e, f , g)
index of c, d, e, f , g increases by 1
What about out of the bound
add(10,h) =ā error
add(-6,h) =ā error
21 / 30
60. Images/cinvestav-
Linear List Operations: remove(theIndex)
Deļ¬nition
Remove and return element with given index.
For example
L = (a, b, c, d, e, f , g)
Example
Remove(2) returns c and L becomes (a, b, d, e, f , g)
Index of d, e, f , g decreases by 1
22 / 30
61. Images/cinvestav-
Linear List Operations: remove(theIndex)
Deļ¬nition
Remove and return element with given index.
For example
L = (a, b, c, d, e, f , g)
Example
Remove(2) returns c and L becomes (a, b, d, e, f , g)
Index of d, e, f , g decreases by 1
22 / 30
62. Images/cinvestav-
Linear List Operations: remove(theIndex)
Deļ¬nition
Remove and return element with given index.
For example
L = (a, b, c, d, e, f , g)
Example
Remove(2) returns c and L becomes (a, b, d, e, f , g)
Index of d, e, f , g decreases by 1
22 / 30
65. Images/cinvestav-
The Final Abstract Data Type for Linear List
Linear List
AbstractDataType LinearList
{
instances
ordered ļ¬nite collections of zero or more elements
operations
isEmpty(): return true iļ¬ the list is empty, false otherwise
size(): return the list size (i.e., number of elements in the list)
get(index): return the element with āindexā index
indexOf(x): return the index of the ļ¬rst occurrence of x in the list, return -1
if x is not in the list
remove(index): remove and return the indexth element, elements with higher
index have their index reduced by 1
add(theIndex, x): insert x as the index of th element, elements with
theIndex ā„ index have their index increased by 1
output(): output the list elements from left to right
}
24 / 30
66. Images/cinvestav-
Java Interface
We can implement this idea of Abstract Data Types
Using Java through the generic interface!!!
An interface may include constants and abstract methods (i.e.,
methods for which no implementation is provided).
Example for List
25 / 30
67. Images/cinvestav-
Java Interface
We can implement this idea of Abstract Data Types
Using Java through the generic interface!!!
An interface may include constants and abstract methods (i.e.,
methods for which no implementation is provided).
Example for List
p u b l i c i n t e r f a c e L i n e a r L i s t <Item>{
p u b l i c boolean isEmpty ( ) ;
p u b l i c i n t s i z e ( ) ;
p u b l i c Item get ( i n t index ) ;
p u b l i c i n t indexOf ( Item myobject ) ;
p u b l i c void add ( i n t index , Item myobject ) ;
p u b l i c Item remove ( i n t index ) ;
p u b l i c S t r i n g t o S t r i n g ( ) ;
}
25 / 30
68. Images/cinvestav-
Implementing the Java Interface
Using a calss to implement the interface
c l a s s SimpleArrayList <Item> implements
L i n e a r L i s t <Item>{
// code f o r a l l methods must be provided
}
26 / 30
70. Images/cinvestav-
Abstract Classes Vs Interfaces
However, we have another way to implement ADT
Abstract Classes
Abstract Classes
An abstract class is a special kind of class that cannot be instantiated.
It is a contract that is used to deļ¬ne hierarchies for all subclasses.
Interface
An interface is not a class.
An interface has no implementation.
It only has the deļ¬nition of the methods without the body.
28 / 30
71. Images/cinvestav-
Abstract Classes Vs Interfaces
However, we have another way to implement ADT
Abstract Classes
Abstract Classes
An abstract class is a special kind of class that cannot be instantiated.
It is a contract that is used to deļ¬ne hierarchies for all subclasses.
Interface
An interface is not a class.
An interface has no implementation.
It only has the deļ¬nition of the methods without the body.
28 / 30
72. Images/cinvestav-
Abstract Classes Vs Interfaces
However, we have another way to implement ADT
Abstract Classes
Abstract Classes
An abstract class is a special kind of class that cannot be instantiated.
It is a contract that is used to deļ¬ne hierarchies for all subclasses.
Interface
An interface is not a class.
An interface has no implementation.
It only has the deļ¬nition of the methods without the body.
28 / 30
73. Images/cinvestav-
Abstract Classes Vs Interfaces
However, we have another way to implement ADT
Abstract Classes
Abstract Classes
An abstract class is a special kind of class that cannot be instantiated.
It is a contract that is used to deļ¬ne hierarchies for all subclasses.
Interface
An interface is not a class.
An interface has no implementation.
It only has the deļ¬nition of the methods without the body.
28 / 30
74. Images/cinvestav-
Abstract Classes Vs Interfaces
However, we have another way to implement ADT
Abstract Classes
Abstract Classes
An abstract class is a special kind of class that cannot be instantiated.
It is a contract that is used to deļ¬ne hierarchies for all subclasses.
Interface
An interface is not a class.
An interface has no implementation.
It only has the deļ¬nition of the methods without the body.
28 / 30
75. Images/cinvestav-
Abstract Classes Vs Interfaces
However, we have another way to implement ADT
Abstract Classes
Abstract Classes
An abstract class is a special kind of class that cannot be instantiated.
It is a contract that is used to deļ¬ne hierarchies for all subclasses.
Interface
An interface is not a class.
An interface has no implementation.
It only has the deļ¬nition of the methods without the body.
28 / 30
76. Images/cinvestav-
Important
We have
A Java class may implement as many interfaces as it wants but can extend
at most one class.
This is why
Once you have chosen your design you can enforce the structure using
Abstract Classes!!!
29 / 30
77. Images/cinvestav-
Important
We have
A Java class may implement as many interfaces as it wants but can extend
at most one class.
This is why
Once you have chosen your design you can enforce the structure using
Abstract Classes!!!
29 / 30
78. Images/cinvestav-
Example of Abstract Class
Code
p u b l i c a b s t r a c t c l a s s L i n e a r L i s t <Item>{
p u b l i c boolean isEmpty ( ) ;
p u b l i c i n t s i z e ( ) ;
p u b l i c Item get ( i n t index ) ;
p u b l i c i n t indexOf ( Item myobject ) ;
p u b l i c void add ( i n t index , Item myobject ) ;
p u b l i c Item remove ( i n t index ) ;
p u b l i c S t r i n g t o S t r i n g ( ) ;
}
30 / 30