gladi

Queue

Sep 28th, 2011
569
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <sstream>
  4. using namespace std;
  5. const int initialSize = 5;
  6.  
  7.  
  8. struct Order {
  9.  
  10.  
  11. string sandwich;
  12. string customerName;
  13. int orderNbr;
  14. bool fries;
  15. };
  16.  
  17.  
  18. class SandwichQueue{
  19.  
  20. public:
  21.  
  22. SandwichQueue();
  23.  
  24. //Interface for queue
  25. Order pop();
  26. void push(const Order& sandwich);
  27. void expandQ();
  28. bool isEmpty();
  29. int size();
  30.  
  31. private:
  32.  
  33. //qSize is size of array, not amount stored in queue
  34. int qSize;
  35. Order* orderQ;
  36. int front;
  37. int back;
  38.  
  39. };
  40.  
  41. int main()
  42. {
  43.  
  44. SandwichQueue sQ;
  45.  
  46. Order testCases[15];
  47. Order temp;
  48.  
  49. string s = "sandwich";
  50. string c = "customer";
  51.  
  52. for(int i = 0; i < 15; i++)
  53. {
  54.  
  55. testCases[i].sandwich = s;
  56. testCases[i].customerName = c;
  57. testCases[i].orderNbr = i;
  58. testCases[i].fries = i%2;
  59. }
  60.  
  61.  
  62.  
  63. //a few tests...
  64.  
  65. //add one
  66. sQ.push(testCases[0]);
  67.  
  68. //remove one
  69. testCases[0] = sQ.pop();
  70.  
  71.  
  72. //Next test wrap around
  73. for(int i = 0; i < 3; i++)
  74. {
  75. sQ.push(testCases[i]);
  76. sQ.push(testCases[i]);
  77. temp = sQ.pop();
  78.  
  79. }
  80.  
  81. //test undrflow
  82. for(int i = 0; i < 11; i++)
  83. {
  84. if(!sQ.isEmpty())
  85. temp = sQ.pop();
  86. else
  87. cout << "Q empty - no more sandwiches" << endl;
  88. }
  89.  
  90. //test overflow
  91. for(int i = 0; i < 15; i++)
  92. {
  93. sQ.push(testCases[i]);
  94. }
  95.  
  96. }
  97.  
  98. SandwichQueue::SandwichQueue()
  99. {
  100. qSize = initialSize;
  101. orderQ = new Order[initialSize];
  102. back = qSize - 1;
  103. front = 0;
  104. }
  105.  
  106. bool SandwichQueue::isEmpty()
  107. {
  108. if (size() == 0)
  109. return true;
  110. else
  111. return false;
  112. }
  113.  
  114.  
  115. int SandwichQueue::size()
  116. {
  117.  
  118. return (back - front + 1 + qSize) % qSize;
  119.  
  120. }
  121.  
  122. //function to pop
  123. Order SandwichQueue::pop()
  124. {
  125.  
  126.  
  127.  
  128.  
  129. }
  130.  
  131. //push an element, make sure it is not full, if it is call expand funciton
  132. void SandwichQueue::push(const Order& sw)
  133. {
  134.  
  135.  
  136.  
  137. }
  138.  
  139. //Double the queue size, copy the values, and reset back and front
  140. void SandwichQueue::expandQ()
  141. {
  142.  
  143.  
  144.  
  145. }
  146.  
  147.  
  148.  
  149.  
Advertisement
Add Comment
Please, Sign In to add comment