Using ADT interfaces

When using a data type developed by someone else, you rarely find it does exactly what you need. As a result, you have to come up with creative ways to fill in the gaps.

The problems below are a few examples of supplied interfaces and problems to be overcome (and here are some potential solutions).


Reversing a list when you can't edit the class
Suppose you are given the class definition below:
class StrList {
   public:
      StrList();  
     ~StrList();  
      long getListSize();
      bool insertAtBack(string s);
      bool insertAtFront(string s);
      bool removeFromBack(string &s);
      bool removeFromFront(string &s);
   private:
      // who cares, we can't access it anyway ;-)
};
Try to implement a stand-alone function to reverse the contents of a linked list, e.g.:
bool Reverse(StrList &L);


Reversing a queue when you can't edit the class
Suppose you are given the class definition below:
class Queue {
   public:
      Queue();
     ~Queue();
      bool enqueue(string s);
      bool dequeue(string &s);
      long getQsize();
   private:
      // who cares, we can't access it anyway ;-)
};
Try to implement a stand-alone function to reverse the contents of a queue, e.g.:
bool Reverse(Queue &Q);


Reversing a list when you can edit the data directly
Suppose you are given pointers to the front and back nodes of a linked list, where the data type for a node is as follows:
struct Node {
   string info;
   Node *next, *prev;
};
Try to implement a stand-alone function to reverse the order of the data in the linked list, e.g.:
bool Reverse(Node *front, Node *back);


Removing a queue item when you cannot edit the class

Suppose you are given the class definition below:

class Queue {
   public:
      Queue();
     ~Queue();
      bool enqueue(string s);
      bool dequeue(string &s);
      long getSize();
   private:
      // who cares, we can't access it anyway ;-)
};

Try to implement a stand-alone function to remove a particular string, s, from a queue but leave the rest of the organized as before:
bool Remove(Queue &Q, string s);


Command line arguments: just some practice

Try writing a complete C++ program that accepts command line arguments: printing all the command line arguments a user provided that begin with the '-' character. For example, if the user command was
./myprog -r -blah foo
then the program should display "-r" and "-blah".


File I/O: just some practice

Try writing a complete C++ program that accepts a filename as a command line argument, tries to open the file for input, counts the total number of lines in the file, counts the total number of whitespace characters in the file, and determines the length of the longest line in the file. (Remember to close the file afterwards.)