C++ 队列类

queue.h

  1. #ifndef PRO1_QUEUE_H
  2. #define PRO1_QUEUE_H
  3. class Customer {
  4. private:
  5. long arrive;
  6. int processtime;
  7. public:
  8. Customer(){ arrive = processtime = 0; }
  9. void set(long when);
  10. long when()const { return arrive; }
  11. int ptime()const { return processtime; }
  12. };
  13. typedef Customer Item;
  14. class Queue{
  15. private:
  16. struct Node {Item item; struct Node* next;};
  17. enum {Q_SIZE = 10};
  18. Node* front;
  19. Node* rear;
  20. int items;
  21. const int qsize;
  22. Queue(const Queue & q): qsize(0){}
  23. Queue &operator=(const Queue & q){ return *this;}
  24. public:
  25. Queue(int qs = Q_SIZE);
  26. ~Queue();
  27. bool isempty()const ;
  28. bool isfull()const ;
  29. int queuecount()const ;
  30. bool enqueue(const Item &item);
  31. bool dequeue(Item &item);
  32. };
  33. #endif //PRO1_QUEUE_H

queue.cpp

  1. #include <cstdlib>
  2. #include "queue.h"
  3. using namespace std;
  4. Queue::Queue(int qs): qsize(qs) {
  5. front = rear = NULL;
  6. items = 0;
  7. }
  8. Queue::~Queue() {
  9. Node * temp;
  10. while (front != NULL){
  11. temp = front;
  12. front = front -> next;
  13. delete temp;
  14. }
  15. }
  16. bool Queue::isempty() const {
  17. return items == 0;
  18. }
  19. bool Queue::isfull() const { return items == qsize; }
  20. int Queue::queuecount() const {
  21. return items;
  22. }
  23. bool Queue::enqueue(const Item &item) {
  24. if (isfull())
  25. return false;
  26. Node* add = new Node;
  27. if (add == NULL)
  28. return false;
  29. add->item = item;
  30. add->next = NULL;
  31. items++;
  32. if (front == NULL)
  33. front = add;
  34. else
  35. rear->next = add;
  36. rear = add;
  37. return true;
  38. }
  39. bool Queue::dequeue(Item &item) {
  40. if (front == NULL)
  41. return false;
  42. item = front->item;
  43. items--;
  44. Node* temp = front;
  45. front = front->next;
  46. delete temp;
  47. if (items == 0)
  48. rear = NULL;
  49. return true;
  50. }
  51. //void Customer::set(long when) {
  52. // processtime = rand()%3 + 1;
  53. // arrive = when;
  54. //}

main.cpp

  1. #include <iostream>
  2. #include <ctime>
  3. #include "module5/queue.h"
  4. using namespace std;
  5. // 队列类
  6. const int MIN_PER_HR = 60;
  7. bool newcustomer(double x);
  8. bool newcustomer(double x){ return rand()*x / RAND_MAX < 1;}
  9. void bank(){
  10. cout << "Case Study:Bank of Heather Automatic Teller\n";
  11. cout << "Enter maximum size of queue: ";
  12. int qs;
  13. cin >> qs;
  14. Queue line(qs);
  15. cout << "Enter the number of simulation hours: ";
  16. int hours;
  17. cin >> hours;
  18. long cyclelimit = MIN_PER_HR * hours;
  19. cout << "Enter the average number of customers per hour: ";
  20. double perhour;
  21. cin >> perhour;
  22. double min_per_cust;
  23. min_per_cust = MIN_PER_HR / perhour;
  24. Item temp;
  25. long turnaways = 0;
  26. long customers = 0;
  27. long served = 0;
  28. long sum_line = 0;
  29. int wait_time = 0;
  30. long line_wait = 0;
  31. for (int cycle = 0; cycle < cyclelimit; cycle ++){
  32. if (newcustomer(min_per_cust)){
  33. if (line.isfull())
  34. turnaways++;
  35. else{
  36. customers++;
  37. temp.set(cycle);
  38. line.enqueue(temp);
  39. }
  40. }
  41. if (wait_time <= 0 && !line.isempty()){
  42. line.dequeue(temp);
  43. wait_time = temp.ptime();
  44. line_wait += cycle = temp.when();
  45. served++;
  46. }
  47. if (wait_time > 0)
  48. wait_time--;
  49. sum_line += line.queuecount();
  50. }
  51. if (customers >0){
  52. cout << "customers accepted: " << customers << endl;
  53. cout << "customers served: " << served << endl;
  54. cout << "turnaways: " << turnaways << endl;
  55. cout << "average queue size: ";
  56. cout.setf(ios_base::fixed, ios_base::floatfield);
  57. cout.setf(ios_base::showpoint);
  58. cout << (double)sum_line/cyclelimit << endl;
  59. cout << "average wait time: " << (double)line_wait/served << " minutes\n";
  60. } else
  61. cout << "No customers!\n";
  62. cout << "Done!\n";
  63. }
  64. int main() {
  65. bank();
  66. return 0;
  67. }