Вы находитесь на странице: 1из 2

CSCI 2421 Midterm 2 Review Fall 2014

-1-
1. Write a prototype ONLY of a global function template for the list that adds an
element at the end of a linked-list.
template <class Item>
List<Item>::AddItem(Item item);
2. Fill in the missing statement for a list function that returns its last element. You may
assume there are at least two elements in the bag.
template <typename T> const T& list<T>::last( ) const
{
return listData->next ? ;
}
3. Write the statements that creates a dynamic array of <T> and that deletes the same
array. Any size will do.
T *array = new T[5];
delete[] array;
4. For each of the following, state whether the syntax is correct or incorrect using C or I.
a. template <typename T> list<T> * b foo(); __I__
b. template <typename T> void f() { std::cout << T(); } __C__
c. int a[] = {1,1,1,1,1}; vector<int> v(a+1,a+3); __C__
d. template <typename T, typename U> const U& f(const T& t) { } __C__
5. Show an iterator range for an array that represents elements 2 through 7. Assume
there are at least 7 elements in the array.
[1, 6];
6. Write the implementation of a list<T>::clear() where clear() erases all elements of the
list.
ResetList();
for (int i = 0; i < GetLength; i++) {
T item = GetNextItem();
DeleteItem(item);
}
7. State the running times of the push_back, pop_front, and front operations of a first-in,
first-out queue.
O(n), O(1), O(1)
9. Explain how to recognize a palindrome using a stack and a queue.
To determine whether a given input string is a palindrome, the characters of
the string should be pushed onto a stack and enqueued onto a queue. Then,
iterate over the entire stack or queue, popping a single character from the
stack, and dequeueing an item from the queue over each iteration. Compare
the characters; if they do not match, then the string is not a palindrome. If
the queue or stack has been iterated over, and no two characters have been
found to not match, then the string is a palindrome.
8. Write the implementation of a global function that displays the contents of a
stack<T>.
template <class T>
Stack<T>::DisplayContents()
{
while (!Empty()) {
T t = Pop();
std::cout << t;
}
}

Вам также может понравиться