c3-1.c
/* code: c3-1.c (v1.18.00) */
#include<stdio.h>
#include<stdlib.h>
#define MAX 128
#define ENQUEUE_SUCCESS 1
#define ENQUEUE_FAILURE -1
#define DEQUEUE_SUCCESS 2
#define DEQUEUE_FAILURE -2
/* ------------------------------------------- */
void queue_init (int *front, int *rear) {
*front = -1;
*rear = -1;
}
/* ------------------------------------------- */
int enqueue (int q[], int *rear, int data) {
if (*rear < MAX - 1) {
*rear = *rear + 1;
q[*rear] = data;
return ENQUEUE_SUCCESS;
}
else {
return ENQUEUE_FAILURE;
}
}
/* ------------------------------------------- */
int dequeue (int q[], int *front, int rear, int *data) {
if (*front == rear) {
return DEQUEUE_FAILURE;
}
*front = *front + 1;
*data = q[*front];
return DEQUEUE_SUCCESS;
}
/* ------------------------------------------- */
int main () {
int queue[MAX];
int front, rear, data;
int stat;
queue_init (&front, &rear);
enqueue (queue, &rear, 100);
enqueue (queue, &rear, 200);
enqueue (queue, &rear, 300);
enqueue (queue, &rear, 400);
enqueue (queue, &rear, 500);
while (rear - front) {
stat = dequeue (queue, &front, rear, &data);
if (stat == DEQUEUE_SUCCESS) {
printf ("%d\n", data);
}
else {
printf ("QUEUE is empty\n");
}
}
return 0;
}
q3-1.c
/* code: q3-1.c (v1.18.00) */
#include<stdio.h>
#include<stdlib.h>
#define MAX 128
#define ENQUEUE_SUCCESS 1
#define ENQUEUE_FAILURE -1
#define DEQUEUE_SUCCESS 2
#define DEQUEUE_FAILURE -2
/* ------------------------------------------- */
void queue_init (int *front, int *rear) {
*front = -1;
*rear = -1;
}
/* ------------------------------------------- */
int enqueue (int q[], int *rear, int data) {
if (*rear < MAX - 1) {
*rear = *rear + 1;
q[*rear] = data;
return ENQUEUE_SUCCESS;
}
else {
return ENQUEUE_FAILURE;
}
}
/* ------------------------------------------- */
int dequeue (int q[], int *front, int rear, int *data) {
if (*front == rear) {
return DEQUEUE_FAILURE;
}
*front = *front + 1;
*data = q[*front];
return DEQUEUE_SUCCESS;
}
/* ------------------------------------------- */
void queue_display (int q[], int front, int rear) {
int i;
if (front == rear) {
printf ("QUEUE empty!");
}
printf ("[ ");
for (i = front + 1; i <= rear; i++) {
printf ("%d ", q[i]);
}
printf ("]\n");
}
/* ------------------------------------------- */
int main () {
int queue[MAX];
int front, rear, data;
int stat;
queue_init (&front, &rear);
enqueue (queue, &rear, 100);
enqueue (queue, &rear, 200);
enqueue (queue, &rear, 300);
enqueue (queue, &rear, 400);
enqueue (queue, &rear, 500);
queue_display (queue, front, rear);
while (rear - front) {
stat = dequeue (queue, &front, rear, &data);
if (stat == DEQUEUE_SUCCESS) {
printf ("%d\n", data);
}
else {
printf ("QUEUE is empty\n");
}
}
return 0;
}
q3-2.c
/* code: q3-2.c (v1.18.00) */
#include<stdio.h>
#include<stdlib.h>
#define MAX 128
#define ENQUEUE_SUCCESS 1
#define ENQUEUE_FAILURE -1
#define DEQUEUE_SUCCESS 2
#define DEQUEUE_FAILURE -2
#define PEEK_SUCCESS 3
#define PEEK_FAILURE -3
/* ------------------------------------------- */
void queue_init (int *front, int *rear) {
*front = -1;
*rear = -1;
}
/* ------------------------------------------- */
int enqueue (int q[], int *rear, int data) {
if (*rear < MAX - 1) {
*rear = *rear + 1;
q[*rear] = data;
return ENQUEUE_SUCCESS;
}
else {
return ENQUEUE_FAILURE;
}
}
/* ------------------------------------------- */
int dequeue (int q[], int *front, int rear, int *data) {
if (*front == rear) {
return DEQUEUE_FAILURE;
}
*front = *front + 1;
*data = q[*front];
return DEQUEUE_SUCCESS;
}
/* ------------------------------------------- */
int peek_front (int q[], int front, int rear, int *data) {
if (front == rear) {
return PEEK_FAILURE;
}
*data = q[front + 1];
return PEEK_SUCCESS;
}
/* ------------------------------------------- */
int main () {
int queue[MAX];
int front, rear, data, i;
int d[] = { 680, 670, 690, 650, 660 };
queue_init (&front, &rear);
data = 0;
for (i = 0; i < 5; i++) {
printf ("enqueue: %d\n", d[i]);
enqueue (queue, &rear, d[i]);
}
printf ("\n");
for (i = 0; i < 3; i++) {
dequeue (queue, &front, rear, &data);
printf ("dequeue: %d\n", data);
}
printf ("\n");
peek_front (queue, front, rear, &data);
printf ("peek_front: %d\n", data);
return 0;
}
q3-3.c
/* code: q3-3.c (v1.18.00) */
#include<stdio.h>
#include<stdlib.h>
#define DATA_SIZE 100000000
#define MAX 100000000
#define ENQUEUE_SUCCESS 1
#define ENQUEUE_FAILURE -1
#define DEQUEUE_SUCCESS 2
#define DEQUEUE_FAILURE -2
/* ------------------------------------------- */
void queue_init (int *front, int *rear) {
*front = -1;
*rear = -1;
}
/* ------------------------------------------- */
int enqueue (int q[], int *rear, int data) {
if (*rear < MAX - 1) {
*rear = *rear + 1;
q[*rear] = data;
return ENQUEUE_SUCCESS;
}
else {
return ENQUEUE_FAILURE;
}
}
/* ------------------------------------------- */
int dequeue (int q[], int *front, int rear, int *data) {
if (*front == rear) {
return DEQUEUE_FAILURE;
}
*front = *front + 1;
*data = q[*front];
return DEQUEUE_SUCCESS;
}
/* ------------------------------------------- */
int main () {
int *queue;
int front, rear, data;
int i, stat;
queue = malloc (sizeof (int) * MAX);
queue_init (&front, &rear);
printf ("queue size: %d\n\n", DATA_SIZE);
printf ("enqueue:\n");
for (i = 0; i < DATA_SIZE; i++) {
data = rand () % 1000;
if (i < 10)
printf ("%3d ", data);
enqueue (queue, &rear, data);
}
printf ("\n");
printf ("dequeue:\n");
while ((10 - 1) - front) {
stat = dequeue (queue, &front, rear, &data);
if (stat == DEQUEUE_SUCCESS) {
printf ("%d ", data);
}
else {
printf ("QUEUE is empty\n");
}
}
printf ("\n");
free (queue);
return 0;
}
q3-4.c
/* code: q3-4.c (v1.18.00) */
#include<stdio.h>
#include<stdlib.h>
#define MAX 128 /* ring buffer max size */
#define ENQUEUE_SUCCESS 1
#define ENQUEUE_FAILURE -1
#define DEQUEUE_SUCCESS 2
#define DEQUEUE_FAILURE -2
/* ------------------------------------------- */
void rb_queue_init (int *front, int *rear) {
*front = 0;
*rear = 0;
}
/* ------------------------------------------- */
int rb_enqueue (int q[], int *front, int *rear, int data) {
int index_f, index_r, index_q;
index_f = *front % MAX;
index_r = (*rear + 1) % MAX;
if (index_f != index_r) {
index_q = (*rear)++ % MAX;
q[index_q] = data;
return ENQUEUE_SUCCESS;
}
else {
return ENQUEUE_FAILURE;
}
}
/* ------------------------------------------- */
int rb_dequeue (int q[], int *front, int *rear, int *data) {
int index;
if (*front != *rear) {
index = (*front)++ % MAX;
*data = q[index];
return DEQUEUE_SUCCESS;
}
else {
return DEQUEUE_FAILURE;
}
}
/* ------------------------------------------- */
int main () {
int queue[MAX];
int front, rear, data;
int stat;
rb_queue_init (&front, &rear);
rb_enqueue (queue, &front, &rear, 100);
rb_enqueue (queue, &front, &rear, 200);
rb_enqueue (queue, &front, &rear, 300);
rb_enqueue (queue, &front, &rear, 400);
rb_enqueue (queue, &front, &rear, 500);
while (rear - front) {
stat = rb_dequeue (queue, &front, &rear, &data);
if (stat == DEQUEUE_SUCCESS) {
printf ("%d\n", data);
}
else {
printf ("QUEUE is empty\n");
}
}
return 0;
}
EOF