Hello;

steps are correct;
there is no error ;
but when running the prog. this what apear :(its in attach)


this is my code and the main:

template<class Type>
void sortQueue(queueType <Type> & q1)
{
queueType <Type> q2;
queueType <Type>  qresult;
Type min=q1.front();
q2.addQueue(min);
while(!q1.isEmptyQueue())
{
q1.deleteQueue();
while(!q1.isEmptyQueue())
{
if(min>q1.front())
{min=q1.front();
q2.addQueue(q1.front());
q1.deleteQueue();
}
else
{ q2.addQueue(q1.front());
q1.deleteQueue();
}
qresult.addQueue(min);
while(!q2.isEmptyQueue())
{
 if(q2.front()==min)
  q2.deleteQueue();
 else
 {
  q1.addQueue(q2.front());
   q2.deleteQueue();
 }
}
min=q1.front();
q2.addQueue(min);
}
}
}
int main()
{
queueType <int> myQ ;
initializeQueue();
myQ.addQueue(15);
myQ.addQueue(9);
myQ.addQueue(4);
myQ.addQueue(3);
myQ.addQueue(1);
myQ.addQueue(2);
sortQueue(myQ);
while(!myQ.isEmptyQueue())
{
 cout<<myQ.front()<<" ";
 myQ.deleteQueue();
}
return 0;
}

this is :

Can you post the definition of queueType? It's hard to test when the code won't compile.

#include <iostream>
#include <cassert>
using namespace std;
template<class Type>
class queueType
{
public:
    const queueType<Type>& operator=(const queueType<Type>&); 
        // overload the assignment operator
    bool isEmptyQueue();
  //Function to determine if the queue is empty.
  //Postcondition: returns true if the queue is empty;
  //               otherwise, it returns false.
    bool isFullQueue();
  //Function to determine if the queue is full.
  //Postcondition: returns true if the queue is full;
  //               otherwise, it returns false.
    void initializeQueue();
  //Function to initialize the queue to an empty state.
  //Postcondition: count = 0, queueFront = 0;
  //               queueRear = maxQueueSize - 1;
    void destroyQueue();
  //Function to remove all the elements from the queue.
  //Postcondition: count = 0, queueFront = 0;
  //               queueRear = maxQueueSize - 1;
    Type front();
  //Function to returns the first element of the queue.
  //Precondition: The queue exists and is not empty
   //Postcondition: If queue is empty, program terminates;
   //               otherwise the first element of the queue
   //               is returned  
    Type back();
  //Function to returns the last element of the queue.
  //Precondition: The queue exists and is not empty
   //Postcondition: If the queue is empty, program terminates;
   //               otherwise the last element of the queue
  //               is returned
   void addQueue(const Type& queueElement);
  //Function to add queueElement to the queue.
  //Precondition: The queue exists and is not full.
  //Postcondition: The queue is changed and queueElement
     //               is added to the queue.
 void deleteQueue();
  //Function to remove the first element of the queue.
  //Precondition: The queue exists and is not empty.
  //Postcondition: The queue is changed and the first 
  //               element is removed from the queue.
    queueType(int queueSize = 100); 
  //constructor
    queueType(const queueType<Type>& otherQueue); 
   // copy constructor
    ~queueType(); 
   //destructor
private:
    int maxQueueSize;
    int count;
    int queueFront;
    int queueRear;
    Type *list;   //pointer to the array that holds 
        //the queue elements 
};

template<class Type>
void queueType<Type>::initializeQueue()
{
 queueFront = 0;
    queueRear = maxQueueSize - 1;
 count = 0;
}

template<class Type>
void queueType<Type>::destroyQueue()
{
 queueFront = 0;
    queueRear = maxQueueSize - 1;
 count = 0;
}

template<class Type>
bool queueType<Type>::isEmptyQueue()
{
   return(count == 0);
}
template<class Type>
bool queueType<Type>::isFullQueue()/////100
{
   return(count == maxQueueSize);
}

template<class Type>
void queueType<Type>::addQueue(const Type& newElement)
{
   if(!isFullQueue())
   {   
  queueRear = (queueRear + 1) % maxQueueSize; //use mod operator 
       //to advance queueRear because 
        //the array is circular
     count++;
     list[queueRear] = newElement;
   }
   else
    cerr<<"Cannot add to a full queue"<<endl; 
}

template<class Type>
Type queueType<Type>::front()
{
   assert(!isEmptyQueue());
   return list[queueFront]; 
}

template<class Type>
Type queueType<Type>::back()
{
     assert(!isEmptyQueue());
     return list[queueRear];
}

template<class Type>
void queueType<Type>::deleteQueue()
{
   if(!isEmptyQueue())
   {   
  count--;
     queueFront = (queueFront + 1) % maxQueueSize; //use the mod 
      //operator to advance queueFront 
       //because the array is circular 
   }
   else
  cerr<<"Cannot remove from an empty queue"<<endl;
}

//constructor
template<class Type>
queueType<Type>::queueType(int queueSize)   
{
    if(queueSize <= 0)
    {
  cerr<<"Size of the array to hold the queue must "
   <<"be positive."<<endl;
  cerr<<"Creating an array of size 100."<<endl;
  maxQueueSize = 100;
    }
    else
     maxQueueSize = queueSize;  //set maxQueueSize to queueSize
    queueFront = 0;          //initialize queueFront
    queueRear = maxQueueSize - 1;     //initialize queueRear
    count = 0;
    list = new Type[maxQueueSize];  //create the array to
                //hold the queue elements
 assert(list != NULL);
}

