20-CS-122-001 Computer Science II Spring 2012
Array Implementation With Class List

Virtual functions, classes, inheritance, lists, queues, stacks, applications

Prev      Next      Lectures      Makefile      uselist.2.cc      list.2.h      list.2.cc      power point

#include <iostream>
#include "list.2.h"
using namespace std;

// Problems solved:
//  1. Easy to use list services provided by List class
//  2. The List class can be used for any type of object
//  3. The user of the List class never worries about array sizes
//     or whether an array is even used!!
//  4. The user of the List class sees no ** or &
//  5. Funky code hidden within the List class!!

// Requires:
//  1. implementation of void (*)(void*) display function which prints
//     a listed object to the console
//  2. implementation of bool (*)(void*, void*) function which outputs
//     true if and only if two objects being compared are considered
//     "equal" in some respect.

// Note: From here on the '+' and '-' operators are overloaded so as not
// to destroy or modify the operands.  To do this, copies of objects are 
// made using a 'copy' method.  This space can be reclaimed.  The list.2.cc 
// and list.2.h files linked to above show how this might be accomplished, 
// although the result is not perfect.  Deletion of the copies is not done 
// in further examples in this string.

void intdisp(void *object) {  cout << *(int*)object << " ";  }
bool intfind(void *obj1, void *obj2) { return *(int*)obj1 == *(int*)obj2; }

int main () {
   List lst(intfind, intdisp);
   cout << lst << "\n";
   lst += new int(9);
   lst += new int(4);
   lst += new int(23);
   lst += new int(2);
   cout << lst << "\n";
   lst -= new int(23);
   cout << lst << "\n";
   List m(intfind, intdisp);
   cout << m + new int(33) + new int (42) + new int(26) << "\n";
   cout << "m=" << m << "\n";
   m = m + new int(67) + new int(99) + new int(23);
   cout << "m=" << m << "\n";
}

//Problems:
//  1. with all that copying maybe this is a tad slow!!!