Winter 2007 CS 32
Homework 1 Solution
Problems 1, 2, and 3
Problem 4
Problem 5
Problems 1, 2, and 3:
In this solution, the functions with small, fast implementations are inlined. Alternatively,
the
inline
keyword can be removed and the function definitions moved to
Sequence.cpp. (
inline
will be mentioned at some point in class, so don't worry if you've
never seen it before.)
Notice which member functions are const, and observe the use of the typedef name
ItemType
.
// Sequence.h
#ifndef SEQUENCE_INCLUDED
#define SEQUENCE_INCLUDED
// Later in the course, we'll see that templates provide a much nicer
// way of enabling us to have Sequences of different types.
For now,
// we'll use a typedef.
typedef unsigned long ItemType;
const int DEFAULT_MAX_ITEMS = 200;
class Sequence
{
public:
Sequence();
// Create an empty sequence.
bool empty()
const
;
// Return true iff the sequence is empty.
int size()
const
;
// Return the number of items in the sequence.
bool insert(int pos, const
ItemType
& value);
// Insert value into the sequence so that it becomes the item at
// position pos.
The original item at position pos and those that
// follow it end up at a position one higher than before.
Return
// true if 0 <= pos <= size() and the value could be inserted.
// (It might not be, if the sequence has a fixed capacity, e.g.,
// because it's implemented using a fixedsize array.)
Otherwise,
// leave the sequence unchanged and return false.
Notice that
// if pos is equal to size(), the value is inserted at the end.
bool insert(const
ItemType
& value);
// Let p be the smallest integer such that value <= the item at
// position p in the sequence; if no such item exists (i.e.,
// value > all items in the sequence), let p be size().
Insert
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
// value into the sequence so that it becomes the item at position
// p.
The original item at position p and those that follow it
end
// up at a position one higher than before.
Return true iff the
// value could be inserted.
(It might not be, if the sequence
has a
// fixed capacity.)
bool erase(int pos);
// If 0 <= pos < size(), remove the item at position pos from
// the sequence (so that all items that followed this item end up
at
// a position one lower than before), and return true.
Otherwise,
// leave the sequence unchanged and return false.
int remove(const
ItemType
& value);
// Erase all items from the sequence that == value.
Return the
// number of items removed (which will be 0 if no item == value).
bool select(int pos,
ItemType
& value)
const
;
// If 0 <= pos < size(), copy into value the item at position pos
// in the sequence and return true.
Otherwise, leave value
unchanged
// and return false.
int find(const
ItemType
& value)
const
;
// Let p be the smallest integer such that value == the item at
// position p in the sequence; if no such item exists, let p be 
1.
// Return p.
void swap(Sequence& other);
// Exchange the contents of this sequence with the other one.
private:
ItemType m_data[DEFAULT_MAX_ITEMS];
// the items in the sequence
int
m_size;
// number of items in the
sequence
// At any time, the elements of m_data indexed from 0 to m_size1
// are in use.
This is the end of the preview.
Sign up
to
access the rest of the document.
 Winter '08
 DavidSmallBerg
 Return statement, Exit status

Click to edit the document details