140 likes | 151 Views
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.
E N D
CS 240: Data Structures Thursday, July 12th 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 • 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.
Vector • Vector is an STL provided sequential container. • It provides us with similar abilities as does our templated mycontainer (lab 4), soon… very soon.
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
Vector • We can access Vector data as follows: • front() //gets first element • back() //gets last element • operator [unsigned int] //gets element at specified location.
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);
Vector • Let’s write some code with Vector: • Handling insertion • Scanning through the vector • Iterators • Some built-in algorithms.
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!
Time efficiency • The time an algorithm takes has many variables: • Size of data set • Processing speed • Compiler optimizations, effective coding
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?
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
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