Homework_solutions

Homework_solutions - Winter 2007 CS 32 Homework 1 Solution...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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 fixed-size 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 // 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....
View Full Document

This note was uploaded on 03/18/2008 for the course CS 32 taught by Professor Davidsmallberg during the Winter '08 term at UCLA.

Page1 / 57

Homework_solutions - Winter 2007 CS 32 Homework 1 Solution...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online