home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.bin / SourceCode / OOP_Course / Labs / Stack / Extensions / Queue / Queue.m < prev    next >
Encoding:
Text File  |  1993-01-18  |  1.2 KB  |  53 lines

  1. /*Objective_C Implementation of the Queue Class: Queue.m*/
  2.  
  3. #import <stdio.h>
  4. #import "Queue.h"
  5.  
  6. @implementation Queue
  7.  
  8. //Initialize a Queue instance of floating point elements
  9. -init
  10. {
  11.     return [self initCount:0 elementSize:sizeof(float) description:"f"];
  12.                                  //return id of newly created Queue instance
  13. }
  14.                                                                        
  15. //Add an element to the queue
  16. -enQueue: (float)aNumber
  17. {
  18.     [self  addElement: &aNumber];    
  19.     return self;
  20. }
  21.  
  22. //Remove an element from the Queue
  23. -deQueue: (float *)aNumber
  24. {
  25.     if (numElements != 0) {
  26.         *aNumber = *((float *)[self elementAt:0]);
  27.          [self removeElementAt:0];    /* This also automatically shifts all the */
  28.                   /* elements between index and the end of  */
  29.                 /* the queue to close the gap*/
  30.     } else
  31.         *aNumber = 0.0;       //Empty queue returns zero
  32.     
  33.     return self;
  34. }
  35.  
  36. //Print the elements in the queue
  37. -printQueue
  38. {
  39.     int i=0;
  40.     
  41.     if (numElements == 0) 
  42.         printf("Queue is empty.\n");
  43.     else {
  44.         printf("%i elements in queue, last in to first:\n", numElements); 
  45.                     //numElements is an instance variable inherited from the Storage class
  46.         for(i=(numElements-1);i>=0;--i)
  47.             printf("    %f\n", *((float *)[self elementAt:i])); 
  48.      }
  49.     return self;
  50. }
  51.  
  52. @end
  53.