void deleteNegative(queue <int> &q)
{
      queue <int> temp;
      
      if (!q.empty())
      {
           int x = q.front();
           
           if (x >= 0)
           {
            temp.push(x);
            q.pop();
           }
           else
           {
            q.pop();
           }    
      }
      
      while(!temp.empty())
      {
       q.push(temp.front());
       temp.pop();
      }

}

just afunction that returns a queue without the negatives in the same order. unsure where ive gone wrong

Recommended Answers

All 8 Replies

void deleteNegative(queue <int> &q)
{
      queue <int> temp;
      
      if (!q.empty())
      {
           int x = q.front();
           
           if (x >= 0)
           {
            temp.push(x);
            q.pop();
           }
           else
           {
            q.pop();
           }    
      }
      
      while(!temp.empty())
      {
       cout << temp.front() << " ";
       temp.pop();
      }
}

updated just outputs first number of queue

Member Avatar for iamthwee

why have you got a temp, surely you would just pop anything that is negative and that's that.

why have you got a temp, surely you would just pop anything that is negative and that's that.

void deleteNegative(queue <int> &q)

doest the ampersand make a difference?

void deleteNegative(queue <int> &q)
{

      while (!q.empty())
      {
           int x = q.front();
           
           if (x < 0)
           {
           q.pop();
           
           }
           else
           {
            cout << q.front() << " ";
            q.pop();
           }    
      }
      
}

got it working but the "&" is still confusing me

Member Avatar for iamthwee

& is pass by reference is that what you require?

& is pass by reference is that what you require?

yeh im wondering what difference will that fact that its pass by reference make to the code in the deleteNegative function?

Member Avatar for iamthwee

Well do you know what pass by reference means, if not read up on it.

Lol .!! late post.

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.