Committer:
alejo5214416
Date:
Fri Jul 27 00:31:19 2018 +0000
Revision:
0:28ba51765608
Esclavo Maestro

Who changed what in which revision?

UserRevisionLine numberNew contents of line
alejo5214416 0:28ba51765608 1 #include "qnode.h"
alejo5214416 0:28ba51765608 2 #include "mbed.h"
alejo5214416 0:28ba51765608 3
alejo5214416 0:28ba51765608 4
alejo5214416 0:28ba51765608 5 // A utility function to create a new linked list node.
alejo5214416 0:28ba51765608 6 QNode* newNode(uint8_t *k)
alejo5214416 0:28ba51765608 7 {
alejo5214416 0:28ba51765608 8 QNode *temp = (struct QNode*)malloc(sizeof(struct QNode));
alejo5214416 0:28ba51765608 9 for(int i=0;i<4;i++){
alejo5214416 0:28ba51765608 10 temp->uid[i]=k[i];
alejo5214416 0:28ba51765608 11 }
alejo5214416 0:28ba51765608 12 temp->next= NULL;
alejo5214416 0:28ba51765608 13 return temp;
alejo5214416 0:28ba51765608 14 }
alejo5214416 0:28ba51765608 15
alejo5214416 0:28ba51765608 16 // A utility function to create an empty queue
alejo5214416 0:28ba51765608 17 struct Queue *createQueue()
alejo5214416 0:28ba51765608 18 {
alejo5214416 0:28ba51765608 19 struct Queue *q = (struct Queue*)malloc(sizeof(struct Queue));
alejo5214416 0:28ba51765608 20 q->front = q->rear = NULL;
alejo5214416 0:28ba51765608 21 return q;
alejo5214416 0:28ba51765608 22 }
alejo5214416 0:28ba51765608 23
alejo5214416 0:28ba51765608 24 // The function to add a key k to q
alejo5214416 0:28ba51765608 25 void enQueue(struct Queue *q, uint8_t *k)
alejo5214416 0:28ba51765608 26 {
alejo5214416 0:28ba51765608 27 // Create a new LL node
alejo5214416 0:28ba51765608 28 struct QNode *temp = newNode(k);
alejo5214416 0:28ba51765608 29
alejo5214416 0:28ba51765608 30 // If queue is empty, then new node is front and rear both
alejo5214416 0:28ba51765608 31 if (q->rear == NULL)
alejo5214416 0:28ba51765608 32 {
alejo5214416 0:28ba51765608 33 q->front = q->rear = temp;
alejo5214416 0:28ba51765608 34 return;
alejo5214416 0:28ba51765608 35 }
alejo5214416 0:28ba51765608 36
alejo5214416 0:28ba51765608 37 // Add the new node at the end of queue and change rear
alejo5214416 0:28ba51765608 38 q->rear->next = temp;
alejo5214416 0:28ba51765608 39 q->rear = temp;
alejo5214416 0:28ba51765608 40 }
alejo5214416 0:28ba51765608 41
alejo5214416 0:28ba51765608 42 // Function to remove a key from given queue q
alejo5214416 0:28ba51765608 43 struct QNode *deQueue(struct Queue *q)
alejo5214416 0:28ba51765608 44 {
alejo5214416 0:28ba51765608 45 // If queue is empty, return NULL.
alejo5214416 0:28ba51765608 46 if (q->front == NULL)
alejo5214416 0:28ba51765608 47 return NULL;
alejo5214416 0:28ba51765608 48
alejo5214416 0:28ba51765608 49 // Store previous front and move front one node ahead
alejo5214416 0:28ba51765608 50 struct QNode *temp = q->front;
alejo5214416 0:28ba51765608 51 q->front = q->front->next;
alejo5214416 0:28ba51765608 52
alejo5214416 0:28ba51765608 53 // If front becomes NULL, then change rear also as NULL
alejo5214416 0:28ba51765608 54 if (q->front == NULL)
alejo5214416 0:28ba51765608 55 q->rear = NULL;
alejo5214416 0:28ba51765608 56 return temp;
alejo5214416 0:28ba51765608 57 }
alejo5214416 0:28ba51765608 58
alejo5214416 0:28ba51765608 59