Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. #include <cassert>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. struct Qnode{
  9.     string val;
  10.     Qnode* next;
  11. };
  12.  
  13. struct Queue{
  14.     Qnode *first;
  15.     Qnode *last;
  16. };
  17.  
  18. struct PQnode{
  19.     int priority;
  20.     Queue q;
  21.     PQnode* next;
  22.  
  23. };
  24.  
  25. typedef PQnode* PQ;
  26.  
  27. void initPQ (PQ& pq) {
  28.     pq = NULL;
  29. }
  30.  
  31. bool isEmptyPQ (const PQ& pq){
  32.     return NULL == pq;
  33. }
  34.  
  35. void enterPQ (PQ& pq, string val, int priority) {
  36.     Qnode *newQnode = new Qnode;
  37.     newQnode->val = val;
  38.     newQnode->next = NULL;
  39.     if(isEmptyPQ(pq) || priority < pq->priority){
  40.         PQnode *newpq = new PQnode;
  41.         newpq->priority = priority;
  42.         newpq->next = NULL;
  43.         newpq->q.first = newQnode;
  44.         newpq->q.last = newQnode;
  45.         newpq->next = pq;
  46.         pq = newpq;
  47.     }else{
  48.         PQnode *temp = pq;
  49.         while(temp->next!=NULL && temp->next->priority <= priority){
  50.             temp = temp->next;
  51.         }
  52.         if(temp->priority == priority){
  53.             temp->q.last->next = newQnode;
  54.             temp->q.last = newQnode;
  55.         }else{
  56.             PQnode *newpq = new PQnode;
  57.             newpq->priority = priority;
  58.             newpq->next = NULL;
  59.             newpq->q.first = newQnode;
  60.             newpq->q.last = newQnode;
  61.             if(temp->next != NULL){
  62.                 newpq->next = temp->next;
  63.             }
  64.             temp->next = newpq;
  65.         }
  66.     }
  67. }
  68.  
  69. string firstPQ (const PQ& pq){
  70.     assert (!isEmptyPQ(pq));
  71.     return(pq->q.first->val);
  72. }
  73.  
  74. void leavePQ (PQ& pq) {
  75.     assert (!isEmptyPQ(pq));
  76.     if(pq->q.first->next == NULL){
  77.         PQnode *temppq = pq;
  78.         pq=pq->next;
  79.         delete temppq->q.first;
  80.         delete temppq;
  81.     }else{
  82.         Qnode *temp = pq->q.first;
  83.         pq->q.first = pq->q.first->next;
  84.         delete temp;
  85. }
  86.  
  87. int sizePQ (const PQ& pq) {
  88.     if (isEmptyPQ(pq)) {
  89.         return 0;
  90.     }
  91.     int numPQ = 0;
  92.     PQnode *temp = pq;
  93.     while(temp!= NULL){
  94.         Qnode *tempQ = temp->q.first;
  95.         //cout << "Blah: " << tempQ->val << endl;
  96.         while(tempQ!=NULL){
  97.             //cout << tempQ->val << endl;
  98.             numPQ++;
  99.             tempQ = tempQ->next;
  100.         }
  101.         temp = temp->next;
  102.     }
  103.     return numPQ;
  104. }
  105.  
  106. int sizeByPriority (const PQ& pq, int priority) {
  107.     if (isEmptyPQ(pq)) {
  108.         return 0;
  109.     }
  110.     int numPQ = 0;
  111.     PQnode *temp = pq;
  112.     while(temp->next!=NULL && temp->next->priority <= priority){
  113.         temp = temp->next;
  114.     }
  115.     if (NULL == temp || temp->priority != priority) {
  116.         return 0;
  117.     }
  118.     Qnode *tempQ = temp->q.first;
  119.     while(tempQ!=NULL){
  120.         numPQ ++;
  121.         tempQ = tempQ->next;
  122.     }
  123.     return numPQ;
  124. }
  125.  
  126. int numPriorities (const PQ& pq) {
  127.     if (isEmptyPQ(pq)) {
  128.         return 0;
  129.     }
  130.     int numPQ = 0;
  131.     PQnode *temp = pq;
  132.     while(temp!=NULL){
  133.         numPQ ++;
  134.         temp = temp->next;
  135.     }
  136.     return numPQ;
  137. }
  138.  
  139. void CGprint(const PQ& pq){
  140.     PQ temp=pq;
  141.     while(temp!=NULL){
  142.         //cout<<"priority "<<temp->priority<<". Priority size: "<<sizeByPriority(pq, temp->priority)<<" :"<<endl;
  143.         Qnode* iter=temp->q.first;
  144.         while(iter!=NULL){
  145.             cout<<iter->val<<endl;
  146.             iter=iter->next;
  147.         }
  148.         temp=temp->next;
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement