Priority_queue<generic>::Priority_queue(Priority_queue & original) { m_size = 0 ; m_root = NULL; m_push = NULL; m_pop = NULL; //create temp Priority_queue<generic> temp; //copy to temp while (!original.empty()) { temp.push(original.top()) ; original.pop(); } //copy temp to new and old while (!temp.empty()) { original.push(temp.top()) ; push(temp.top()) ; temp.pop(); } }
Priority_queue<generic> & Priority_queue<generic>:: operator= ( Priority_queue & original) { clear(); //create temp Priority_queue<generic> temp; //copy to temp while (!original.empty()) { temp.push(original.top()) ; original.pop(); } //copy temp to new and old while (!temp.empty()) { original.push(temp.top()) ; push(temp.top()) ; temp.pop(); } return *this ; }
int main() { const int testcases = 1000; Priority_queue<Int> mypq; priority_queue<Int> stdpq; int myoutput, stdoutput; srand( time(NULL) ); for(int i=0; i<testcases; i++) { myoutput = -1; stdoutput = -1; int cmd = rand()%5; Int temp( rand()%testcases ); // if( i<testcases/2 ) // cmd = 3; // else if( i%2==0 ) // cmd = 2; // else // cmd = 4; switch(cmd) { case 0: myoutput = mypq.empty(); stdoutput = stdpq.empty(); break; case 1: myoutput = mypq.size(); stdoutput = stdpq.size(); break; case 2: if( stdpq.size()>0 ) { myoutput = mypq.top().value(); stdoutput = trans(stdpq.top()); } break; case 3: mypq.push(temp); stdpq.push(temp); break; default: if( stdpq.size()>0 ) { mypq.pop(); stdpq.pop(); } break; } printf("(%d,%d) with operation %d\n",myoutput,stdoutput,cmd); if( myoutput!=stdoutput ) { printf("fail\n"); return 0; } } printf("success\n"); return 0; }
int main() { Priority_queue<int> pq; assert(pq.size() == 0); assert(pq.empty()); pq.push(10); assert(pq.top() == 10); pq.push(20); assert(pq.top() == 20); pq.push(30); assert(pq.top() == 30); pq.push(40); assert(pq.top() == 40); pq.push(50); assert(pq.top() == 50); pq.push(5); assert(pq.top() == 50); pq.pop(); assert(pq.top() == 40); pq.pop(); assert(pq.top() == 30); pq.pop(); assert(pq.top() == 20); pq.pop(); assert(pq.top() == 10); pq.pop(); assert(pq.top() == 5); pq.pop(); assert(pq.size() == 0); Priority_queue<int> pq2; pq2.push(30); pq2.push(11); pq2.push(7); pq2.pop(); assert(pq2.top() == 11); pq2.pop(); assert(pq2.top() == 7); pq2.pop(); assert(pq2.empty()); cout << "All tests passed." << endl; }