So I have been working on this program from my instructors notes, google and lots of brain power. Basically I just have to implement inserting, deleting and printing a queue using the array way. I am now at the point where I need you guys to tell me what isn't correct in my code. Right now I am only inserting and printing a queue and later (hopefully) I'll do the delete part. I'll appreciate your help.
Q.H (header file)
main.cCode: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);
Q.c (THE REAL STUFF)Code:#include <string.h> #include <stdio.h> #include <stdlib.h> #include "Q.h" /* Queue header file */ 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 = Qnew(); /* create an empty Q data object */ char command; // store current command int errorflag; // 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; default: printf("Command not recognized\n"); show_commands(); } printf("Queue command? "); scanf("%c",&command); } Q_free (myQ); return 0; }
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 typedef struct Q_struct { int contents[Q_MAX_SIZE]; int front; int count; } Q_struct; Q_type *Qnew() { Q_type *Q; Q = (Q_type *)malloc(sizeof(Q_type)); Q->front = 0; Q->count = 0; return Q; } void Q_add(Q_type *Q, int value, int *errorflag) { int newElementIndex; if (Q->count >= Q_MAX_SIZE) { *errorflag = ERROR_FULL_QUEUE; return; } *errorflag = EVERYTHING_OK; newElementIndex = (Q->front + Q->count) % Q_MAX_SIZE; Q->contents[newElementIndex] = value; Q->count++; } void Q_print (Q_type *Q) { int i; if (front < 0) return; if ( count >= front) { for(i = front; i <= count; i++) { printf("\n i = %d", i); printf(" %d ", contents[i]); } } else { for(i = front; i < Q_MAX_SIZE_; i++) { printf("\n i = %d", i); printf(" %d", contents[i]); } for(i = 0; i <= count; i++) { printf("\n i = %d", i); printf(" %d ", contents[i]); } } }



1Likes
LinkBack URL
About LinkBacks




