We help IT Professionals succeed at work.
Get Started

Help on insert function.

chetmunk700
chetmunk700 asked
on
486 Views
Last Modified: 2012-05-11
Ok so ive tried to implement a insert fuction using nodes but i am having trouble getting it to compile. I have also tried to implement a remove() fuction using node T and just curious if that is how im supposed to do that.
#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()									// default constructor
		{
			last = NULL;
			count = 0;
		}

		cQueue( const cQueue& q )					// copy constructor
		{
			copyQueue(q);
		}

		virtual ~cQueue()							// queue destructor
		{
			deleteQueue();
		}

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

		size_t size() const							// return the size of the queue
		{
			return count;
		}

		void insert( const T& v )					// insert an value at the end of the queue
		{
			node<T>* last = tmp;
					
			insert( tmp->data );
					tmp = tmp->next;

		}

		T remove()									// remove the front item from the queue
		{
			delete node<T>* front;
		}

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

		void print( ostream& os = cout ) const		// print the contents of the list
		{
			
		}

	private:
		void deleteQueue()							// delete nodes in "this" queue
		{
			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 )			// copy q queue into "this" queue ("this" queue must be empty)
		{
			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;							// a pointer to the last node in the list
		size_t		count;							// queue length
};

template <typename T>								// overloaded ostream operator<<
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;

	}
}

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 11 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE