Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1.  
  2.  
  3. #include <iostream>
  4. #include <queue>
  5. using namespace std;
  6.  
  7. void printqueue(queue<int> firstqueue,queue<int> secondqueue){
  8. //Printing our queues in their order that they were in myqueue.
  9. //If the hw required them to be in their own order in two rows, thats simpler code.
  10.  
  11. while (!firstqueue.empty()){ //Second queue will always be either equal or smaller
  12. cout << firstqueue.front() << ' ';
  13. firstqueue.pop();
  14. if (!secondqueue.empty()){ //Boolean check of possible odd numbered size queue
  15. cout << secondqueue.front() << ' ';
  16. secondqueue.pop();
  17. }
  18.  
  19. }
  20. cout << endl; //Make it look nice
  21. return;
  22. }
  23.  
  24. void splitqueue(queue<int> myqueue){
  25. queue<int> firstqueue; //Declaring queues.
  26. queue<int> secondqueue;
  27. while (myqueue.size() != 0){ //Push front, pop front (alternating between queues)
  28. int front = myqueue.front();
  29. firstqueue.push(front);
  30. myqueue.pop();
  31. if (!myqueue.empty()){ //Boolean check of possible odd numbered size queue
  32. front = myqueue.front();
  33. secondqueue.push(front);
  34. myqueue.pop();
  35. }
  36. }
  37.  
  38. printqueue(firstqueue,secondqueue); //Printing
  39.  
  40. }
  41.  
  42.  
  43.  
  44. int main() {
  45. queue<int> myqueue; //Basic setup and test value code.
  46. for (int i = 0; i < 11; i++)
  47. myqueue.push(i);
  48. splitqueue(myqueue); //Calling our function
  49.  
  50. return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement