00001 /* 00002 * Copyright (C) 2009, Edmundo Albuquerque de Souza e Silva. 00003 * 00004 * This file may be distributed under the terms of the Q Public License 00005 * as defined by Trolltech AS of Norway and appearing in the file 00006 * LICENSE.QPL included in the packaging of this file. 00007 * 00008 * THIS FILE IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING 00009 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00010 * PURPOSE. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, 00011 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING 00012 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 00013 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 00014 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 00015 * 00016 */ 00017 00018 #include "tqueue.h" 00019 00020 TQueue::ListNode::ListNode( void * d, ListNode * n ) 00021 { 00022 data = d; 00023 next = n; 00024 } 00025 00026 TQueue::ListNode::~ListNode( void ) 00027 { 00028 if( next ) 00029 delete next; 00030 } 00031 00032 void TQueue::ListNode::SetNext( TQueue::ListNode * n ) 00033 { 00034 next = n; 00035 } 00036 00037 TQueue::ListNode * TQueue::ListNode::GetNext( void ) const 00038 { 00039 return next; 00040 } 00041 00042 void * TQueue::ListNode::GetData( void ) const 00043 { 00044 return data; 00045 } 00046 00047 TQueue::TQueue( ) 00048 { 00049 size = 0; 00050 head = NULL; 00051 tail = NULL; 00052 } 00053 00054 TQueue::~TQueue( ) 00055 { 00056 if( head ) 00057 delete head; 00058 } 00059 00060 void TQueue::Add( void * d ) 00061 { 00062 ListNode * temp_node; 00063 00064 temp_node = new ListNode( d, NULL ); 00065 00066 if( head == NULL ) 00067 { 00068 head = tail = temp_node; 00069 } 00070 else 00071 { 00072 tail->SetNext( temp_node ); 00073 tail = temp_node; 00074 } 00075 size++; 00076 } 00077 00078 void * TQueue::Remove( void ) 00079 { 00080 void * temp_val; 00081 ListNode * temp_node; 00082 00083 if( head == NULL ) 00084 return NULL; 00085 00086 temp_node = head; 00087 head = head->GetNext( ); 00088 00089 temp_val = temp_node->GetData( ); 00090 temp_node->SetNext( NULL ); 00091 delete temp_node; 00092 00093 size--; 00094 00095 return temp_val; 00096 } 00097 00098 void * TQueue::GetFirst( void ) const 00099 { 00100 if( head == NULL ) 00101 return NULL; 00102 return head->GetData( ); 00103 } 00104 00105 int TQueue::GetSize( void ) const 00106 { 00107 return size; 00108 }