#ifndef _fifo_tmpl #define _fifo_tmpl /* This file implements a FIFO structure for arbitrary data types */ /* Usage: new Fifo; Fifo->Push(ITEM *); fifosize = Fifo->Size(); itemptr = Fifo->Pull(); */ template class Fifo { public: Fifo() { head = NULL; tail = NULL; size = 0; } virtual ~Fifo() { while ( Pull() ) ; } virtual void Push(ITEM *item) { if ( tail ) { tail->next = new element; tail = tail->next; } else { tail = new element; if ( ! head ) head = tail; } tail->next = NULL; tail->item = item; ++size; return; } virtual ITEM *Peek(void) { return(head ? head->item : (ITEM *)0); } virtual ITEM *Pull(void) { element *tmp; ITEM *item; /* Don't pull on a tired head */ if ( ! head ) return(NULL); /* Pull off the top! */ tmp = head; if ( head == tail ) { head = tail = NULL; } else { head = head->next; } item = tmp->item; delete tmp; --size; return(item); } virtual int Size(void) { return(size); } protected: int size; typedef struct element { ITEM *item; struct element *next; } element; element *head, *tail; }; #endif /* _fifo_tmpl */