7. Ph動董ng th畛c traverse v tham s畛 hm void print_int( int &x) { cout << x << ; } void increase_int( int &x) { x++; } void main() { List< int > alist; alist.traverse(print_int); alist.traverse(increase_int); } Khi g畛i tham s畛 hm, ch動董ng tr狸nh d畛ch ph畉i nh狸n th畉y hm 動畛c g畛i. T湛y theo m畛c 鱈ch m g畛i c叩c hm kh叩c nhau.
8. Hi畛n th畛c danh s叩ch li棚n t畛c template < class List_entry> class List { public: // methods of the List ADT List( ) ; int size( ) const; bool full( ) const; bool empty( ) const; void clear( ) ; void traverse( void (*visit)(List_entry &)) ; Error_code retrieve( int position , List_entry &x) const; Error_code replace( int position , const List_entry &x) ; Error_code remove( int position , List_entry &x) ; Error_code insert( int position , const List_entry &x) ; protected: // data members for a contiguous list implementation int count ; List_entry entry[max_list] ; } ;
9. Th棚m vo m畛t danh s叩ch li棚n t畛c insert(3, z) d e f g h d e f g h z count=8 count=9 a b c 0 1 2 3 4 5 6 7 8 9
10. Gi畉i thu畉t th棚m vo m畛t danh s叩ch li棚n t畛c Algorithm Insert Input : position l v畛 tr鱈 c畉n th棚m vo, x l gi叩 tr畛 c畉n th棚m vo Output : danh s叩ch 達 th棚m vo x 1. if list 畉y 1.1. return overflow 2. if position n畉m ngoi kho畉ng [0..count] 2.1. return range_error //D畛i t畉t c畉 c叩c ph畉n t畛 t畛 position v畛 sau 1 v畛 tr鱈 3. for index = count-1 down to position 3.1. entry[index+1] = entry[index] 4. entry[position] = x //G叩n x vo v畛 tr鱈 position 5. count++ //Tng s畛 ph畉n t畛 l棚n 1 6. return success; End Insert
11. M達 C++ th棚m vo m畛t danh s叩ch li棚n t畛c template < class List_entry> Error_code List<List_entry> :: insert( int position , const List_entry &x) { if (full( )) return overflow ; if (position < 0 || position > count) return range_error ; for ( int i = count 1 ; i >= position ; i) entry[i + 1] = entry[i] ; entry[position] = x ; count++; return success ; }
12. X坦a t畛 m畛t danh s叩ch li棚n t畛c x remove(3, x) d e f g h d e f g h count=8 count=7 a b c 0 1 2 3 4 5 6 7 8 9
13. Gi畉i thu畉t x坦a t畛 m畛t danh s叩ch li棚n t畛c Algorithm Remove Input : position l v畛 tr鱈 c畉n x坦a b畛, x l gi叩 tr畛 l畉y ra 動畛c Output : danh s叩ch 達 x坦a b畛 ph畉n t畛 t畉i position 1. if list r畛ng 1.1. return underflow 2. if position n畉m ngoi kho畉ng [0..count-1] 2.1. return range_error 3. x = entry[position] //L畉y x t畉i v畛 tr鱈 position ra 4. count-- //Gi畉m s畛 ph畉n t畛 i 1 //D畛i t畉t c畉 c叩c ph畉n t畛 t畛 position v畛 tr動畛c 1 v畛 tr鱈 5. for index = position to count-1 5.1. entry[index] = entry[index+1] 6. return success; End Remove
14. Gi畉i thu畉t duy畛t m畛t danh s叩ch li棚n t畛c Algorithm Traverse Input : hm visit d湛ng 畛 t叩c 畛ng vo t畛ng ph畉n t畛 Output : danh s叩ch 動畛c c畉p nh畉t b畉ng hm visit //Qu辿t qua t畉t c畉 c叩c ph畉n t畛 trong list 1. for index = 0 to count-1 1.1. Thi hnh hm visit 畛 duy畛t ph畉n t畛 entry[index] End Traverse
15. M達 C++ duy畛t m畛t danh s叩ch li棚n t畛c template < class List_entry> void List<List_entry> :: traverse( void (*visit)(List_entry &)) / * Post: T叩c v畛 cho b畛i hm visit s畉 動畛c thi hnh t畉i m畛i thnh ph畉n c畛a list b畉t 畉u t畛 v畛 tr鱈 0 tr畛 i. * / { for ( int i = 0 ; i < count ; i++) (*visit)(entry[i]) ; }
18. T狸m v畛 tr鱈 tr棚n DSLK 董n Nhu c畉u: Nh畉p vo ch畛 s畛 c畛a m畛t ph畉n t畛 Cho bi畉t 坦 l ph畉n t畛 no (con tr畛 ch畛 畉n ph畉n t畛) t動畛ng: B畉t 畉u t畛 ph畉n t畛 畉u ti棚n Di chuy畛n 炭ng position b動畛c th狸 畉n 動畛c ph畉n t畛 c畉n t狸m Ph畉i 畉m b畉o l position n畉m trong kho畉ng [0..count-1]
19. Gi畉i thu畉t t狸m v畛 tr鱈 tr棚n DSLK 董n Algorithm Set position Input : position l v畛 tr鱈 c畉n t狸m Output : con tr畛 ch畛 畉n ph畉n t畛 t畉i v畛 tr鱈 c畉n t狸m 1. set q to head 2. for index =0 to position //Thi hnh position b動畛c 2.1. advance q to the next element //Tr畛 q 畉n ph畉n t畛 k畉 ti畉p 3. return q End Set position index=0 set_position(2) index=1 index=2 q x y z head m
20. M達 C++ t狸m v畛 tr鱈 tr棚n DSLK 董n template < class List_entry> Node<List_entry> *List<List_entry> :: set_position( int position) const / * Pre: position l v畛 tr鱈 h畛p l畛 trong list, 0 < position < count. Post: Tr畉 v畛 m畛t con tr畛 ch畛 畉n Node ang 畛 v畛 tr鱈 position */ { Node<List_entry> *q = head ; for ( int i = 0 ; i < position ; i++) q = q->next ; return q ; }
21. Th棚m vo m畛t DSLK 董n new_node a y following_node ph畉n t畛 t畉i v畛 tr鱈 position x previous_node ph畉n t畛 t畉i v畛 tr鱈 position-1 b但y gi畛, ph畉n t畛 ny c坦 v畛 tr鱈 position b但y gi畛, ph畉n t畛 ny c坦 v畛 tr鱈 position+1
22. Gi畉i thu畉t th棚m vo m畛t DSLK 董n Algorithm Insert Input: position l v畛 tr鱈 th棚m vo, x l gi叩 tr畛 th棚m vo Output: danh s叩ch 達 th棚m vo x t畉i v畛 tr鱈 position 1. N畉u position > 0 1.1. Tr畛 previous 畉n ph畉n t畛 t畉i v畛 tr鱈 position-1 1.2. Tr畛 following 畉n ph畉n t畛 sau previous 2. Ng動畛c l畉i 2.1. Tr畛 following 畉n head 3. T畉o ra node m畛i l new_node v畛i gi叩 tr畛 x 4. Tr畛 next c畛a new_node 畉n following 5. N畉u position l 0 5.1. Tr畛 head 畉n new_node 6. Ng動畛c l畉i 6.1. Tr畛 next c畛a previous 畉n new_node 7. Tng s畛 l動畛ng c叩c ph畉n t畛 l棚n 1 End Insert
23. M達 C++ th棚m vo m畛t DSLK 董n template < class List_entry> Error_code List<List_entry> :: insert( int position , const List_entry &x) { if (position < 0 || position > count) return range_error ; Node<List_entry> *new_node , *previous , *following ; if (position > 0) { previous = set_position(position 1) ; following = previous->next ; } else following = head ; new_node = new Node<List_entry>(x , following) ; if (new_node == NULL) return overflow ; if (position == 0) head = new_node ; else previous->next = new_node ; count++ ; return success ; }
24. X坦a b畛 t畛 m畛t DSLK 董n b但y gi畛, ph畉n t畛 ny c坦 v畛 tr鱈 position ph畉n t畛 t畉i v畛 tr鱈 position ph畉n t畛 t畉i v畛 tr鱈 position+1 x z previous_node ph畉n t畛 t畉i v畛 tr鱈 position-1 y following_node
26. 畛nh ngh挑a DSLK k辿p template < class List_entry> class List { public: // Add specications for methods of the list ADT. // Add methods to replace compiler generated defaults. protected: // Data members for the doubly-linked list implementation follow: int count ; mutable int current_position ; mutable Node<List_entry> *current ; // The auxiliary function to locate list positions follows: void set_position( int position) const; } ; C叩c hm h畉ng ( const ) c坦 th畛 thay 畛i gi叩 tr畛 c畛a c叩c bi畉n mutable ny
27. 畛nh ngh挑a Node cho DSLK k辿p template < class Node_entry> struct Node { // data members Node_entry entry ; Node<Node_entry> *next ; Node<Node_entry> *back ; // constructors Node( ) ; Node(Node_entry , Node<Node_entry> *link_back = NULL , Node<Node_entry> *link_next = NULL) ; } ; z
28. T狸m v畛 tr鱈 trong DSLK k辿p set_position(6) set_position(8) x y z m current current_position position = 5 position = 6 position = 7 position = 8
29. Th棚m vo trong DSLK k辿p current previous following new_node x y z ph畉n t畛 t畉i v畛 tr鱈 position-1 ph畉n t畛 t畉i v畛 tr鱈 position ph畉n t畛 ny b但y gi畛 c坦 v畛 tr鱈 l position ph畉n t畛 ny b但y gi畛 c坦 v畛 tr鱈 l position+1
30. Th棚m vo trong DSLK k辿p Algorithm Insert Input: x l gi叩 tr畛 c畉n th棚m vo t畉i position (0<=position<=count) Output : danh s叩ch 達 th棚m gi叩 tr畛 x vo v畛 tr鱈 position 1. if position l 0 1.1. if s畛 ph畉n t畛 l 0 1.1.1. Tr畛 following 畉n NULL 1.2. Tr畛 preceding 畉n NULL 2. else 2.1. Tr畛 preceding 畉n v畛 tr鱈 position -1, following 畉n v畛 tr鱈 position 3. T畉o ra ph畉n t畛 m畛i new_node 4. Tr畛 next v back c畛a new_node 畉n following v preceding 5. if preceding kh叩c r畛ng 5.1. Tr畛 next c畛a preceding 畉n new_node 6. if following kh叩c r畛ng 6.1. Tr畛 back c畛a following 畉n new_node 7. Tng s畛 ph畉n t畛 l棚n 1 End Insert
31. M達 C++ th棚m vo trong DSLK k辿p template < class List_entry> Error_code List<List_entry> :: insert( int position , const List_entry &x) { Node<List_entry> *new node , *following , *preceding ; if (position < 0 || position > count) return range_error ; if (position == 0) { if (count == 0) following = NULL ; else { set_position(0) ; following = current ; } preceding = NULL ; } else { set_position(position 1) ; preceding = current ; following = preceding->next ; } new_node = new Node<List_entry>(x , preceding , following) ; if (new_node == NULL) return overflow ; if (preceding != NULL) preceding->next = new_node ; if (following != NULL) following->back = new_node ; current = new_node ; current_position = position ; count++ ; return success ; }
32. So s叩nh c叩ch hi畛n th畛c li棚n t畛c v c叩ch hi畛n th畛c li棚n k畉t DS li棚n t畛c th鱈ch h畛p khi: K鱈ch th動畛c t畛ng ph畉n t畛 l r畉t nh畛 K鱈ch th動畛c c畛a c畉 danh s叩ch (s畛 ph畉n t畛) 達 bi畉t khi l畉p tr狸nh C坦 鱈t s畛 th棚m vo hay lo畉i b畛 畛 gi畛a danh s叩ch H狸nh th畛c truy c畉p tr畛c ti畉p l quan tr畛ng DSLK th鱈ch h畛p khi: K鱈ch th動畛c t畛ng ph畉n t畛 l l畛n K鱈ch th動畛c c畛a danh s叩ch kh担ng bi畉t tr動畛c C坦 nhi畛u s畛 th棚m vo, lo畉i b畛, hay x畉p x畉p c叩c ph畉n t畛 trong danh s叩ch
33. Chu畛i (string) Chu畛i l m畛t d達y c叩c k箪 t畛 V鱈 d畛: This is a string l 1 chu畛i c坦 16 k箪 t畛 l m畛t chu畛i r畛ng (c坦 0 k箪 t畛) Chu畛i tr畛u t動畛ng: C坦 th畛 xem l danh s叩ch C坦 c叩c t叩c v畛 th動畛ng d湛ng: Sao ch辿p ( strcpy ) N畛i k畉t ( strcat ) T鱈nh chi畛u di ( strlen ) So s叩nh 2 chu畛i ( strcmp ) T狸m m畛t chu畛i trong chu畛i kh叩c ( strstr )
34. Chu畛i tr棚n C C坦 ki畛u l char * K畉t th炭c b畉ng k箪 t畛 (NULL) S畛 ph畉n t畛 trong b畛 nh畛 nhi畛u h董n chi畛u di chu畛i l 1 C畉n chu畉n b畛 b畛 nh畛 c畉n thi畉t khi thao t叩c V鱈 d畛: char *str1, *str2; delete str2; str2 = new char[strlen(str1) + 1]; strcpy(str2, str1);