20-CS-122-001 Computer Science II Spring 2012
Queue Class With Function Pointers

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

Prev      Next      Lectures     q.2.cc     queue.2.h     queue.2.cc

#include <iostream>
using namespace std;

void intDisplay (ostream & out, void *object) {
   out << *(int *)object << " "; 
}

class Queue;

class Cell {
friend ostream & operator<<(ostream & out, Queue &lst);
friend class Queue;
   Cell *next; void *object;
   Cell (void *o, Cell *n) { next = n; object = o; }
};

class Queue {
friend ostream & operator<<(ostream & out, Queue &que);
   Cell *tail;
   void (*disp)(ostream &, void *);

public:
   Queue() { tail = NULL; disp = intDisplay; }

   Queue(void (*d)(ostream &, void *)) { tail = NULL; disp = d; }

   void enqueue(void *t) {
      if (t == NULL) return;
      if (tail == NULL) {
         tail = new Cell(t, NULL);
         tail->next = tail;
      } else {
         Cell *h = new Cell(t, tail->next);
         tail->next = h;
         tail = h;
      }
   }

   void *dequeue() {
      if (tail == NULL) return NULL;
      Cell *ptr = tail->next;
      void *t = ptr->object;
      if (ptr != tail) tail->next = ptr->next; else tail = NULL;
      delete ptr;
      return t;
   }

   bool isEmpty() {  return tail == NULL;  }
};

ostream & operator<<(ostream & out, Queue &que) {
   Cell *t;
   if (que.isEmpty()) { 
      out << "(empty)";
   } else {
      for (Cell *ptr=que.tail->next ; ptr != que.tail ; ptr=ptr->next)
         que.disp(out, ptr->object);
      que.disp(out, que.tail->object);
   }   
   out << "\n";
   return out;
}

class Object {
friend ostream & operator<<(ostream & out, Object & obj);
   char *str;

public:
   Object (char *s) { 
      str = new char[strlen(s)+1];
      strcpy(str,s);
   }
   
   char *getString() { return str; }
};

void objectDisplay (ostream & out, void *object) {
   out << ((Object *)object)->getString() << " ";
}

ostream & operator<<(ostream & out, Object & obj) {
   out << obj.getString() << " ";
   return out;
}

int main () {
   Queue q;
   cout << q;
   q.enqueue(new int(3));
   cout << q;
   q.enqueue(new int(6));
   cout << q;
   q.enqueue(new int(9));
   q.enqueue(new int(12));
   q.enqueue(new int(15));
   cout << q;
   int result = *(int*)q.dequeue();
   cout << "-----------\n";
   cout << "Popped Result: " << result << "\nQueue: " << q << "\n";
   q.enqueue(new int(18));
   cout << q;
   cout << "-----------\n";
   result = *(int *)q.dequeue();
   cout << "Popped Result: " << result << "\n";
   result = *(int *)q.dequeue();
   cout << "Popped Result: " << result << "\nQueue: " << q << "\n";
   cout << "==============\n";

   Queue qq(objectDisplay);
   qq.enqueue(new Object("Cincinnati"));
   qq.enqueue(new Object("Chicago"));
   qq.enqueue(new Object("Cleveland"));
   qq.enqueue(new Object("Columbus"));
   cout << qq;
   Object *obj = (Object *)qq.dequeue();
   cout << "Popped Result: " << *obj << "\nQueue: " << qq << "\n";   
}