Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- using namespace std;
- void printqueue(queue<int> firstqueue,queue<int> secondqueue){
- //Printing our queues in their order that they were in myqueue.
- //If the hw required them to be in their own order in two rows, thats simpler code.
- while (!firstqueue.empty()){ //Second queue will always be either equal or smaller
- cout << firstqueue.front() << ' ';
- firstqueue.pop();
- if (!secondqueue.empty()){ //Boolean check of possible odd numbered size queue
- cout << secondqueue.front() << ' ';
- secondqueue.pop();
- }
- }
- cout << endl; //Make it look nice
- return;
- }
- void splitqueue(queue<int> myqueue){
- queue<int> firstqueue; //Declaring queues.
- queue<int> secondqueue;
- while (myqueue.size() != 0){ //Push front, pop front (alternating between queues)
- int front = myqueue.front();
- firstqueue.push(front);
- myqueue.pop();
- if (!myqueue.empty()){ //Boolean check of possible odd numbered size queue
- front = myqueue.front();
- secondqueue.push(front);
- myqueue.pop();
- }
- }
- printqueue(firstqueue,secondqueue); //Printing
- }
- int main() {
- queue<int> myqueue; //Basic setup and test value code.
- for (int i = 0; i < 11; i++)
- myqueue.push(i);
- splitqueue(myqueue); //Calling our function
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement