Okay I am lost. I have to do simple implementation of insertion and deletion of queue. The Q.h file was given to me and I have to work on that to build my code. I probably have a gazillion mistakes but please guide me. Here is my code. It is separate compilation, so:
Q.h (HEADER FILE)
Code:
struct Queue_struct;
typedef struct Queue_struct Q_type;
/* Allocate a new Queue and initialize it to be empty. */
Q_type *Q_new();
/* Deallocate a Queue and free the memory */
void Q_free(Q_type *);
/* Print the Queue in an attractive fashion */
void Q_print (Q_type *);
/* error codes for errorflag returned by Q_add and Q_delete */
#define ERROR_FULL_QUEUE 0
#define ERROR_EMPTY_QUEUE -1
#define EVERYTHING_OK 1
/* add an item at the end of the Queue */
void Q_add(Q_type *, int, int *errorflag);
/* remove an item from the front of the Queue */
int Q_delete(Q_type *, int *errorflag);
/* note errorflag is passed by reference so values can change */
Q.c FILE:
Code:
#include <stdio.h>
#include <stdlib.h>
#include .Q.h"
/*********************************************************
Implementation of the Queue ADT using a fixed size array.
*********************************************************/
/* Queue size must be an array of size 10 */
#define Q_MAX_SIZE 10
struct Q_struct
{
/* Pointer to the array of list elements.
Each element is a generic pointer.
*/
void **elements;
/* The number of elements in the list. */
int length;
};
static void _list_extend(Q_type *Q)
{
if (Q->elements == NULL) {
Q->elements = malloc(sizeof(void*));
}
else {
Q->elements = realloc(Q->elements,
sizeof(void*) * (Q->length + 1));
}
if (Q->elements == NULL) {
fprintf(stderr, "list_insert: out of memory.\n");
exit(1);
}
}
Q_type *Qnew()
{
Q_type *Q;
Q = (Q_type *)malloc(sizeof(Q_type));
Q->elements = NULL;
Q->length = 0;
return Q;
}
void Q_insert(Q_type *Q, void *data)
{
int i;
_list_extend(Q);
for (i = Q->length - 1; i >= 0; i--) {
Q->elements[i + 1] = Q->elements[i];
}
Q->elements[0] = data;
(Q->length)++;
}
main.c FILE:
Code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "Q.h" /* Queue header file */
typedef struct student {
// char lastName[16];
int id;
// float gpa;
// char major[5];
} student;
void showcommands()
{
printf("Queue Processing Commands:\n");
printf("==========================\n");
printf("n : \n");
printf("p : \n");
printf("a : \n");
printf("d : \n");
printf("v : \n");
printf("e : \n");
printf("f : \n");
}
main(void)
{
Q_type *myQ;
myQ = Q_new();
char command; // store current command
int errorcode; // possible error code
int value; // value to be inserted or returned
float avg; // average of queue values
show_commands();
printf("Queue command? ");
scanf("%c", command);
while (command != 'q')
{
switch (command)
{
case 'n': free(myQ);
myQ = Q_new();
break;
case 'p': Q_print(myQ);
break;
case 'a': printf("Enter value to insert: ");
scanf("%d", value);
Q_add(myQ, value, &errorflag);
if (errorflag == ERROR_FULL_QUEUE)
printf("Queue full -- insert failed.\n");
break;
case 'd': value = Q_delete(myQ, &errorflag);
if (errorflag != ERROR_EMPTY_QUEUE)
printf("Removed value [%d]\n",value);
else
printf("Queue empty -- delete failed.\n");
break;
default: printf("Command not recognized\n");
show_commands();
}
printf("Queue command? ");
scanf("%c",command);
}
Q_free (myQ);
return 0;
}