Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
queue.h@0:7d55df32c988, 2010-12-04 (annotated)
- Committer:
- linxinda
- Date:
- Sat Dec 04 07:12:45 2010 +0000
- Revision:
- 0:7d55df32c988
HW3B Part1 runAtTrigger
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| linxinda | 0:7d55df32c988 | 1 | #include "mbed.h" |
| linxinda | 0:7d55df32c988 | 2 | Serial pc(USBTX, USBRX); |
| linxinda | 0:7d55df32c988 | 3 | |
| linxinda | 0:7d55df32c988 | 4 | struct _task{ |
| linxinda | 0:7d55df32c988 | 5 | unsigned long long int usec; |
| linxinda | 0:7d55df32c988 | 6 | void (*schedFunc)(); |
| linxinda | 0:7d55df32c988 | 7 | }; |
| linxinda | 0:7d55df32c988 | 8 | |
| linxinda | 0:7d55df32c988 | 9 | |
| linxinda | 0:7d55df32c988 | 10 | class PriorityQueue |
| linxinda | 0:7d55df32c988 | 11 | { |
| linxinda | 0:7d55df32c988 | 12 | private: |
| linxinda | 0:7d55df32c988 | 13 | struct Node |
| linxinda | 0:7d55df32c988 | 14 | { |
| linxinda | 0:7d55df32c988 | 15 | struct Node *Previous; |
| linxinda | 0:7d55df32c988 | 16 | _task Data; |
| linxinda | 0:7d55df32c988 | 17 | struct Node *Next; |
| linxinda | 0:7d55df32c988 | 18 | }Current; |
| linxinda | 0:7d55df32c988 | 19 | struct Node *head; // Pointer to Head |
| linxinda | 0:7d55df32c988 | 20 | struct Node *ptr; |
| linxinda | 0:7d55df32c988 | 21 | // Pointer for travelling through Queue |
| linxinda | 0:7d55df32c988 | 22 | int NumOfNodes; |
| linxinda | 0:7d55df32c988 | 23 | // Keeps track of Number of nodes |
| linxinda | 0:7d55df32c988 | 24 | public: |
| linxinda | 0:7d55df32c988 | 25 | PriorityQueue(void); |
| linxinda | 0:7d55df32c988 | 26 | void Insert(_task * myTask); |
| linxinda | 0:7d55df32c988 | 27 | _task* Delete(); |
| linxinda | 0:7d55df32c988 | 28 | void Display(void); |
| linxinda | 0:7d55df32c988 | 29 | ~PriorityQueue(void); |
| linxinda | 0:7d55df32c988 | 30 | }; |
| linxinda | 0:7d55df32c988 | 31 | |
| linxinda | 0:7d55df32c988 | 32 | PriorityQueue::PriorityQueue(void) |
| linxinda | 0:7d55df32c988 | 33 | { |
| linxinda | 0:7d55df32c988 | 34 | Current.Previous=NULL; |
| linxinda | 0:7d55df32c988 | 35 | Current.Next=NULL; |
| linxinda | 0:7d55df32c988 | 36 | head=&Current; |
| linxinda | 0:7d55df32c988 | 37 | ptr=head; |
| linxinda | 0:7d55df32c988 | 38 | } |
| linxinda | 0:7d55df32c988 | 39 | |
| linxinda | 0:7d55df32c988 | 40 | void PriorityQueue::Insert(_task * myTask) |
| linxinda | 0:7d55df32c988 | 41 | { |
| linxinda | 0:7d55df32c988 | 42 | struct Node *newnode; |
| linxinda | 0:7d55df32c988 | 43 | struct Node *traceback; |
| linxinda | 0:7d55df32c988 | 44 | struct Node *backnode; //the node behind traceback |
| linxinda | 0:7d55df32c988 | 45 | |
| linxinda | 0:7d55df32c988 | 46 | traceback=ptr; |
| linxinda | 0:7d55df32c988 | 47 | newnode=new Node; |
| linxinda | 0:7d55df32c988 | 48 | newnode->Data.usec=myTask->usec; |
| linxinda | 0:7d55df32c988 | 49 | newnode->Data.schedFunc=myTask->schedFunc; |
| linxinda | 0:7d55df32c988 | 50 | if (ptr->Previous==NULL){ |
| linxinda | 0:7d55df32c988 | 51 | head=newnode; |
| linxinda | 0:7d55df32c988 | 52 | } |
| linxinda | 0:7d55df32c988 | 53 | while ((newnode->Data.usec<traceback->Data.usec)&&(traceback->Previous!=NULL)){ |
| linxinda | 0:7d55df32c988 | 54 | traceback=traceback->Previous; |
| linxinda | 0:7d55df32c988 | 55 | } |
| linxinda | 0:7d55df32c988 | 56 | if (traceback->Previous==NULL){ |
| linxinda | 0:7d55df32c988 | 57 | head=newnode; |
| linxinda | 0:7d55df32c988 | 58 | newnode->Next=traceback; |
| linxinda | 0:7d55df32c988 | 59 | } |
| linxinda | 0:7d55df32c988 | 60 | |
| linxinda | 0:7d55df32c988 | 61 | if (traceback->Next!=NULL){ |
| linxinda | 0:7d55df32c988 | 62 | backnode=traceback->Next; |
| linxinda | 0:7d55df32c988 | 63 | newnode->Next=backnode; |
| linxinda | 0:7d55df32c988 | 64 | newnode->Previous=traceback; |
| linxinda | 0:7d55df32c988 | 65 | traceback->Next=newnode; |
| linxinda | 0:7d55df32c988 | 66 | backnode->Previous=newnode; |
| linxinda | 0:7d55df32c988 | 67 | } |
| linxinda | 0:7d55df32c988 | 68 | |
| linxinda | 0:7d55df32c988 | 69 | if(traceback->Next==NULL) |
| linxinda | 0:7d55df32c988 | 70 | { |
| linxinda | 0:7d55df32c988 | 71 | newnode->Next=traceback->Next; |
| linxinda | 0:7d55df32c988 | 72 | traceback->Next=newnode; |
| linxinda | 0:7d55df32c988 | 73 | newnode->Previous=traceback; |
| linxinda | 0:7d55df32c988 | 74 | } |
| linxinda | 0:7d55df32c988 | 75 | |
| linxinda | 0:7d55df32c988 | 76 | while(ptr->Next!=NULL){ |
| linxinda | 0:7d55df32c988 | 77 | ptr=ptr->Next; |
| linxinda | 0:7d55df32c988 | 78 | } |
| linxinda | 0:7d55df32c988 | 79 | NumOfNodes++; |
| linxinda | 0:7d55df32c988 | 80 | } |
| linxinda | 0:7d55df32c988 | 81 | |
| linxinda | 0:7d55df32c988 | 82 | _task * PriorityQueue::Delete() |
| linxinda | 0:7d55df32c988 | 83 | { |
| linxinda | 0:7d55df32c988 | 84 | struct Node *mynode; |
| linxinda | 0:7d55df32c988 | 85 | ptr=head; |
| linxinda | 0:7d55df32c988 | 86 | mynode=head; |
| linxinda | 0:7d55df32c988 | 87 | head=head->Next; |
| linxinda | 0:7d55df32c988 | 88 | return &mynode->Data; |
| linxinda | 0:7d55df32c988 | 89 | } |
| linxinda | 0:7d55df32c988 | 90 | |
| linxinda | 0:7d55df32c988 | 91 | void PriorityQueue::Display(void) |
| linxinda | 0:7d55df32c988 | 92 | { |
| linxinda | 0:7d55df32c988 | 93 | ptr=head; |
| linxinda | 0:7d55df32c988 | 94 | //pc.printf("Priority Queue is as Follows: "); |
| linxinda | 0:7d55df32c988 | 95 | while(ptr!=NULL) |
| linxinda | 0:7d55df32c988 | 96 | { |
| linxinda | 0:7d55df32c988 | 97 | pc.printf ("%lld\r\n",ptr->Data.usec); |
| linxinda | 0:7d55df32c988 | 98 | ptr=ptr->Next; |
| linxinda | 0:7d55df32c988 | 99 | } |
| linxinda | 0:7d55df32c988 | 100 | } |
| linxinda | 0:7d55df32c988 | 101 | |
| linxinda | 0:7d55df32c988 | 102 | PriorityQueue::~PriorityQueue(void) |
| linxinda | 0:7d55df32c988 | 103 | { |
| linxinda | 0:7d55df32c988 | 104 | struct Node *temp; /* Temporary variable */ |
| linxinda | 0:7d55df32c988 | 105 | while(head->Next!=NULL) |
| linxinda | 0:7d55df32c988 | 106 | { |
| linxinda | 0:7d55df32c988 | 107 | temp=head->Next; |
| linxinda | 0:7d55df32c988 | 108 | head=temp; |
| linxinda | 0:7d55df32c988 | 109 | } |
| linxinda | 0:7d55df32c988 | 110 | if(head->Next==NULL) |
| linxinda | 0:7d55df32c988 | 111 | delete head; |
| linxinda | 0:7d55df32c988 | 112 | } |