Lecture 2 ADTs
-
Upload
uploadingperson -
Category
Documents
-
view
226 -
download
0
Transcript of Lecture 2 ADTs
-
8/10/2019 Lecture 2 ADTs
1/31
ABSTRACT DATA TYPES (ADTS)
COMP1927 Computing 2 15x1
Sedgewick Chapter 4
-
8/10/2019 Lecture 2 ADTs
2/31
ABSTRACTION
To understand a system, it should be enough to
understand what its components do without knowing how
Watching a television
We operate the tv through its interface remote control and buttons.
We do not need to open the tv up and see inside to use it.
When designing a new library, it is important to understand what are the abstract properties of the data types we want
provide?
which operations do we need to create (destroy), query, and
manipulate objects of these types?
Do we need to or want to know how FILE * is implemented? Or
just HOW to use it?
Abstraction is the idea of hiding complexity, breaking larger functions into smaller functions
Main function should be quite small
As we get to bigger functions, we break them down further, into abstract data TYPES
analogy
-
8/10/2019 Lecture 2 ADTs
3/31
ABSTRACT DATA TYPES
A data type is ...
a set of values (atomic or structured values) a collection of operations on those values
An abstract data type is ...
an approach to implementing data types
separates interface from implementation builders of the ADT provide an implementation
Users/clients of the ADT see only the interface
A client can not see the implementation through theinterface
They do not know if you used an array, a linked list etc oranything else.
This allows the implementation to change without breakingclient code.
Facilitates decomposing problems into smaller parts
linked list and array are data types
client can see only
necessary functions, not
all implementation details
they only know that it works properly, if you push/pop it works
-
8/10/2019 Lecture 2 ADTs
4/31
-
8/10/2019 Lecture 2 ADTs
5/31
PUSHDOWN STACK OR LAST-IN, FIRST-OUT
(LIFO) QUEUE
Two basic operations to manipulate a stack Insert (push) a new item
Remove (pop) the most recently inserted item
An operation to create a stack
Create an empty stack
An operation to query the state of the stack
Check if stack is empty
Applications
Stacks are useful for many applications
backtracking search, function call stacks, evaluation of expressions
analogy: washing plates and stacking them in a rack
at the top
from the top
in the .h file, youll hae prototypes for pushing/popping
urt er
in computer science
such as
functions get put on a stack
the system uses a stack to keep track of where we're up to
-
8/10/2019 Lecture 2 ADTs
6/31
STACKADT IMPLEMENTATION 1: USING
ARRAYS
Array as stack
fill items into s[0], s[1],....
maintain a counter of the number of pushed items
pre-allocate array given maximum number of elements
Push a, push b, push c, pop, push d would result in:
a b d
DATA STRUCTURE WE KNOW: arrays and linked lists
we need: an array and an index/counter to keep track where we're up to (which is an inner implementation - user knows nothing)
pop - pop rom in ex since t e in ex is eeping trac o w ere we re up to
then decrement our index
the number of elements in the array is independent to the cost of pushing or popping elements (we go
straight to the particular index).
-
8/10/2019 Lecture 2 ADTs
7/31
STACKADT IMPLEMENTATION 2: USING LISTS
push (a)
a
List as stack
add node to front of the list when pushing
take node from front of the list when popping
-
8/10/2019 Lecture 2 ADTs
8/31
STACKADT IMPLEMENTATION 2: USING LISTS
push (a)
push (b)
ab
List as stack
add node to front of the list when pushing
take node from front of the list when popping
-
8/10/2019 Lecture 2 ADTs
9/31
STACKADT IMPLEMENTATION 2: USING LISTS
push (a)
push (b)
push (c)c ab
List as stack
add node to front of the list when pushing
take node from front of the list when popping
-
8/10/2019 Lecture 2 ADTs
10/31
STACKADT IMPLEMENTATION 2: USING LISTS
push (a)
push (b)
pop()ab
List as stack
add node to front of the list when pushing
take node from front of the list when popping
-
8/10/2019 Lecture 2 ADTs
11/31
STACKADT IMPLEMENTATION 2: USING LISTS
push (a)
push (b)
push (c)
pop()
push (d)
d ab
List as stack
add node to front of the list when pushing
take node from front of the list when popping
-
8/10/2019 Lecture 2 ADTs
12/31
EXAMPLE: BALANCING BRACKETS
Example of stack ADT use on sample input:
( [ { } ] )
Next char Stack Check
(start) (empty) -( ( -
[ ( [ -
{ ( [ { -
} ( [ { vs }
] ( [ vs ]
) (empty) ( vs )
(eof) (empty) -
-
8/10/2019 Lecture 2 ADTs
13/31
INFIX, PREFIX AND POSTFIX EXPRESSIONS
Infix
2 + 3
Prefix
+ 2 3
Postfix
2 3 +
-
8/10/2019 Lecture 2 ADTs
14/31
STACK ADT CLIENT EXERCISE: POSTFIX
EXPRESSION EVALUATION
Task: Given an expression in postfix notation, return its value:
% ./eval_postfix 5 9 8 + 4 6 * * 7 + *
2075
How can we evaluate a postfix expression?
We use a stack
When we encounter a number, push it
When we encounter an operator, pop the two topmost numbers, applythe operator to those numbers, and push the result on the stack
-
8/10/2019 Lecture 2 ADTs
15/31
#include
intmain(intargc,char*argv[]){
printf("argc is %d\n",argc);
inti;
for(i=0;i
-
8/10/2019 Lecture 2 ADTs
16/31
#include
#include
#include "Item.h"#include "Stack.h"
intmain(intargc,char*argv[]){
char*exp;//pointer to current character in input string
inti;
if(argc
-
8/10/2019 Lecture 2 ADTs
17/31
FIRST-IN, FIRST-OUT (FIFO) QUEUE
Two basic operations to manipulate the queue
insert (put) new item
delete (get) the least recently inserted item
An operation to create a queue
Create an empty queue
An operation to query the state of the queue
Check if queue is empty
-
8/10/2019 Lecture 2 ADTs
18/31
QUEUEADT IMPLEMENTATION1: USING LISTS
List as queue
add node to end of the list when pushing
take node from front of the list when removing
put(a)
a b a
-
8/10/2019 Lecture 2 ADTs
19/31
QUEUEADT IMPLEMENTATION1: USING LISTS
List as queue
add node to end of the list when pushing
take node from front of the list when removing
put(a)
put (b)
a b a b
-
8/10/2019 Lecture 2 ADTs
20/31
QUEUEADT IMPLEMENTATION1: USING LISTS
List as queue
add node to end of the list when pushing
take node from front of the list when removing
put(a)
put (b)
put (c)a b a b c
-
8/10/2019 Lecture 2 ADTs
21/31
QUEUEADT IMPLEMENTATION1: USING LISTS
List as queue
add node to end of the list when pushing
take node from front of the list when removing
put(a)
put (b)
put (c)
get()
put (d)
get ()
a b b c
-
8/10/2019 Lecture 2 ADTs
22/31
QUEUEADT IMPLEMENTATION1: USING LISTS
List as queue
add node to end of the list when pushing
take node from front of the list when removing
put(a)
put (b)
put (c)
get()
put (d)
a b b c d
-
8/10/2019 Lecture 2 ADTs
23/31
QUEUEADT IMPLEMENTATION 2: USING
ARRAYS
Array as queue
fill items into s[0], s[1],....
maintain a counter for beginning and end of queue
pre-allocate array given maximum number of elements
roll over when reaching end of array
a
put(a)
-
8/10/2019 Lecture 2 ADTs
24/31
QUEUEADT IMPLEMENTATION 2: USING
ARRAYS
Array as queue
fill items into s[0], s[1],....
maintain a counter for beginning and end of queue
pre-allocate array given maximum number of elements
roll over when reaching end of array
a b
put(a)
put (b)
-
8/10/2019 Lecture 2 ADTs
25/31
QUEUEADT IMPLEMENTATION 2: USING
ARRAYS
Array as queue
fill items into s[0], s[1],....
maintain a counter for beginning and end of queue
pre-allocate array given maximum number of elements
roll over when reaching end of array
a b c
put(a)
put (b)
put(c)
-
8/10/2019 Lecture 2 ADTs
26/31
QUEUEADT IMPLEMENTATION 2: USING
ARRAYS
Array as queue
fill items into s[0], s[1],....
maintain a counter for beginning and end of queue
pre-allocate array given maximum number of elements
roll over when reaching end of array
b c
put(a)
put (b)
put(c)
get()
-
8/10/2019 Lecture 2 ADTs
27/31
QUEUEADT IMPLEMENTATION 2: USING
ARRAYS
Array as queue
fill items into s[0], s[1],....
maintain a counter for beginning and end of queue
pre-allocate array given maximum number of elements
roll over when reaching end of array
b c
put(a)
put (b)
put(c)
get()
put(d)
d
-
8/10/2019 Lecture 2 ADTs
28/31
TESTING
Testing cannot establish that a program is correct
would need to show for all possible inputs it produces the
correct output
This is impossible except in trivial cases
We can only choose this subset well!
Different types of parameters require different types oftesting
numeric: check the value, +ve, -ve, 0, large values, boundary
cases etc.
string: check the length, empty, 1 element, many elements properties like increasing order, decreasing order, random order
-
8/10/2019 Lecture 2 ADTs
29/31
TEST CASES EXERCISE
Finding the maximum value in an un-ordered array?
-
8/10/2019 Lecture 2 ADTs
30/31
BLACK BOX VS WHITE BOX TESTING
Black Box Testing: Testing code from the outside:
Checks behaviour
Does tested input result in the correct output ?
Program does not know about the underlying implementation
If the implementation changes the tests should still pass
White Box Testing:
Testing code from the inside:
Checks code structure
Tests internal functions
Tests rely on and can access the implementation
-
8/10/2019 Lecture 2 ADTs
31/31
ASSERT BASED TESTING
How to use assert: use while developing, testing and debugging a program to make
sure pre- and postconditions are valid
not in production code!
it aborts the program, error message useful to the programmer,
but not to the user of the application
Use exception handlers in production code to terminate
gracefully with a sensible error message (if necessary)