1 / 14

CS 240: Data Structures

CS 240: Data Structures. Thursday, July 12 th Lists, Vector, Algorithms. Puppets. Now, dance!. Queues. Queues can be implemented from a linked list. We need to change insert to enqueue remove -> dequeue No operator[], but we do get peek. Stacks.

warrenc
Download Presentation

CS 240: Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 240: Data Structures Thursday, July 12th Lists, Vector, Algorithms

  2. Puppets. Now, dance!

  3. Queues • Queues can be implemented from a linked list. • We need to change insert to enqueue • remove -> dequeue • No operator[], but we do get peek.

  4. Stacks • Stacks can also be implemented from a linked list. • We need to change insert to push • remove -> pop • No operator[], but we do get peek.

  5. Vector • Vector is an STL provided sequential container. • It provides us with similar abilities as does our templated mycontainer (lab 4), soon… very soon.

  6. Vector • We declare a vector just like we do a templated mycontainer: • vector<T> testvector; • Many methods are built in: • Constructor, destructor, operator = • size(), capacity(), • clear() //equivalent to mycontainer::empty() • push_back(T) //equivalent to mycontainer::insert(T) • pop_back(T) //equivalent to mycontainer::remove(T), which we somehow didn’t cover

  7. Vector • We can access Vector data as follows: • front() //gets first element • back() //gets last element • operator [unsigned int] //gets element at specified location.

  8. Vector • Instead of currentvalue, Vector uses iterators: • vector<T>::iterator myiterator; //T must match the vector you want to use this iterator with. • myiterator = testvector.begin(); • myiterator = testvector.end(); • myiterator++; //equivalent to mycontainer::next() • myiterator--; //equivalent to mycontainer::previous() • *myiterator; //equivalent to mycontainer::current() • testvector.erase(myiterator); //equivalent to mycontainer::removeHere(); • testvector.insert(myiterator, T); //equivalent to mycontainer::insertHere(T);

  9. Vector • Let’s write some code with Vector: • Handling insertion • Scanning through the vector • Iterators • Some built-in algorithms.

  10. Algorithm Efficiency • What determines if an algorithm is efficient? • How much space does it take up? • How long does it take? • We usually worry about time when we discuss efficiency – however, space issues are also important!

  11. Time efficiency • The time an algorithm takes has many variables: • Size of data set • Processing speed • Compiler optimizations, effective coding

  12. Time Evaluation • We could count how many instructions are executed. • Let T(n) represent the time it takes for an algorithm to handle a data size of size n. • How long does insert() take?

  13. What about taking an average? How does this vary based on SIZE? SIZE has a direct effect on the performance of this algorithm! //float array[SIZE] is filled with data float sum = 0; for(int i=0;i<SIZE;i++) { sum += array[i]; } float average = sum/SIZE; Time Evaluation

  14. We refer to this as an “order of magnitude” -> Big Oh, or O() In this case, the algorithm is O(N), where N is the input size. Math: We say that T(N) has an order of magnitude of O(f(X)) where, T(N) <= Cf(X), for some int constant C, a sufficiently large N and f(X) in terms of N and minimized. f(X) = N, C >= 2 //float array[SIZE] is filled with data float sum = 0; for(int i=0;i<SIZE;i++) { sum += array[i]; } float average = sum/SIZE; Time Evaluation

More Related