150 likes | 274 Views
CS342 Data Structures. End-of-semester Review S2002. Final Examination (open book, notes). 30-40% on low-level implementations of the operations of various data structures including stacks; queues and priority queues; lists; binary trees; sets and maps.
E N D
CS342 Data Structures End-of-semester Review S2002
Final Examination(open book, notes) • 30-40% on low-level implementations of the operations of various data structures including stacks; queues and priority queues; lists; binary trees; sets and maps. • 60-70% on high-level abstractions (STL) and applications of various data structures mentioned above. • Must know the “template” well.
Low level implementations Given a class interface such of any class or ADT: • implement member functions. • use the ADT to solve a particular problem.
The STL • Must know and be able to use the frequently used member functions of string, vector, list, stack, queue, priority queue, and set ADTs to solve some simple problems. You may also be asked to describe the action of a segment of code and analyze its efficiency in terms of Big-O notation.
Examples • Implement the push or pop function of a stack using dynamically allocated or static array storage. • Given nodes and pointers to nodes, implement a function or two to insert or delete a node under a specified condition. • etc.
STL examples • Vector: given the following function template<typename T> void func(vector<T> &v) { if(v.size( ) >=2) { v[v.size( ) – 2] = v.back( ); v.pop_back( ): } } • What is the output of the following code? string str[ ] = {“AA”, “AB”, “AC”, “AD”, “AE”}; int strSize = sizeof(str) / sizeof(string); vector<string> v(str, str + strSize); int i; func(v); for (i = 0; i < v.size( ); i++) cout << v[i] << ‘\t’;
STL examples • list: Display the list created by the following sequence of statements: intList.push_front(0); iter = intList.begin( ); for (int i = 1; i <= 4; i++) { intList.push_front(i); insert(iter, i); } • What if the arguments of insert function is replaced by (iter, *iter) ?
STL examples • Stack: trace the following program and answer some questions: template<typename T> void f(stack<T> &s, stack<T> &t, int n); int main() { int arr[5] = {2, 1, 7, 4, 3}, i; stack<int> s, t; for (i = 0; i < 5; i++) s.push(arr[i]); f(s, t, 4); outputStack(t); // display content of stack t one element at a time return 0; }
STL examples template<typename T> void f(stack<T> &s, stack<T> &t, const T item) { T svalue; while (!s.empty( )) { svalue = s.top( ); s.pop( ); if (svalue < item) t.push(svalue); } } • What is displayed by the outputStack( ) function?
STL examples • Given the definition of postfix expression (not given here), draw the sequence of stack configuration in the evaluation of the following postfix expression: 2 3 5 * % 6 +
STL examples • Queue: what is the contents of the bins and the vector after pass 2 of the radix sort for the following three digit integer? Integers to be sorted: 365 251 67 175 453 380 905 659 251 49 598 782 967
STL examples • priority queue: use a priority queue object to reorder a vector of integers. What is the order of the vector v = {6, 12, 34, 15, 67, 45, 53, 3, 5} after the execution of function f( )? void f(vector<int &v) { priority_queue<int> pq; int i; for (i = 0; i < v.size( ); i++) pq.push(v[i]); i = 0; while(!pq.empty( )) { v[i] = pq.top( ); pq.pop( ); i++; } }
STL examples • linked list: trace the code in function f( ): template<typename T> void f (node<T> *front) { node<T> *p = front; T value; if (front == NULL) return; while(p->next !=NULL) p = p->next; value = p->nodevalue p->nodeValue = front->nodeValue; front->nodeValue = value } • Assuming the list is front-> 7-> 2 -> 9 -> 5-> NULL, what is the new list?
STL examples • Binary tree: given a binary tree of any node type, what is the order in which nodes are visited in an LRN, LNR, or NLR scan? • A complete tree with 5125 nodes, what is its depth? • Implement a function countNodes( ) which returns the number of nodes in a binary tree as a reference argument: template<typename T> void countNodes(tnode<T> *t, int & count); // pointer t points to root
Other types of problems • Big-O • Recursive algorithms • Dynamic memory allocations • Graphs (if any, extra points problem)