Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed ESC SR04 TSI
com/queue/queue.cpp
- Committer:
- dereklmc
- Date:
- 2013-06-09
- Revision:
- 30:17297295ce0c
- Parent:
- 16:5f736b955d53
File content as of revision 30:17297295ce0c:
/*************************** queue.cpp ***************************************/ /* */ /* Authers: Oanh Tran, Ling Lei, Sihao Xie, Greg Abdo. */ /* Date: February 23, 2013 */ /* Version: 1.0 */ /* */ /* The queue is used to stack StructureItem in order with a FILO */ /*arrangement. */ /*****************************************************************************/ #include "queue.h" /***************************** constructor ***********************************/ /* Description: */ /*****************************************************************************/ queue::queue() { front = NULL; // Set front to NULL at the start. } /******************************* distructor **********************************/ /* Description: */ /*****************************************************************************/ queue::~queue() { clear(); // Clear the entire queue. } /*****************************************************************************/ /* Description: */ /* Accepts: */ /* Returns: */ /*****************************************************************************/ bool queue::isEmpty() { // Is the queue empty? if( front == NULL ) // Check the front pointer. return true; // Queue is empty, return true. return false; // There is atleast one item, not empty. } /*****************************************************************************/ /* Description: */ /* Accepts: */ /* Returns: */ /*****************************************************************************/ void queue::clear() { // Is the list already empty? if( isEmpty() ) // Check for an empty list. return; // List is empty, don't need to do anything. queueNode * current = front; // Create node to help step through list. queueNode * placeHold = front; // Create node to keep place of delete. // As long as were not at the end, keep stepping to the next node. while( current != NULL) { placeHold = current->next; // Hold where we have to go. delete current; // Delete the node. current = placeHold; // Set current to the next node. } front = NULL; // Reset the front to NULL; } /*****************************************************************************/ /* Description: */ /* Accepts: */ /* Returns: */ /*****************************************************************************/ void queue::add( char * item ) { // Were we passed an invalid object somehow? if( item == NULL ) // Check for NULL return; // If so, return. queueNode * newNode = new queueNode( item ); // Create the new node. if( isEmpty() ) front = newNode; // Set front to the new node. else { queueNode *temp = front; while( temp->next != NULL ) temp = temp->next; temp->next = newNode; } } /*****************************************************************************/ /* Description: */ /* Accepts: */ /* Returns: */ /*****************************************************************************/ char * queue::pop() { // Is the list already empty? if( isEmpty() ) // Check for an empty list. return NULL; // List is empty, don't need to do anything. char* placeHold = front->data; // Keep track of what were returning. delete front; // Delete the front node. front = front->next; // Set front to next object. return placeHold; // return the stuctureItem. } /*****************************************************************************/ /* Description: */ /* Accepts: */ /* Returns: */ /*****************************************************************************/ char * queue::peek() { if( front != NULL ) return front->data; return NULL; }