Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Data Structures CSCI 132, Spring 2014 Lecture 19 Lists.

Similar presentations


Presentation on theme: "1 Data Structures CSCI 132, Spring 2014 Lecture 19 Lists."— Presentation transcript:

1 1 Data Structures CSCI 132, Spring 2014 Lecture 19 Lists

2 2 Looking ahead recursively LookAhead() for each possible move make move answer = lookAhead() undo move keep track of best answer return best answer

3 Looking Ahead int look_ahead(const Board &game, int depth, Move &recommended) { if (game.done( ) || depth == 0) { return game.evaluate( ); } else { Stack moves; game.legal_moves(moves); int value, best_value = game.worst_case( ); while (!moves.empty( )) { Move try_it, reply; moves.top(try_it);//get next move Board new_game = game;//make copy of board new_game.play(try_it);//try next move value = look_ahead(new_game, depth - 1, reply); //recursively look ahead if (game.better(value, best_value)) { best_value = value;//value is new best value recommended = try_it;//recommend this move } moves.pop( );//pop it off the stack } return best_value;//return best value found }

4 4 The List ADT (abstract data type) Stacks and Queues are restricted lists--You can only perform operations at the ends of the list. Lists are more general--You can insert, delete or access data at any point in the list--in the beginning middle or end. List operations: Constructinsert emptyremove fullretrieve sizereplace cleartraverse

5 5 List Specification 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]; };

6 6 Class templates To make our list (or stack or queue) class general we have been using a typedef: typedef int List_entry; Problem: We cannot specify two lists that have different types for their data members within a single piece of client code. Solution: Use a template class that allows the specification of generic types.

7 7 Specification of a template class template class List { public: // methods of the List ADT List( ); //other list methods here protected: // data members for a contiguous list implementation int count; List_entry entry[max_list]; };

8 8 Specifying types for a template class When objects are declared the type is specified: List first_list;//a list of integers List second_list;//a list of characters

9 9 Implementing a template class function template int List :: size( ) const { return count; }

10 10 Traversing a list Many useful functions traverse a list and perform some operation on each item in the list To specify the operation to be performed, we pass a function as a parameter. Functions can be passed by reference--the name of the function is a pointer to that function. Example: void print(List_entry &item) print with no parentheses is a pointer to the function, print(). To pass print as a parameter we use the pointer: List theList; theList.traverse(print);

11 11 Implementing traverse( ) template void List :: traverse(void (*visit)(List_entry &)) { for (int i = 0; i < count; i ++ ) { (*visit)(entry[i]); } } function parameter's return type formal parameter (pointer to a function) function's parameter list

12 12 Inserting an item in a contiguous (array) list To insert an item in a list implemented with an array, all items above the insertion position must be moved one position up in the array.

13 13 Implementing insert( ) template Error_code List :: insert(int position, const List_entry &x) { if (full( )) { return overflow; } if ((position count)) { return range_error; } // to be completed in class }

14 14 Implementing insert( ) template Error_code List :: insert(int position, const List_entry &x) { if (full( )) { return overflow; } if ((position count)) { return range_error; } for (int i = count - 1; i >= position; i--) {//Move each entry up by one entry[i + 1] = entry[i]; } entry[position] = x; count++; return success; }

15 15 Execution times for List functions List, clear, full, size, replace and retrieve-- All these have the same execution time independent of the size of the list. They have constant running time. Insert-- On the average, insert must shift n/2 items for a list of length n. So, the running time of insert is proportional to the length of the list (n). Traverse-- The running time of traverse depends on the time needed to execute the parameter function. But, since traverse must visit each of the n entries in the list, it can never be better than a running time that is proportional to the length of the list (n).


Download ppt "1 Data Structures CSCI 132, Spring 2014 Lecture 19 Lists."

Similar presentations


Ads by Google