Title: Math Review - Proof By Induction
1Abstract Data Types
- An ADT is a set of operations upon a set of data.
- Implementation details are not specified in an
ADT. - The program designer determines the operations
that are needed and the specific data that will
be used in the implementation. - The implementation of the ADT should be easy to
modify, and such modifications should be
transparent to any code deploying the ADT.
2ADT 1 The List
- A list is a finite ordered collection of items of
the same type. - Common list operations include
- Emptying the entire list
- Determining whether the list is empty
- Determining the size of the list
- Determining the location of a particular list
element - Determining the value of an element at a
particular location in the list - Inserting a new list element at a particular
location - Removing a particular element from the list
- Outputting the entire list
3List Implementation Option An Array
- An Array Implementations Performance
n
- Problems with the array implementation
- Data movement really slows down insertions to and
removals from the list - The maximum list size must be specified
4List Implementation Option A Linked List
- A Linked List Implementations Performance
- Problems with the linked list implementation
- Pointers consume memory not needed with arrays
- Lack of indexing necessitates repeated list
traversals (e.g., binary search is impossible)
5Performance - Compression and Caching Effects
- Compression
- Compress bitmaps for optimal space (core memory
and disk storage) - Perform bitmap operations on compressed bitmaps.
- Caching
- Locality of Reference principle
- Keep record of location of recent bits referenced
- Cache area is relatively small
- Each bitmap has its own cache area
6Effect of cache on loading bitmaps
7Effect of cache on OR bitmap operations
8Effect of cache on AND bitmap operations
9Linked List Implementation in Visual C
ifndef LIST_H include ltstdlib.hgt template
ltclass Etypegt class list protected
struct node Etype element
node next node(Etype e 0, node
n NULL) element(e), next(n)
node head node current void
deleteList() public list() head(new
node), current(head) virtual list()
deleteList() const list operator
(list value) const list operator ()
boolean operator ! () const const
Etype operator () () const boolean
isEmpty() const return (head-gtnext NULL)
virtual boolean find(const Etype x)
virtual boolean findPrevious(const Etype x)
void first() if (head-gtnext ! NULL) current
head-gtnext void header() current
head boolean remove(const Etype x)
virtual void insert(const Etype x)
virtual void insertAsFirstElement(const Etype
x)
Class Template
Structure
Structure Constructor
Virtual Function Derived classes may have their
own version of this function
Class Constructor
Class Destructor
Constant Modifier This operator accesses but
doesnt modify
Constant Return Value returned is treated as a
constant
In-Line Code
10// Member function to free all memory associated
with the linked list. template ltclass Etypegt void
listltEtypegt deleteList() node p
head-gtnext node temp while (p ! NULL)
temp p-gtnext delete p p
temp delete head // Assignment
operator duplicates parameterized linked
list. template ltclass Etypegt inline const
listltEtypegt listltEtypegt operator (list
value) if (this value) return
this deleteList() current head new
node for (value.first() !value value)
current-gtnext new node(value(),
current-gtnext) current current-gtnext
current-gtnext NULL first()
value.first() return this
In-Line Function Prompts the compiler to
generate code inline instead of laying it down
once and calling it through the usual mechanisms
11// Increment operator moves current pointer to
next node (if possible). template ltclass Etypegt
inline const listltEtypegt listltEtypegt operator
() if (current ! NULL) current
current-gtnext return this // Logical
not operator indicates whether current pointer
is non-NULL. template ltclass Etypegt inline
boolean listltEtypegt operator ! () const
return (current ! NULL) // Parenthetical
operator returns value of current node (or head
node if current is NULL). template ltclass Etypegt
inline const Etype listltEtypegt operator () ()
const if (current ! NULL) return
current-gtelement else return
head-gtelement
12// Member function to determine whether
parameterized element is in list. template ltclass
Etypegt boolean listltEtypegt find(const Etype
x) node p for (p head-gtnext p !
NULL p p-gtnext) if (p-gtelement
x) current p return
true return false // Member
function to locate predecessor of parameterized
value in list. template ltclass Etypegt boolean
listltEtypegt findPrevious(const Etype
x) node p for (p head p-gtnext ! NULL
p p-gtnext) if (p-gtnext-gtelement x)
current p return true
return false
13// Member function to remove first occurrence of
parameterized value from list. template ltclass
Etypegt boolean listltEtypegt remove(const Etype
x) node cellToDelete if (findPrevious(x))
cellToDelete current-gtnext
current-gtnext cellToDelete-gtnext delete
cellToDelete return true return
false // Member function to insert
parameterized value after current node in
list. template ltclass Etypegt void listltEtypegt
insert(const Etype x) node p new node(x,
current-gtnext) if (p ! NULL)
current-gtnext p current
current-gtnext // Member function to insert
parameterized value as new head element in
list. template ltclass Etypegt void listltEtypegt
insertAsFirstElement(const Etype
x) header() insert(x) define
LIST_H endif
14A Test Driver For The Linked List Implementation
include "list.h" include ltiostream.hgt void
printList(listltintgt lst) // The main function
generates a couple of integer lists // tp test
the functionality of the linked list class. void
main() listltintgt lst1, lst2 cout ltlt
"(This should be empty)" ltlt endl
printList(lst1) for (int i 1 i lt 5 i)
lst1.insertAsFirstElement(i) cout ltlt
"(This should be 5 4 3 2 1)" ltlt endl
printList(lst1) for (i 4 i lt 6 i)
if (lst1.find(i)) cout ltlt "Found " ltlt
lst1() ltlt endl else cout ltlt i ltlt
" not found" ltlt endl lst2 lst1 cout ltlt
"(This should be 5 4 3 2 1)" ltlt endl
printList(lst2) lst2.remove(3) cout ltlt
"(This should be 5 4 2 1)" ltlt endl
printList(lst2) cout ltlt "(but this should
still be 5 4 3 2 1)" ltlt endl
printList(lst1)
15// The printList function outputs the contents //
of the linked list, starting at the head. void
printList(listltintgt lst) if
(lst.isEmpty()) cout ltlt "Empty list." ltlt
endl else for (lst.first() !lst
lst) cout ltlt lst() ltlt endl
16Inheritance The sortedList Class
If Etype values can be sorted, then the list
class can be modified to accommodate this
sorting. This can be easily accomplished by
deriving a subclass of list, and overriding the
two insertion functions insert and
insertAsFirstElement.
include "list.h" template ltclass Etypegt class
sortedList public listltEtypegt public
virtual void insert(const Etype x)
virtual void insertAsFirstElement(const Etype x)
// This member function inserts the
parameterized // value and preserves the sorted
nature of the list. template ltclass Etypegt void
sortedListltEtypegt insert(const Etype x)
for (node p head p-gtnext ! NULL p
p-gtnext) if (p-gtnext-gtelement gt x)
break current p
listltEtypegtinsert(x)
17Example Linked List Application Polynomials
Let Etype be a two-field structure containing the
coefficient and exponent of each monomial within
the polynomial. Sort the nodes comprising each
polynomial based upon the values of their
exponents.
- The operations for this ADT could include
- Addition, subtraction, and multiplication of
polynomials - Derivatives of polynomials
- Evaluation of polynomials (i.e., plugging in
values)