wheelsmanx

queue

Nov 27th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <vector>
  4. // the problem with this program is in the stackTop not with the rest of it
  5. using namespace std;
  6.  
  7. template <typename T>
  8. class stackNode {
  9. public:
  10. T data;
  11. stackNode<T> *nextNode = nullptr;
  12. };
  13.  
  14. template <typename T>
  15. class stackTop {
  16. public:
  17. stackNode<T> *top, *bottom;
  18. int sizeOfList = 0;
  19. stackTop() {
  20. bottom = nullptr;
  21. top = nullptr;
  22. }
  23.  
  24. void display()
  25. {
  26. stackNode<T> *temp = new stackNode<T>;
  27. temp = bottom;
  28. while (temp != NULL)
  29. {
  30. cout << temp->data << endl;
  31. temp = temp->nextNode;
  32. }
  33. }
  34. void addNode(T userData) {
  35. stackNode<T> *tempNode = new stackNode<T>;
  36. tempNode->data = userData;
  37. tempNode->nextNode = nullptr;
  38. if (bottom == nullptr) {
  39. bottom = tempNode;
  40. top = tempNode;
  41. }
  42. else {
  43. bottom->nextNode = tempNode;
  44. bottom = tempNode;
  45. }
  46. sizeOfList++;
  47. }
  48.  
  49. void pop()
  50. {
  51. sizeOfList--;
  52. stackNode<T> *current = new stackNode<T>;
  53. stackNode<T> *previous = new stackNode<T>;
  54. current = top;
  55. while (current->nextNode != NULL)
  56. {
  57. previous = current;
  58. current = current->nextNode;
  59. }
  60. top = previous;
  61. previous->nextNode = nullptr;
  62. delete current;
  63. }
  64. T last() {
  65. stackNode<T> *temp = new stackNode<T>;
  66. temp = top;
  67. T returnObject = temp->data;
  68. return returnObject;
  69. }
  70. };
  71.  
  72. struct personInLine {
  73. public:
  74. int timeInLine;
  75. int timeLeft;
  76. int ID;
  77. };
  78. stackTop<personInLine> createPILVector(int numberOfPeople) {
  79. stackTop<personInLine> returnObject;
  80. int counter = 0;
  81. personInLine tempPerson;
  82. for (int i = 0; i < numberOfPeople; i++) {
  83. tempPerson.timeInLine = rand() % 4 + 1;
  84. cout << "[" << tempPerson.timeInLine << "]" << endl;
  85. tempPerson.timeLeft = tempPerson.timeInLine;
  86. tempPerson.ID = counter++;
  87. returnObject.addNode(tempPerson);
  88. }
  89. return returnObject;
  90. }
  91. struct envVariables {
  92. int time = 0;
  93. int maxTime = 120;
  94. int numberOfPeople = 0;
  95. };
  96.  
  97. struct varPair {
  98. envVariables pairVariables;
  99. stackTop<personInLine> mainList;
  100. };
  101. varPair runTimer(stackTop<personInLine> userInput) {
  102. varPair returnObject;
  103. returnObject.mainList = userInput;
  104. while (returnObject.pairVariables.time < returnObject.pairVariables.maxTime) {
  105. if (returnObject.mainList.sizeOfList > 0) {
  106. returnObject.pairVariables.time = returnObject.pairVariables.time + returnObject.mainList.top->data.timeInLine;
  107. cout << returnObject.mainList.top->data.timeInLine << endl;
  108. returnObject.mainList.pop();
  109. returnObject.pairVariables.numberOfPeople++;
  110.  
  111. }
  112. }
  113.  
  114. return returnObject;
  115. }
  116.  
  117.  
  118. void main(){
  119. varPair main = runTimer(createPILVector(100));
  120. cout << main.pairVariables.time << endl << main.pairVariables.numberOfPeople << endl;
  121. system("pause");
  122. }
Advertisement
Add Comment
Please, Sign In to add comment