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

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

DATA STRUCTURES USING C++ Chapter 5
CSEB324 Data Structures & Algorithms
Chapter6 LISTS AND STRINGS. Outline 1. List Specifications 2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked.
Templates in C++. Generic Programming Programming/developing algorithms with the abstraction of types The uses of the abstract type define the necessary.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
1 Data Structures CSCI 132, Spring 2014 Lecture 8 Implementing Queues.
Lecture 18: 4/11/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Data Structures Chapter 4 Linked Stacks and Queues Andreas Savva.
Stacks.
Void write_method() /* Post: A short string identifying the abstract data type used for the structure is written. */ Project 2 -- Analysis.
Data Structures Topic #3. Today’s Agenda Ordered List ADTs –What are they –Discuss two different interpretations of an “ordered list” –Are manipulated.
1 Data Structures CSCI 132, Spring 2014 Lecture 22 Searching.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Data Structures Chapter 6 Linked Lists Andreas Savva.
Data Structures Chapter 2 Stacks Andreas Savva. 2 Stacks A stack is a data structure in which all insertions and deletions of entries are made at one.
Object Oriented Data Structures
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
Introduction to Stacks Chapter 2. Objectives Introduce abstract data types. Discuss implementation types. – Static – Dynamic – Contiguous Introduce the.
1 Data Structures CSCI 132, Spring 2014 Lecture 36 Binary Search Trees.
Lists and Strings Chapter 6. Objectives Discuss general list abstract data type. Implement class templates. Implement the general list three different.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
CS342 Data Structures End-of-semester Review S2002.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Data Structures & Algorithms
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
1 Data Structures CSCI 132, Spring 2014 Lecture 6 Applications using Stacks.
Queues. Like Stacks, Queues are a special type of List for storing collections of entities. Stacks are Lists where insertions (pushes) and deletions (pops)
Kruse/Ryba ch031 Object Oriented Data Structures Queues Implementations of Queues Circular Implementation of Queues.
Kruse/Ryba ch031 Object Oriented Data Structures Queues Implementations of Queues Circular Implementation of Queues.
1 Data Structures CSCI 132, Spring 2014 Lecture 34 Analyzing Hash Tables.
Chapter 6 LISTS AND STRINGS 1. List Definition 2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Doubly Linked 3. Linked.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
1 Data Structures CSCI 132, Spring 2014 Lecture 18 Recursion and Look-Ahead.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 CSC 222: Computer Programming II Spring 2004 Stacks and recursion  stack ADT  push, pop, top, empty, size  vector-based implementation, library 
Fall 2006 METU EEEEE 441 S. Ece (GURAN) SCH MIDT EE 441 Data Structures Lecture 6 Stacks and Queues.
1 Data Structures CSCI 132, Spring 2016 Notes 6 Applications using Stacks.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
CS505 Data Structures and Algorithms
Chapter 4 The easy stuff.
CC 215 Data Structures Queue ADT
CSCI-255 LinkedList.
Chapter 4 Linked Lists
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
CMSC 341 Lecture 5 Stacks, Queues
Chapter 4 Linked Lists.
Stacks as Linked lists top_node typedef Stack_entry Node_entry;
Chapter 4 Linked Lists.
Lists - I The List ADT.
Lists - I The List ADT.
Queues Jyh-Shing Roger Jang (張智星)
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Abstract Data Types Stacks CSCI 240
5.3 Implementing a Stack Chapter 5 – The Stack.
Presentation transcript:

1 Data Structures CSCI 132, Spring 2014 Lecture 19 Lists

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

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 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 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 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 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 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 Implementing a template class function template int List :: size( ) const { return count; }

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 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 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 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 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 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).