I'm trying to make this queue program for school to take the next packet out of my queue (therefore delete it) in the queue function : struct packet_t queue_retrieve(struct queue_t queue) in line 73. I know that i have to use my write and read but i fail to understand how exactly.

I also have to implement it as a circular buffer so when it reaches the end of my queue it starts at 0 again. Also is it enough to free(q) and (p) in order to delete packets/queues like i did in the queue_destroy and packet_destroy functions or do i have to add more here?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>
#include <string.h>

struct packet_t {
    char *name;
    struct queue_t *queue;
};

struct queue_t {
    char *name;     // name of queue
    int size;       // size of queue
    int entries;    // number of valid entries in queue
    int time;       // timestamp (we will need that later in P6)
    struct packet_t **packets; // Array of pointer on packets
    int read;       // read-position (retrieve_packet())
    int write;      // write-Position (store_packet())
    long lost;      // number of all lost packets
};

struct queue_t *queue_create(char *name, int size) {
    int i;
    struct queue_t *q;                           /* temporary pointer on queue */
    q = malloc(sizeof(struct queue_t));

    if (q == NULL) {
        printf("Memory allocation failed");         //check for NULL
        exit(0);
    }

    q -> name = name;
    q -> size = size;
    q -> entries = 0;
    q -> write   = 0;
    q -> read    = -1;
    q -> ...