CS 215 ­ Fundamentals of Programming II  Spring 2011 ­ Homework 11

advertisement
CS 215 ­ Fundamentals of Programming II Spring 2011 ­ Homework 11
20 points
Out: March 23
Due: March 30 (Wednesday)
Answer the following questions regarding 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 ListDivide that receives an STL list, aList, 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, aStack, 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.
4. (5 points) Write a function N2Front that receives and passes back an STL queue, aQueue, 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.
03/22/2011
Page 1 of 1
D. Hwang
Download