template<class Type>
queueType<Type>::~queueType()   //destructor
{
   delete [] list;
}
template<class Type>
const queueType<Type>& queueType<Type>::operator=
                    (const queueType<Type>& otherQueue)
{
 cout<<"Write the definition of the function "
  <<"to overload the assignment operator"<<endl;
}
template<class Type>
queueType<Type>::queueType(const queueType<Type>& otherQueue)
{
 cout<<"Write the definition of the copy constructor"<<endl;
}
 
template<class Type>
void sortQueue(queueType <Type> & q1)
{
queueType <Type> q2;
queueType <Type>  qresult;
Type min=q1.front();
q2.addQueue(min);
while(!q1.isEmptyQueue())
{
 q1.deleteQueue();
 while(!q1.isEmptyQueue())
 {
  if(min>q1.front())
  {min=q1.front();
  q2.addQueue(q1.front());
  q1.deleteQueue();
  }
  else
  { q2.addQueue(q1.front());
  q1.deleteQueue();
  }
  qresult.addQueue(min);
  while(!q2.isEmptyQueue())
  {
   if(q2.front()==min)
    q2.deleteQueue();
   else
   {
    q1.addQueue(q2.front());
     q2.deleteQueue();
   }
  }
  min=q1.front();
  q2.addQueue(min);
 }
}
}
int main()
{
  queueType <int> myQ ;
  
  myQ.addQueue(15);
  myQ.addQueue(9);
  myQ.addQueue(4);
  myQ.addQueue(3);
  myQ.addQueue(1);
  myQ.addQueue(2);
  sortQueue(myQ);
  while(!myQ.isEmptyQueue())
  {
   cout<<myQ.front()<<" ";
   myQ.deleteQueue();
  }
  return 0;
}

You drill down to the point where count is 0 in the queue, and then try to pull the front in the highlighted line:

void sortQueue(queueType <Type> & q1)
{
  queueType <Type> q2;
  queueType <Type>  qresult;
  Type min=q1.front();
  q2.addQueue(min);
  while(!q1.isEmptyQueue())
  {
    q1.deleteQueue();
    while(!q1.isEmptyQueue())
    {
      if(min>q1.front())
      {min=q1.front();
      q2.addQueue(q1.front());
      q1.deleteQueue();
      }
      else
      { q2.addQueue(q1.front());
      q1.deleteQueue();
      }
      qresult.addQueue(min);
      while(!q2.isEmptyQueue())
      {
        if(q2.front()==min)
          q2.deleteQueue();
        else
        {
          q1.addQueue(q2.front());
          q2.deleteQueue();
        }
      }
      min=q1.front();
      q2.addQueue(min);
    }
  }
}

I'd recommend doing a paper run of your algorithm with a small queue to find out where your logic is going wrong.

could you explain it better PLZ!

Run your program on paper. Keep track of all of the values, and execute the logic. You'll be able to see where things stop doing what you expect and you can find the flaw.

hi Narue, i track my program on paper as u said, step by step, it's ok logically, but till now i can't realize what's missing or what's going wrong!!!

>i track my program on paper as u said, step by step, it's ok logically
Make sure you perform the steps that the computer does, not the steps that you think the computer does. If you're not hitting the error, clearly your paper run is biased by your assumptions. Take care to be completely objective.

How do I know this? Because I can set a breakpoint on your code and it fails every time. If the flaw is so complete and predictable, an accurate paper run should never succeed.

i did so, and even draw a diagram and follow the steps...can u give me just a hint!!

>can u give me just a hint!!
That was my hint. I don't know what the problem is because I haven't gone to the trouble of figuring out what you're trying to do in the uncommented algorithm and finding the flaw.

>can u give me just a hint!!
That was my hint. I don't know what the problem is because I haven't gone to the trouble of figuring out what you're trying to do in the uncommented algorithm and finding the flaw.

merrcy Narue ;

every thing is oK now :)

>every thing is oK now
What was the problem?

>every thing is oK now
What was the problem?

First of all ; the red statement was a big problem
i really draw a diagram and follow the steps but may be i do not notice to the last element ; when Q1 is Empty we can not take its front and insert it in Q2 :icon_wink:

void sortQueue(queueType <Type> & q1)
{  queueType <Type> q2;  
queueType <Type>  qresult; 
 Type min; 
 while(!q1.isEmptyQueue()) 
 {     min=q1.front();//the correction    
     q2.addQueue(min);  
    q1.deleteQueue();   
 while(!q1.isEmptyQueue())    
{      if(min>q1.front())      
    {min=q1.front();      
    q2.addQueue(q1.front());     
     q1.deleteQueue();      }     
   else      { 
    q2.addQueue(q1.front());     
    q1.deleteQueue();      }     
   qresult.addQueue(min);      
 
while(!q2.isEmptyQueue())      
{        if(q2.front()==min)          
   q2.deleteQueue();       
 else        {          
  q1.addQueue(q2.front());         
   q2.deleteQueue();        }    
  }      
  min=q1.front();     
  q2.addQueue(min);    
}  
}
}

also i forget the result, i have to return the Qresult just by copy its elaement in Q1 .

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.