CS 215 ­ Fundamentals of Programming II  Fall 2013 ­ Homework 9

advertisement
CS 215 ­ Fundamentals of Programming II Fall 2013 ­ Homework 9
20 points
Out: November 1, 2013
Due: November 8, 2013 (Friday) November 11, 2013 (Monday)
Answer the following questions regarding STL lists, stacks and queues. Note: you are not required to implement these functions in a program that runs, but you can if you want to.
1. (5 points) Briefly answer the following questions
(a) What is the main difference between a vector<T> and a list<T>?
(b) What is the main difference between a stack<T> and a queue<T>?
2. (5 points) Write a template function list_divide that receives an STL list, a_list, and passes back two newly created STL lists, list1 and list2, such that list1 contains (copies of) the first, third, fifth, and successive odd­numbered elements, and list2 contains the even­numbered elements.
3. (5 points) Write a template function n2top that receives and passes back an STL stack, a_stack, and receives an integer n. This function moves the nth element (counting from the top, which is element 1) of the stack to the top of the stack. For example, if an integer stack s has the elements: (top) 3 4 17 5 8, then after the call n2top(s, 4), the stack will have elements: (top) 5 3 4 17 8. You may use only additional (local) stack(s) and individual variables to solve this problem. (I.e., use other data structures is not allowed.)
4. (5 points) Write a function n2front that receives and passes back an STL queue, a_queue, and receives an integer n. This function moves the nth element (counting from the front, which is element 1) of the queue to the front of the queue. For example, if a queue q has the elements: (front) 3 4 17 5 8 (back), then after the call n2front(q, 4), the queue will have elements: (front) 5 3 4 17 8 (back). You may use only additional (local) queue(s) and individual variables to solve this problem. (I.e., use of other data structures is not allowed.)
Revised 11/03/2013 – new due date
Page 1 of 1
D. Hwang
Download