All pastes #584009 Raw Edit

Mine

public text v1 · immutable
·published 2007-06-22 16:12 UTC
rendered paste body
#include <stdio.h>
#include <stdlib.h>

#define MAX_Q_SIZE 8 /* Maximum size of the queue */

/*
 * Declare the queue and the front and rear indexes
 * They are also called head and tail
 */
char queue[MAX_Q_SIZE];
int rear_index = 0, front_index = 0;
int elements_in_q = 0;

int insert(char value)
{
    if (elements_in_q == MAX_Q_SIZE)
        return 1;

    elements_in_q++;
    rear_index = rear_index + 1;
    if (rear_index == MAX_Q_SIZE)
        rear_index = 0;

    queue[rear_index] = value;
    return 0;
}

int delete()
{
    if (!elements_in_q)
    {
        return -1;
    }

    elements_in_q--;
    front_index = front_index + 1;
    if (front_index == MAX_Q_SIZE)
        front_index = 0;
    return (queue[front_index]);
}

char operations[]={'i','a','i','b','i','c','d','i','d','i','e','i','f','i','g',
                'i','h','i','i','i','k','d','d','d','d','d','d','d','d','d'};

int main()
{
    char   op, value;
    int    op_index = 0;
    int    deleted_value;

    while (op_index < sizeof(operations)) {
        op = operations[op_index++];
        switch (op) {
            case 'i':
                if (op_index >= sizeof(operations)) {
                    printf("insert operation without value at end of"
                        " operations array\n");
                    exit(1);
                }
                value = operations[op_index++];
                if (insert(value)) {
                    printf("insert failed - queue full for op_index = %d\n",
                        op_index);
                }
                break;
            case 'd':
                if ((deleted_value = delete()) == -1) {
                    printf("cannot delete from empty queue. op_index = %d\n",
                        op_index);
                    break;
                }
                printf("deleted %c\n", deleted_value);
                break;
            default:
                printf("invalid operation %c at op_index = %d\n", op, op_index);

        }
    }
}