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