|
|
COT 5410-01 Fall 2004 Algorithms Chris Lacher Notes 5: Sequential ADTs and Data Structures |
//--------------------------
// Vector<T>
//--------------------------
template < typename T >
class Vector
{
public:
// Access support: VectorIterator
// Terminology support: value_type, Iterator
// Proper type: constructors, destructors, assignment
T& operator [] (size_t) const;
// Container class protocol
int Empty () const;
size_t Size () const;
int PushBack (const T&);
int PopBack ();
void Clear ();
T& Front () const;
T& Back () const;
// other methods
int SetSize (size_t);
int SetSize (size_t, const T&);
int SetCapacity (size_t);
size_t Capacity () const;
// Iterator support
typedef VectorIterator<T> Iterator;
friend class VectorIterator<T>;
Iterator Begin () const;
Iterator End () const;
Iterator rBegin () const;
Iterator rEnd () const;
protected:
// data
size_t size, capacity;
T* content; // pointer to the primative array elements
} ;
//----------------------------------
// VectorIterator<T>
//----------------------------------
template < typename T >
class VectorIterator
{
public:
// Access support: Vector
// Terminology support: value_type
// Proper type: constructors, destructors, assignment
// Random Access Iterator protocol
int operator == (const VectorIterator<T>& I2) const;
int operator != (const VectorIterator<T>& I2) const;
T& operator * () const; // Return reference to current Tval
T& operator [] (size_t i) const; // Return reference to Tval at index i
VectorIterator<T>& operator = (const VectorIterator<T> & I);
VectorIterator<T>& operator ++ (); // prefix
VectorIterator<T> operator ++ (int); // postfix
VectorIterator<T>& operator -- (); // prefix
VectorIterator<T> operator -- (int); // postfix
// "pointer" arithmetic -- uses template parameter for integer type
long operator - (const VectorIterator<T> & I2) const;
template <typename N>
VectorIterator<T> operator + (N n) const;
template <typename N>
VectorIterator<T>& operator += (N n);
template <typename N>
VectorIterator<T>& operator -= (N n);
protected:
T* Tptr;
} ;
1. Each of the container class operations
PopBack(),
Empty(), Size(), Clear(), Front(), Back(), and
operator []()
has runtime O(1) and runspace O(1)
2. The operation PushBack() has amortized runtime O(1)
and amortized runspace O(1), with worst cases Theta(n)
3. SetSize(), SetCapacity(), copy, and assignment, have runtime Theta(n) and
runspace Theta(n)
4. Destruction has runtime Theta(n) and constant runspace
5. Each Iterator operation has runtime O(1) and runspace O(1)
//----------------------------------
// List<T>
//----------------------------------
template < typename T >
class List
{
public:
// Access support: ListIterator
// Terminology support: value_type, Iterator
// Proper type: constructors, destructors, assignment
// sContainer Protocol:
int PushFront (const T& t); // Insert t at front of list (see notes 1,2)
int PushBack (const T& t); // Insert t at back of list
int Insert (Iterator& I, const T& t); // Insert t at I (see note 3)
int PopFront (); // Remove the Tval at front
int PopBack (); // Remove the Tval at back
int Remove (Iterator& I); // Remove item at I (see note 5)
void Clear (); // Empty the list, deleting all elements
size_t Size () const; // return the number of elements on the list
int Empty () const; // true iff list has no elements
T& Front () const; // return reference to Tval at front of list
T& Back () const; // return reference to Tval at back of list
Iterator Begin () const; // return iterator to front (see note 7)
Iterator End () const; // return iterator "passed the back"
Iterator rBegin () const; // return iterator to back
Iterator rEnd () const; // return iterator "passed the front" in reverse
protected:
// A scope List<T>:: class usable only by its friends (all members are private)
class Link
{
friend class List<T>;
friend class ListIterator<T>;
// Link data
T value; // data
Link * prevLink; // ptr to predecessor Link
Link * nextLink; // ptr to successor Link
// Link constructor - parameter required
Link(const T& Tval);
} ;
// structural data
Link * firstLink, // pointer to the first element in the list
* lastLink; // pointer to the last element in the list
// protected method -- used only by other methods
void Clone (const List<T>& L); // makes *this a clone of L
} ;
//----------------------------------
// ListIterator<T>
//----------------------------------
template <typename T>
class ListIterator
{
public:
// Access support: List
// Terminology support: value_type
// Proper type: constructors, destructors, assignment
// Bidirectional Iterator protocol
int operator == (const ListIterator& I2) const;
int operator != (const ListIterator& I2) const;
T& operator * () const; // Return reference to current Tval
ListIterator<T>& operator = (const ListIterator <T> & I);
ListIterator<T>& operator ++ (); // prefix
ListIterator<T> operator ++ (int); // postfix
ListIterator<T>& operator -- (); // prefix
ListIterator<T> operator -- (int); // postfix
protected:
typename List<T>::Link * currLink;
} ;
1. All List and ListIterator operations run in constant time and space, with the exceptions listed below: 2. Size(), copy, assignment, and destruction have runtime Theta(size) and constant runspace.
1. All List and ListIterator operations run in constant time and space, with the exceptions listed below: 2. Copy, assignment, and destruction have runtime Theta(size) and constant runspace.
//--------------------------
// Deque<T>
//--------------------------
template < typename T >
class Deque
{
public:
// Access support: DequeIterator
// Terminology support: value_type, Iterator
// Proper type: constructors, destructors, assignment
T& operator [] (size_t) const;
// Container class protocol
int Empty () const;
size_t Size () const;
int PushBack (const T&);
int PopBack ();
void Clear ();
T& Front () const;
T& Back () const;
size_t Capacity () const;
// Iterator support
typedef DequeIterator<T> Iterator;
friend class DequeIterator<T>;
Iterator Begin () const;
Iterator End () const;
Iterator rBegin () const;
Iterator rEnd () const;
protected:
// classic circular array implementation
T* content;
size_t content_size, beg, end;
} ;
//----------------------------------
// DequeIterator<T>
//----------------------------------
template < typename T >
class DequeIterator
{
public:
// Access support: Deque
// Terminology support: value_type
// Proper type: constructors, destructors, assignment
// Random Access Iterator protocol
int operator == (const DequeIterator<T>& I2) const;
int operator != (const DequeIterator<T>& I2) const;
T& operator * () const; // Return reference to current Tval
T& operator [] (size_t i) const; // Return reference to Tval at index i
DequeIterator<T>& operator = (const DequeIterator<T> & I);
DequeIterator<T>& operator ++ (); // prefix
DequeIterator<T> operator ++ (int); // postfix
DequeIterator<T>& operator -- (); // prefix
DequeIterator<T> operator -- (int); // postfix
// "pointer" arithmetic -- uses template parameter for integer type
long operator - (const DequeIterator<T> & I2) const;
template <typename N>
DequeIterator<T> operator + (N n) const;
template <typename N>
DequeIterator<T>& operator += (N n);
template <typename N>
DequeIterator<T>& operator -= (N n);
private:
// alternative pattern for iterator implementation
const Deque<T>* Qptr;
size_t index;
} ;
1. Each of the container class operations
PopFront(), PopBack(),
Empty(), Size(), Clear(), Front(), Back(), and
operator []()
has runtime O(1) and runspace O(1)
2. The operations PushFront() and PushBack() ha\ve amortized runtime O(1)
and amortized runspace O(1), with worst cases Theta(n)
3. Copy and assignment have runtime Theta(n) and runspace Theta(n)
4. Destruction has runtime Theta(n) and constant runspace
5. Each Iterator operation has runtime O(1) and runspace O(1)
Vector List Deque PushFront(t) O(1) AO(1) PopFront() O(1) O(1) PushBack(t) AO(1) O(1) AO(1) PopBack() O(1) O(1) O(1) Insert(I,t) O(1) Resize(n) O(size) Iterator Support RA BD RA Search Algorithm Binary Sequential Binary