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.
qnode.cpp@0:aaca51fb4854, 2018-07-28 (annotated)
- Committer:
- alejo5214416
- Date:
- Sat Jul 28 01:29:39 2018 +0000
- Revision:
- 0:aaca51fb4854
Comunicacion maestro esclavo con confirmacion de 1S 1Y 1N para solicitudes y respuestas
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| alejo5214416 | 0:aaca51fb4854 | 1 | #include "qnode.h" |
| alejo5214416 | 0:aaca51fb4854 | 2 | #include "mbed.h" |
| alejo5214416 | 0:aaca51fb4854 | 3 | |
| alejo5214416 | 0:aaca51fb4854 | 4 | |
| alejo5214416 | 0:aaca51fb4854 | 5 | // A utility function to create a new linked list node. |
| alejo5214416 | 0:aaca51fb4854 | 6 | QNode* newNode(uint8_t *k) |
| alejo5214416 | 0:aaca51fb4854 | 7 | { |
| alejo5214416 | 0:aaca51fb4854 | 8 | QNode *temp = (struct QNode*)malloc(sizeof(struct QNode)); |
| alejo5214416 | 0:aaca51fb4854 | 9 | for(int i=0;i<4;i++){ |
| alejo5214416 | 0:aaca51fb4854 | 10 | temp->uid[i]=k[i]; |
| alejo5214416 | 0:aaca51fb4854 | 11 | } |
| alejo5214416 | 0:aaca51fb4854 | 12 | temp->next= NULL; |
| alejo5214416 | 0:aaca51fb4854 | 13 | return temp; |
| alejo5214416 | 0:aaca51fb4854 | 14 | } |
| alejo5214416 | 0:aaca51fb4854 | 15 | |
| alejo5214416 | 0:aaca51fb4854 | 16 | // A utility function to create an empty queue |
| alejo5214416 | 0:aaca51fb4854 | 17 | struct Queue *createQueue() |
| alejo5214416 | 0:aaca51fb4854 | 18 | { |
| alejo5214416 | 0:aaca51fb4854 | 19 | struct Queue *q = (struct Queue*)malloc(sizeof(struct Queue)); |
| alejo5214416 | 0:aaca51fb4854 | 20 | q->front = q->rear = NULL; |
| alejo5214416 | 0:aaca51fb4854 | 21 | return q; |
| alejo5214416 | 0:aaca51fb4854 | 22 | } |
| alejo5214416 | 0:aaca51fb4854 | 23 | |
| alejo5214416 | 0:aaca51fb4854 | 24 | // The function to add a key k to q |
| alejo5214416 | 0:aaca51fb4854 | 25 | void enQueue(struct Queue *q, uint8_t *k) |
| alejo5214416 | 0:aaca51fb4854 | 26 | { |
| alejo5214416 | 0:aaca51fb4854 | 27 | // Create a new LL node |
| alejo5214416 | 0:aaca51fb4854 | 28 | struct QNode *temp = newNode(k); |
| alejo5214416 | 0:aaca51fb4854 | 29 | |
| alejo5214416 | 0:aaca51fb4854 | 30 | // If queue is empty, then new node is front and rear both |
| alejo5214416 | 0:aaca51fb4854 | 31 | if (q->rear == NULL) |
| alejo5214416 | 0:aaca51fb4854 | 32 | { |
| alejo5214416 | 0:aaca51fb4854 | 33 | q->front = q->rear = temp; |
| alejo5214416 | 0:aaca51fb4854 | 34 | return; |
| alejo5214416 | 0:aaca51fb4854 | 35 | } |
| alejo5214416 | 0:aaca51fb4854 | 36 | |
| alejo5214416 | 0:aaca51fb4854 | 37 | // Add the new node at the end of queue and change rear |
| alejo5214416 | 0:aaca51fb4854 | 38 | q->rear->next = temp; |
| alejo5214416 | 0:aaca51fb4854 | 39 | q->rear = temp; |
| alejo5214416 | 0:aaca51fb4854 | 40 | } |
| alejo5214416 | 0:aaca51fb4854 | 41 | |
| alejo5214416 | 0:aaca51fb4854 | 42 | // Function to remove a key from given queue q |
| alejo5214416 | 0:aaca51fb4854 | 43 | struct QNode *deQueue(struct Queue *q) |
| alejo5214416 | 0:aaca51fb4854 | 44 | { |
| alejo5214416 | 0:aaca51fb4854 | 45 | // If queue is empty, return NULL. |
| alejo5214416 | 0:aaca51fb4854 | 46 | if (q->front == NULL) |
| alejo5214416 | 0:aaca51fb4854 | 47 | return NULL; |
| alejo5214416 | 0:aaca51fb4854 | 48 | |
| alejo5214416 | 0:aaca51fb4854 | 49 | // Store previous front and move front one node ahead |
| alejo5214416 | 0:aaca51fb4854 | 50 | struct QNode *temp = q->front; |
| alejo5214416 | 0:aaca51fb4854 | 51 | q->front = q->front->next; |
| alejo5214416 | 0:aaca51fb4854 | 52 | |
| alejo5214416 | 0:aaca51fb4854 | 53 | // If front becomes NULL, then change rear also as NULL |
| alejo5214416 | 0:aaca51fb4854 | 54 | if (q->front == NULL) |
| alejo5214416 | 0:aaca51fb4854 | 55 | q->rear = NULL; |
| alejo5214416 | 0:aaca51fb4854 | 56 | return temp; |
| alejo5214416 | 0:aaca51fb4854 | 57 | } |
| alejo5214416 | 0:aaca51fb4854 | 58 | |
| alejo5214416 | 0:aaca51fb4854 | 59 |