The Basic Data Structures
The material on linear structures such as lists, stacks and queues should be review and will be treated as such.
Exercise -- Linear Structure Efficiency
Different linear structures differ in the efficiency of the operations applied to them. One must be aware of these efficiencies when choosing between the different linear structures for a particular problem (or when determining whether any of them are appropriate).
There are three main operations on a linear structure: search, insert and delete. Please fill in the following table to give the worst-case efficiencies of these operations for the different linear data structures. In the table, the ``Insert'' operation is broken up into ``Finding'' the location to insert and actually performing the insert. Similarly, the ``Delete'' operation is broken up into ``Finding'' the location to delete and actually performing the deletion. The operations on sorted lists must preserve the ordering!
Be careful of the questions on stacks and queues: some of the operations are not appropriate!
2c|Insert | 2c|Delete | ||||
Linear Structure | Search | Find | Insert | Find | Delete |
Lists: Arrays | |||||
Lists: Linked | |||||
Sorted Lists: Arrays | |||||
Sorted Lists: Linked | |||||
Stacks | |||||
Queues |
The Polynomial Class
Central and powerful features of C++ are the mechanisms for creating class objects that mimic real life objects, defining natural operations on them, and then using them as building blocks in much larger programs. The extended example here is of polynomials.
let f and g be the two polynomials to be added let h be the resulting polynomial while unprocessed terms remain in both f and g { let t1 be the remaining term in f with greatest exponent let t2 be the remaining term in g with greatest exponent if (t1.exponent == t2.exponent) append a new term to h with coefficient = t1.coefficent + t2.coefficient and exponent = t1.exponent else if (t1.exponent > t2.exponent) append a copy of t1 to the end of h else append a copy of t2 to the end of h } copy the remaining terms of f to the end of h copy the remaining terms of g to the end of h
Exercises exploring the Polynomial class
Amortized analysis and dynamic arrays
This material will only be included if we have time. It shows that the cost of using dynamically resized arrays, such as the vector container class from the standard library, is not significantly greater than the cost of using ordinary arrays.
const int Mult=2; const int Add=100; template <class T> class Vector { public: Vector( int init_size = 1 ); void PushBack( const T& item ); // and many more useful member functions private: T * values_; int count_; // number of current values int size_; // size of the current allocation }; template <class T> Vector<T>::Vector( int init_size ); { values_ = new T[init_size]; count_ = 0; size_ = init_size; } template <class T> void Vector<T>::PushBack( const T & item ) { if ( count_ == size_ ) { // reallocation int new_size = size_ + Add; // #1a int new_size = size_ * Mult; // #1b T * temp = new T[new_size]; for ( int i=0; i<size_; i++ ) temp[i] = values_[i]; // #2 delete [] values_; values_ = temp; size_ = new_size; } values_[count_++] = item; // #3 }
Only one of the statements 1a and 1b will be used. Part of the problem is to figure out which.
Consider the following operation:
Vector<int> v; for ( int i=0; i<n; i++ ) v.PushBack(10*i);
Now, do the following exercises, in order:
PushBack
function? Count only
statements labeled #2
and #3
and consider option #1a
only. Can you see
a pattern?
#3
when it is summed over all n
iterations of the for
loop? This should be straightforward.
#2
when it is summed over all n
iterations of the for
loop? This should be more involved.
PushBack
operation.
#1b
is used instead of #1a
.
Review Problems
In addition to the material covered in class, the quiz for chapter 3
will include material from the lab on doubly-linked lists (lab 3) and
the lab on the standard library (STL) vectors and lists
(lab 4). Students should not worry too much about STL syntax, but
should worry about semantics. For example, if you assume there is a
push_front
method on an STL vector you will be penalized
because this operation makes no sense in terms of the logical
properties of vectors and STL.
std::cout
)
every nth value in the list, starting from the end of the list,
where n is passed in as an integer parameter. The first value
displayed will always be the last item in the list; the second will be
the (n+1)st from the end; etc. Repeat using an STL vector
instead of a list.
template <class T> struct Node { T value; Node * next; };for singly-linked lists and
template <class T> struct Node { T value; Node * next; Node * prev; };for doubly-linked lists. Assume each function call is
void Reverse( Node* & head )and that
head
is to be changed to point to the node that
previously was the tail.