Challenge

Project 1 - Implement a fully functional Queue in C language using Linked lists

Part 1: Implement a Queue using Linked List in C

code;

#include

#include

// Structure to represent a node in the queue

typedef struct Node {

    float data;

    struct Node *next;

} Node;

// Structure to represent a queue

typedef struct Queue {

    Node *front;

    Node *rear;

} Queue;

// Function to initialize a queue

Queue *initializeQueue() {

    Queue *queue = (Queue *)malloc(sizeof(Queue));

    if (!queue) {

        printf("Memory error: Unable to allocate memory for the queue.\n");

        exit(EXIT_FAILURE);

    }

    queue->front = NULL;

    queue->rear = NULL;

    return queue;

}

// Function to check if the queue is empty

int isQueueEmpty(Queue *queue) {

    return (queue->front == NULL);

}

// Function to enqueue a data element

void enqueue(Queue *queue, float data) {

    Node *newNode = (Node *)malloc(sizeof(Node));

    if (!newNode) {

        printf("Memory error: Unable to allocate memory for a new node.\n");

        exit(EXIT_FAILURE);

    }

    newNode->data = data;

    newNode->next = NULL;

    if (isQueueEmpty(queue)) {

        queue->front = newNode;

        queue->rear = newNode;

    } else {

        queue->rear->next = newNode;

        queue->rear = newNode;

    }

}

// Function to dequeue a data element

float dequeue(Queue *queue) {

    if (isQueueEmpty(queue)) {

        printf("Queue is empty. Cannot dequeue.\n");

        exit(EXIT_FAILURE);

    }

    float data = queue->front->data;

    Node *temp = queue->front;

    if (queue->front == queue->rear) {

        queue->front = NULL;

        queue->rear = NULL;

    } else {

        queue->front = queue->front->next;

    }

    free(temp);

    return data;

}

// Function to destroy the queue and free memory

void destroyQueue(Queue *queue) {

    while (!isQueueEmpty(queue)) {

        dequeue(queue);

    }

    free(queue);

}

int main() {

    // Example usage of the queue

    Queue *queue = initializeQueue();

    enqueue(queue, 10.5);

    enqueue(queue, 20.3);

    enqueue(queue, 30.7);

    printf("Dequeued: %f\n", dequeue(queue));

    printf("Dequeued: %f\n", dequeue(queue));

    printf("Dequeued: %f\n", dequeue(queue));

    destroyQueue(queue);

    return 0;

}

This part implements a basic queue using a linked list in C.

Part 2: IMU Integration and Data Classification

To integrate the IMU and classify data into separate queues based on accelerometer and gyroscope, you'll need to use the FreeRTOS framework and integrate your IMU library. Since this is a more complex integration and can vary based on the hardware and IMU library you're using, it's beyond the scope of a simple code snippet. However, I can provide a general outline of how to proceed:

Integrate IMU Library:

Include the IMU library appropriate for your hardware.

Initialize the IMU and set up the necessary configurations.

Read IMU Data:

Read accelerometer and gyroscope data from the IMU.

Classify Data and Enqueue:

Based on the sensor type (accelerometer or gyroscope), enqueue the data into the respective queues.

Use the functions enqueue and isQueueEmpty from Part 1 to enqueue the data into the appropriate queues.

Create Tasks:

Create tasks to handle reading IMU data, classifying the data, and sending it to UART.

Use FreeRTOS task creation functions such as xTaskCreate.

Handle UART Communication:

Implement UART communication based on the classified data.

Ensure you handle synchronization and error handling appropriately in your FreeRTOS tasks.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.