I am trying to do a simple queue using linked list. I am failing to perform dequeue because I cant get the logic straight. If there is anyone who could explain to me about the simple logic of this then it would help alot. Thanks. I have also posted the small code that is giving me the problem

bool dequeue(int& new_num)
{
	myPtr tmp_ptr;
	tmp_ptr=head;
	if (isEmpty())
	{
		cout<<"The queue is empty"<<endl;
		return false;
	}
	else
	{
	for(int i=0;i<counter;i++)
	{
		new_num=head->num;
		tmp_ptr=head->link;
		delete head;
		head=tmp_ptr;
	}
	return true;
	}

}

Dequeue is simple. since it is a queue you need head & tail. I don't know what you are trying to do with new_num. So I'll leave it. And it seems you want to delete all nodes in the queue with the dequeue() Actually dequeue deletes just the first node. so a code like this would do.

bool dequeue()
{
      // check if empty
   temp = head ;
   if ( head == tail ) 
     head = tail = NULL ;
   else 
     head = head-> link ;
   delete temp ;
}

Hope it helps:)

You seem to be using dequeue() to empty a queue. Since queues are first on first off, how you empty the queue depends on how you add to it. If you always add at head then you will need to remove from the tail and visa versa.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.