troubleshooting Question

insert implementation

Avatar of chetmunk700
chetmunk700 asked on
C++
30 Comments1 Solution314 ViewsLast Modified:
Ok so ive tried to implement a insert functions for the value at the end of the queue but i keep getting a mismatch error. If someone could check what i have and relay and changes i need to make that would be great. Also i am trying to remove the front item from queue and im not sure how to go about doing this.



#include <iostream>
#include <stdexcept>

using namespace std;

template <typename T>
class node
{
      private:
            node( const T& x, node* p = NULL )
            {
                  data       = x;
                  next      = p;
            }
            T            data;
            node*      next;
      template<typename S> friend class cQueue;
};

template <typename T>
class cQueue
{
      public:
            cQueue()                                                      
            {
                  last = NULL;
                  count = 0;
            }

            cQueue( const cQueue& q )                              
            {
                  copyQueue(q);
            }

            virtual ~cQueue()                                          
            {
                  deleteQueue();
            }

            cQueue& operator= ( const cQueue& q )            
            {
                  if ( this != &q )
                  {
                        deleteQueue();
                        copyQueue( q );
                  }
                  return *this;
            }

            size_t size() const                                    
            {
                  return count;
            }

            void insert( const T& v )                              // insert an value at the end of the queue
            {
                  if(last == NULL)
      {
            node<T>* last;
            //last = new node<T>*;
            last->next = last;
      }
      else if(v > last->data)
      {
            node<T>* node2 = new node<T>(v,last->next);
            last->next = node2;
            last = node2;
      }
      else
      {
            node<T>* temp;
            temp = last;
            while (v > temp->next->data)
            {
                  temp = temp->next;
            }
            node<T>* temp2 = new node<T> (v, temp->next);
            temp->next = temp2;
      }
            }

            T remove()                                                      // remove the front item from the queue
            {
                  
                  return last->data;
            }

            const T& front() const                                    // return a reference to the front item
            {
                  return 0;
            }

            void print( ostream& os = cout ) const            // print the contents of the list
            {
                  node<T>* temp = last->next;
            while(temp != last)
            {
                  cout<< temp->data << " ";
                  temp = temp->next;
            }
            cout<< temp->data << endl;
            }

      private:
            void deleteQueue()                                          
            {
                  if ( last != NULL )
                  {
                        node<T>* tmp = last->next;
                        while ( tmp != last )
                        {
                              node<T>* doomed = tmp;
                              tmp = tmp->next;
                              delete doomed;
                        }
                        last = NULL;
                        count = 0;
                        delete tmp;
                  }
            }

            void copyQueue( const cQueue& q )                  
            {
                  last = NULL;
                  count = 0;

                  if ( q.last != NULL )
                  {
                        node<T>* tmp = q.last->next;
                        for ( size_t i = 0; i < q.count; i++ )
                        {
                              insert( tmp->data );
                              tmp = tmp->next;
                        }
                  }
                  count = q.count;
            }

            node<T>*      last;                                          
            size_t            count;                                          
};

template <typename T>                                                
ostream& operator<< ( ostream& os, const cQueue<T>& q )
{
      q.print( os );
      return os;
}


template <typename T>
void passByValue( cQueue<T> q )
{
      q.insert( 40 );
      cout << "queue inside passByValue: " << q << endl;
}

int main()
{

      {
            cQueue<int> q;

            q.insert(10);
            q.insert(20);
            q.insert(30);

            cout << "initial queue: " << q << endl;

            passByValue( q );

            cout << "queue after call to passByValue: " << q << endl;

            cout << "front item: " << q.front() << endl;

            q.remove();

            cout << "front item removed: " << q << endl;

      }
}
ASKER CERTIFIED SOLUTION
phoffricSoftware Engineering and Matlab Analyst

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 30 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 30 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros