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 -> lost    = 0;

    return (q);
}

long queue_store(struct queue_t *q, struct packet_t *p) {

    int total = 0;

    q -> entries ++;
    q -> write++;
    q -> read++;

    p -> name = q -> name;

    if (q -> entries < q -> size) {

        total ++;

        return total;

    }
    else {
        packet_destroy(p);                          // packet gets destroyed not enough space
        q -> lost ++;                               // lost packet counter
        return 0;
    }

}

struct packet_t* queue_retrieve(struct queue_t *q) {
    //need help here

    struct packet_t *p;

    ...
    return (p);
}

int queue_destroy(struct queue_t *q) {

    free (q);                                        // destroys queue and frees memory allocated

}

struct packet_t* packet_create(char *p) {

    struct packet_t * packet;

    packet = malloc (sizeof(struct packet_t));

    packet -> name = p;

    return (packet);
}

int packet_destroy(struct packet_t *packet) {      // destroys packet and frees memory allocated

    free(p);

}

// to test my queue
int main(void) {

    long i;

    struct queue_t *Q;                              // pointer on queue
    struct packet_t *P;                             // pointer on packet

    clock_t start, ende;

    start = clock();

    for (i = 0; i < 1000000; i++) {
        P = packet_create ("tester");
    }
    ende = clock();
    printf("create packets:\n");
    printf("i     = %ld\n", i);
    printf("T[s]  = %g\n", (double) (ende - start) / CLOCKS_PER_SEC);
    printf("\n");

    start = clock();
    for (i = 0; i < 1000000; i++) {
        Q = queue_create ("test-queue with 100 spaces", 100);
    }
    ende = clock();

    printf("create queues:\n");
    printf("i     =%ld\n", i);
    printf("T[s]  = %g\n", (double) (ende - start) / CLOCKS_PER_SEC);
    printf("\n");

    start = clock();

    Q = queue_create ("test, 100", 100);

    // fill queue halfway
    for (i = 0; i < 50; i++) {
         P = packet_create ("none");
        queue_store (Q,P);
    }

    // create many packets
    for (i = 0; i < 1000000; i++) {
        P = packet_create ("none");
        queue_store (Q,P);
        P=queue_retrieve(Q);
        packet_destroy(P);
    }

    // delete prefill
    for (i = 0; i < 50; i++) {
        P = queue_retrieve(Q);
        packet_destroy(P);
    }

    ende = clock();

    printf("create queues, <create packets, safe,load,destroy>:\n");

    printf("i     =%ld\n", i);
    printf("T[s]  = %g\n", (double) (ende - start) / CLOCKS_PER_SEC);
    printf("\n");

    start = clock();

    Q = queue_create ("test-queue for 1000 packets", 1000);

    for (i = 0; i < 900; i++) {
        P = packet_create ("aha");
        queue_store (Q,P);

    }
    for (i = 0; i < 901; i++) {
        P=queue_retrieve(Q);
        if (NULL == P)
            printf ("no packet\n");
        else
            packet_destroy(P);

    }
    ende = clock();

    printf("create queue, <create packet, safe,load,destroy>:\n");

    printf("i     =%ld\n", i);
    printf("T[s]  = %g\n", (double) (ende - start) / CLOCKS_PER_SEC);
    printf("\n");

    start = clock();

    Q = queue_create ("test, 1000", 1000);

    for (i = 0; i < 900; i++) {
        P = packet_create ((int)i,0.0,0L,NULL);
        queue_store (Q,P);

    }
    for (i = 0; i < 450; i++) {
        P=queue_retrieve(Q);
        if (NULL == P)
            printf ("no packet\n");
        else
            packet_destroy(P);

    }
    ende = clock();

    printf("create queue, <create packet, safe,load,destroy>:\n");

    printf("i     =%ld\n", i);
    printf ("Q->entries = %d\n", Q->entries);
    printf("T[s]  = %g\n", (double) (ende - start) / CLOCKS_PER_SEC);
    printf("\n");
}

Since this is your assignment my answer is that you test your code and if it works, you submit it.

Don't get trapped in the is it done this way or another. To me, the exercise is to show your solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.