Alejandro Giraldo Martinez / qnode2
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers qnode.cpp Source File

qnode.cpp

00001 #include "qnode.h"
00002 #include "mbed.h"
00003 
00004  
00005 // A utility function to create a new linked list node.
00006 QNode* newNode(uint8_t *k)
00007 {   
00008     QNode *temp = (struct QNode*)malloc(sizeof(struct QNode));
00009     for(int i=0;i<4;i++){
00010         temp->uid[i]=k[i];
00011         }
00012     temp->next= NULL;
00013     return temp; 
00014 }
00015  
00016 // A utility function to create an empty queue
00017 struct Queue *createQueue()
00018 {
00019     struct Queue *q = (struct Queue*)malloc(sizeof(struct Queue));
00020     q->front = q->rear = NULL;
00021     return q;
00022 }
00023  
00024 // The function to add a key k to q
00025 void enQueue(struct Queue *q, uint8_t *k)
00026 {
00027     // Create a new LL node
00028     struct QNode *temp = newNode(k);
00029  
00030     // If queue is empty, then new node is front and rear both
00031     if (q->rear == NULL)
00032     {
00033        q->front = q->rear = temp;
00034        return;
00035     }
00036  
00037     // Add the new node at the end of queue and change rear
00038     q->rear->next = temp;
00039     q->rear = temp;
00040 }
00041  
00042 // Function to remove a key from given queue q
00043 struct QNode *deQueue(struct Queue *q)
00044 {
00045     // If queue is empty, return NULL.
00046     if (q->front == NULL)
00047        return NULL;
00048  
00049     // Store previous front and move front one node ahead
00050     struct QNode *temp = q->front;
00051     q->front = q->front->next;
00052  
00053     // If front becomes NULL, then change rear also as NULL
00054     if (q->front == NULL)
00055        q->rear = NULL;
00056     return temp;
00057 }
00058 
00059