Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Lecture Notes 3
Prepared by nan TAHRALI
ROAD MAP
Abstract Data Types (ADT) The List ADT Implementation of Lists Array implementation of lists Linked list implementation of lists Cursor implementation of lists
Definition : Is a set of operation Mathematical abstraction No implementation detail Example : Lists, sets, graphs, stacks are examples of ADT along with their operations
3
Why ADT ?
Modularity
divide program into small functions easy to debug and maintain easy to modify group work do some operations only once transparent to the program
4
Reuse
Ordered sequence of data items called elements A1, A2, A3, ,AN is a list of size N size of an empty list is 0 Ai+1 succeeds Ai Ai-1 preceeds Ai position of Ai is i first element is A1 called head last element is AN called tail
5
Operations ?
Operations
Find (52) 3 Insert (20, 3) 34, 12, 52, 20, 16, 12 Delete (52) 34, 12, 20, 16, 12 FindKth (3) 20
7
Implementation of Lists
Many Implementations
ROAD MAP
Abstract Data Types (ADT) The List ADT Implementation of Lists Array implementation of lists Linked list implementation of lists Cursor implementation of lists
Need to define a size for array High overestimate (waste of space) Operations Running Times
PrintList Find Insert Delete O(N) O(N) (on avarage half needs to be moved)
O(1)
10
Disadvantages :
need to move elements of the list it is difficult to estimate the size of array
11
ROAD MAP
Abstract Data Types (ADT) The List ADT Implementation of Lists Array implementation of lists Linked list implementation of lists Cursor implementation of lists
12
Series of nodes
not adjacent in memory contain the element and a pointer to a node containing its succesor
A1 350
500
A2 500
400
A3 400
666
A4 666
0
13
X 530
A1 350
500
A2 500
530
X 530
400
A3 400
666
A4 666
14
A1 350
500
A2 500
400
A3 400
666
A4 666
A1 350
500
A2 500
666
A4 666
15
No need to move the list for insertion and deletion operations No memory waste
16
Array
O(N)
O(i) O(1)
O(1) O(N)
17
Programming Details
Programming Details
Keep a header node in position 0
header
A1
A2
A3
A4
19
template <class Object> class ListNode { ListNode( const Object & theElement = Object( ), ListNode*n=NULL) : element(theElement),next(n) {}
Object element; ListNode *next; friend class List<Object>; friend class ListItr<Object>; };
20
bool isEmpty( ) const; void makeEmpty( ); ListItr<Object> zeroth( ) const; ListItr<Object> first( ) const; void insert( const Object & x, const ListItr<Object> & p ); ListItr<Object> find( const Object & x ) const; ListItr<Object> findPrevious( const Object & x ) const; void remove( const Object & x ); const List & operator=( const List & rhs );
private: ListNode<Object> *header;
};
22
Find routine
/* Return iterator corresponding to the first node containing an item x. Iterator isPastEnd if item is not found. */ template <class Object> ListItr<Object> List<Object>::find( const Object & x ) const { ListNode<Object> *itr = header->next; while( itr != NULL && itr->element != x ) itr = itr->next; return ListItr<Object>( itr ); }
26
*/
29
32
A1
A2
A3
34
ROAD MAP
Abstract Data Types (ADT) The List ADT Implementation of Lists Array implementation of lists Linked list implementation of lists Cursor implementation of lists
35
Then how can you use linked lists ? Actually not constant time
36
37
Collection of structures
Keep a free list new returns an element from freelist delete place the node in freelist Freelist
Use cell 0 as header All nodes are free initially 0 is a NULL pointer
38
39
41
cursorSpace initialization
/* Routine to initialize the cursorSpace. */ template <class Object> void List<Object>::initializeCursorSpace( ) { static int cursorSpaceIsInitialized = false; if( !cursorSpaceIsInitialized ) { cursorSpace.resize( 100 ); for( int i = 0; i < cursorSpace.size( ); i++ ) cursorSpace[ i ].next = i + 1; cursorSpace[ cursorSpace.size( ) - 1 ].next = 0; cursorSpaceIsInitialized = true; } }
44
48
}
}
49
50